728x90
목표
아래와 같은 사이드 메뉴를 구현하는 방법 기록,
Drawer Widget 사용
import 'package:flutter/material.dart';
class NavDrawer extends StatelessWidget {
const NavDrawer({super.key});
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.green,
),
child: Text(
'Side menu',
style: TextStyle(color: Colors.white, fontSize: 25),
),
),
ListTile(
leading: const Icon(Icons.input),
title: const Text('Welcome'),
onTap: () => {},
),
ListTile(
leading: const Icon(Icons.verified_user),
title: const Text('Profile'),
onTap: () => {Navigator.of(context).pop()},
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('Settings'),
onTap: () => {Navigator.of(context).pop()},
),
ListTile(
leading: const Icon(Icons.border_color),
title: const Text('Feedback'),
onTap: () => {Navigator.of(context).pop()},
),
ListTile(
leading: const Icon(Icons.exit_to_app),
title: const Text('Logout'),
onTap: () => {Navigator.of(context).pop()},
),
],
),
);
}
}
Scaffold의 drawer에 넣기
import 'package:flutter/material.dart';
import 'nav_draw.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: const NavDrawer(),
appBar: AppBar(
title: const Text('Side menu'),
),
body: const Center(
child: Text('Side Menu Tutorial'),
),
);
}
}
구현 화면
무척 간단하지만, 저처럼 강의 안 보고 독학하시는 분들은 의외로 여기까지 접근하는데 헤맬 수도 있으니 기록해 보았습니다.
728x90
'flutter' 카테고리의 다른 글
app link가 app이 아니라 browser를 열때 (0) | 2023.09.07 |
---|---|
flutter unit test 구현 (0) | 2023.09.04 |
Set up universal links for iOS (0) | 2023.09.03 |
Set up app links for Android (0) | 2023.09.03 |
(flutter) uni_links + go_router => routing 안됨 (해결 못함.) (0) | 2023.08.30 |
댓글