Sub topic 9
Adding a Drawer
This chapter assumes that you know basic Dart syntax and can create a StatelessWidget with a build method. You should also have a Flutter project that runs.
A drawer is a panel that slides in from the side of a screen. It usually contains navigation links, account details, or app-level actions. In Flutter, you add a drawer through the Scaffold.drawer property.
How the Drawer fits into the widget tree
A Scaffold connects the drawer to the screen's AppBar. When the drawer property is not null, Flutter usually adds a menu button to the leading side of the AppBar. Pressing that button opens the drawer.
The drawer is an overlay. It appears above the current page, so the user can choose an item without leaving the current route immediately.
Build a drawer with navigation
The following app has two named routes. The home screen has a drawer with a link to the settings screen.
import 'package:flutter/material.dart';
void main() {
runApp(const InboxApp());
}
class InboxApp extends StatelessWidget {
const InboxApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Inbox',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
useMaterial3: true,
),
initialRoute: '/',
routes: {
'/': (context) => const HomePage(),
'/settings': (context) => const SettingsPage(),
},
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inbox'),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(color: Colors.teal),
child: Align(
alignment: Alignment.bottomLeft,
child: Text(
'Inbox menu',
style: TextStyle(color: Colors.white, fontSize: 24),
),
),
),
ListTile(
leading: const Icon(Icons.inbox),
title: const Text('Inbox'),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('Settings'),
onTap: () {
Navigator.pop(context);
Navigator.pushNamed(context, '/settings');
},
),
],
),
),
body: const Center(
child: Text('Your messages appear here'),
),
);
}
}
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
body: const Center(
child: Text('Change your preferences here'),
),
);
}
}Drawer expects one child. ListView is a good choice because it lets the menu scroll if it becomes taller than the screen. padding: EdgeInsets.zero removes the default list padding so the header reaches the top edge.
DrawerHeader displays a prominent top section. The ListTile widgets provide standard rows with icons, titles, and tap handlers.
Close the drawer before navigating
Navigator.pop(context) closes the drawer because the drawer is displayed as a route-like overlay. The following call, Navigator.pushNamed, then places the settings route on top of the home route.
The order matters:
- Close the drawer.
- Push the destination route.
If you navigate without closing the drawer, the drawer can remain visible over the new screen or create confusing back-button behavior.
When SettingsPage is pushed, its AppBar normally displays a back button automatically. The route is above the home route in the navigation stack, so pressing the back button removes the settings route and reveals the home screen.
Common errors
- Forgetting
Navigator.pop(context)leaves the drawer open while navigation starts. - Calling
Navigator.poptwice can remove the current page instead of only closing the drawer. - Repeating a large drawer in every screen makes the app harder to maintain. Extract shared drawer content into its own widget when several screens use the same menu.
- Omitting a route from
MaterialApp.routescausespushNamedto fail when the name is not registered. - Putting a drawer inside the
bodyinstead of usingScaffold.drawerprevents Flutter from connecting it to the standardAppBarmenu button.
A shared drawer can receive the current destination and highlight the selected item. Keep navigation decisions near the app's route configuration, while keeping each screen responsible for its own content.
Exercise
What should a drawer item do before calling Navigator.pushNamed to open another screen?
Show answer
It should call Navigator.pop(context) first to close the drawer, then call Navigator.pushNamed with the registered route name.