What is a Drawer in Flutter?
A Drawer is a panel that slides in from the side of a Scaffold, typically used for navigation. It is a common UI pattern in mobile apps, providing access to different screens or actions without taking up permanent screen space. In Flutter, you add a drawer by setting the drawer property of a Scaffold. The drawer usually contains a DrawerHeader and a list of ListTiles for navigation items.
Basic Drawer Implementation
The drawer opens when the user taps the menu icon in the app bar or swipes from the edge. Tapping a ListTile typically closes the drawer (using Navigator.pop(context)) and navigates to another screen.
DrawerHeader – Customizing the Header
DrawerHeader is a Material widget that provides a decorative header. You can put any widget inside, but it's common to use a Column with an avatar, name, and email. You can also use UserAccountsDrawerHeader for a pre‑styled header with an account switcher.
Navigating with Drawer
To navigate to a new screen when a drawer item is tapped, you typically use Navigator.push to go to a new route. After pushing, you may want to close the drawer – either manually (by calling Navigator.pop(context)) or automatically if you're using pushReplacement (the drawer closes because the scaffold is replaced). The common pattern is:
Closing the Drawer Programmatically
The drawer closes automatically when you call Navigator.pop(context). If you need to close it from a deeper widget, you can use the Scaffold state: Scaffold.of(context).closeDrawer(). However, that's only available if you have a Scaffold ancestor. Alternatively, you can use the Navigator.pop(context) from anywhere inside the drawer's widget tree.
Customizing Drawer Appearance
You can customize the drawer's width, background color, and shape using the Drawer widget's properties. The Drawer itself is a Material widget, so you can set its elevation, color, and shape.
Adding a Scrollable Drawer
If the drawer content is too tall, wrap it in a ListView. The Drawer itself is a Material that can contain any widget; ListView provides scrolling.
Common Mistakes
- Forgetting to close the drawer: After navigating, the drawer stays open unless you call
Navigator.pop(context).
- Forgetting to close the drawer: After navigating, the drawer stays open unless you call
- Using
Scaffold.of(context)without a Scaffold ancestor: In the drawer's builder,Scaffold.of(context)returns the correct scaffold because the drawer is inside the scaffold's tree. However, if you're inside a separate widget, ensure you have aBuildContextwith aScaffoldancestor.
- Using
- Not handling large content: Without a
ListView, the drawer may overflow.
- Not handling large content: Without a
- Using a
Drawerwithout aScaffold: The drawer only works as a child ofScaffold.
- Using a
- Not providing unique keys for list items: If you have dynamic items, use
Keyto preserve state.
- Not providing unique keys for list items: If you have dynamic items, use
Best Practices
- Use
DrawerHeaderorUserAccountsDrawerHeaderfor a consistent header.
- Use
- For navigation items, use
ListTilewith an icon and text.
- For navigation items, use
- After navigation, always close the drawer (
Navigator.pop(context)).
- After navigation, always close the drawer (
- For long drawers, wrap content in a
ListView.
- For long drawers, wrap content in a
- Use
constfor static widgets to improve performance.
- Use
Key Takeaways
- A drawer is added via the
drawerproperty ofScaffold.
- A drawer is added via the
- The drawer typically contains a
DrawerHeaderand a list ofListTiles.
- The drawer typically contains a
- Use
Navigator.pop(context)to close the drawer.
- Use
- Customize width, color, and shape with
Drawerproperties.
- Customize width, color, and shape with
- Wrap content in a
ListViewif it might exceed the screen height.
- Wrap content in a