In apps that employ Material Design, there are two primary options fornavigation: tabs and drawers. When there is insufficient space to support tabs,Drawers provide a handy alternative.

In Flutter, we can use the DrawerWidget in combination with a Scaffoldto create a layout with a Material Design Drawer.

Directions

  • Create a Scaffold
  • Add a drawer
  • Populate the drawer with items
  • Close the drawer programmatically

1. Create a Scaffold

In order to add a Drawer to our app, we’ll need to wrap it in aScaffoldWidget. The Scaffold Widget provides a consistent visual structure to apps thatfollow the Material Design Guidelines. It also supports special Material Designcomponents, such as Drawers, AppBars, and SnackBars.

In this case, we’ll want to create a Scaffold with a drawer:

  1. Scaffold(
  2. drawer: // We'll add our Drawer here in the next step!
  3. );

2. Add a drawer

We can now add a drawer to our Scaffold. A drawer could be any Widget, butit’s often best to use the Drawer widget from thematerial library,which adheres to the Material Design spec.

  1. Scaffold(
  2. drawer: Drawer(
  3. child: // We'll populate the Drawer in the next step!
  4. )
  5. );

3. Populate the drawer with items

Now that we have a Drawer in place, we can add content to it. In this example,we will use a ListView.While we could use a Column Widget, ListView is handy in this situationbecause it will allow users to scroll through the drawer if the content takes upmore space than the screen supports.

We will populate the ListView with aDrawerHeaderand two ListTileWidgets. For more information on working with Lists, please see thelist recipes.

  1. Drawer(
  2. // Add a ListView to the drawer. This ensures the user can scroll
  3. // through the options in the Drawer if there isn't enough vertical
  4. // space to fit everything.
  5. child: ListView(
  6. // Important: Remove any padding from the ListView.
  7. padding: EdgeInsets.zero,
  8. children: <Widget>[
  9. DrawerHeader(
  10. child: Text('Drawer Header'),
  11. decoration: BoxDecoration(
  12. color: Colors.blue,
  13. ),
  14. ),
  15. ListTile(
  16. title: Text('Item 1'),
  17. onTap: () {
  18. // Update the state of the app
  19. // ...
  20. },
  21. ),
  22. ListTile(
  23. title: Text('Item 2'),
  24. onTap: () {
  25. // Update the state of the app
  26. // ...
  27. },
  28. ),
  29. ],
  30. ),
  31. );

4. Close the drawer programmatically

After a user taps on an item, we often want to close the drawer. How can weachieve this? Using the Navigator!

When a user opens the Drawer, Flutter adds the drawer to the navigationstack under the hood. Therefore, to close the drawer, we can callNavigator.pop(context).

  1. ListTile(
  2. title: Text('Item 1'),
  3. onTap: () {
  4. // Update the state of the app
  5. // ...
  6. // Then close the drawer
  7. Navigator.pop(context);
  8. },
  9. ),

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. final appTitle = 'Drawer Demo';
  5. @override
  6. Widget build(BuildContext context) {
  7. return MaterialApp(
  8. title: appTitle,
  9. home: MyHomePage(title: appTitle),
  10. );
  11. }
  12. }
  13. class MyHomePage extends StatelessWidget {
  14. final String title;
  15. MyHomePage({Key key, this.title}) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. appBar: AppBar(title: Text(title)),
  20. body: Center(child: Text('My Page!')),
  21. drawer: Drawer(
  22. // Add a ListView to the drawer. This ensures the user can scroll
  23. // through the options in the Drawer if there isn't enough vertical
  24. // space to fit everything.
  25. child: ListView(
  26. // Important: Remove any padding from the ListView.
  27. padding: EdgeInsets.zero,
  28. children: <Widget>[
  29. DrawerHeader(
  30. child: Text('Drawer Header'),
  31. decoration: BoxDecoration(
  32. color: Colors.blue,
  33. ),
  34. ),
  35. ListTile(
  36. title: Text('Item 1'),
  37. onTap: () {
  38. // Update the state of the app
  39. // ...
  40. // Then close the drawer
  41. Navigator.pop(context);
  42. },
  43. ),
  44. ListTile(
  45. title: Text('Item 2'),
  46. onTap: () {
  47. // Update the state of the app
  48. // ...
  49. // Then close the drawer
  50. Navigator.pop(context);
  51. },
  52. ),
  53. ],
  54. ),
  55. ),
  56. );
  57. }
  58. }

Drawer Demo