DEV Community

Aaron K Saunders
Aaron K Saunders

Posted on

Flutter Drawer with State Management

Getting Started

  • Install the Provider Package from Dart Pub the package manager for the Dart programming language, containing reusable libraries & packages
  • The Provider Package
    • A dependency injection system built with widgets for widgets. provider is mostly syntax sugar for InheritedWidget, to make common use-cases straightforward.
    • Provider Package Documentation

What It Looks Like

Home Page

Drawer Showing Active Home Page

Drawer Showing Active Active Page

Quick Overview of Code

Create a separate widget for the drawer and just use in anywhere you need to.

Manage the Drawer State using a Provider that will keep track of currently selected menu item

this class contains state information and the appropriate methods to modify the state. When state updates happen, the call to notifyListeners is call to let the application know the the appropriate widgets need to be redrawn

class DrawerStateInfo with ChangeNotifier {
  int _currentDrawer = 0;
  int get getCurrentDrawer => _currentDrawer;

  void setCurrentDrawer(int drawer) {
    _currentDrawer = drawer;
    notifyListeners();
  }

  void increment() {
    notifyListeners();
  }
}
Enter fullscreen mode Exit fullscreen mode

Adding State Management to the Widget tree; we are using the MultiProvider here for demonstration purposes of how you could potentially have multiple classes managing different aspects of the application state. Each one would be added to the providers array property.

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.teal,
        ),
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      ),
      providers: <SingleChildCloneableWidget>[
        ChangeNotifierProvider<DrawerStateInfo>(
            builder: (_) => DrawerStateInfo()),
      ],
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Creating The Drawer Widget for reuse in application. We are tracking the state to specify which item is the active item in the menu and then based on that we are updating the styling of the menu item's title

class MyDrawer extends StatelessWidget {
  MyDrawer(this.currentPage);

  final String currentPage;

  @override
  Widget build(BuildContext context) {
    var currentDrawer = Provider.of<DrawerStateInfo>(context).getCurrentDrawer;
    return Drawer(
      child: ListView(
        children: <Widget>[
          ListTile(
            title: Text(
              "Home",
              style: currentDrawer == 0
                  ? TextStyle(fontWeight: FontWeight.bold)
                  : TextStyle(fontWeight: FontWeight.normal),
            ),
            trailing: Icon(Icons.arrow_forward),
            onTap: () {
              Navigator.of(context).pop();
              if (this.currentPage == "Home") return;

              Provider.of<DrawerStateInfo>(context).setCurrentDrawer(0);

              Navigator.of(context).pushReplacement(MaterialPageRoute(
                  builder: (BuildContext context) =>
                      MyHomePage(title: "Home")));
            },
          ),
          ListTile(
            title: Text(
              "About",
              style: currentDrawer == 1
                  ? TextStyle(fontWeight: FontWeight.bold)
                  : TextStyle(fontWeight: FontWeight.normal),
            ),
            trailing: Icon(Icons.arrow_forward),
            onTap: () {
              Navigator.of(context).pop();
              if (this.currentPage == "About") return;

              Provider.of<DrawerStateInfo>(context).setCurrentDrawer(1);

              Navigator.of(context).pushReplacement(MaterialPageRoute(
                  builder: (BuildContext context) => MyAboutPage()));
            },
          ),
        ],
      ),
    );
  }
}

Enter fullscreen mode Exit fullscreen mode

Use of Drawer in one of your pages; when using the Drawer, we specify the name of the Drawer when creating the instance. This should probably be a constant or maybe even an object containing addition information but this works for the example.

class MyAboutPage extends StatefulWidget {
  @override
  _MyAboutPageState createState() => _MyAboutPageState();
}

class _MyAboutPageState extends State<MyAboutPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('About Page'),
      ),
      drawer: MyDrawer("About"),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

GitHub logo aaronksaunders / flutter_drawer_detail

example flutter app showing a drawer and the master-detail page structure with state management

Flutter Drawer with State Management

A new Flutter project demonstrating a pattern for managing state between the drawers in the application and using one widget to represent the drawer across multiple application pages

Getting Started

  • install the Provider Package from https://pub.dev/
  • The Provider Package
    • A dependency injection system built with widgets for widgets. provider is mostly syntax sugar for InheritedWidget, to make common use-cases straightforward.
    • Provider Package Documentation

What It Looks Like

Home Page

Drawer Showing Active Page

About Detail Page

Drawer Showing Active Page

Quick Overview of Code

Create a separate widget for the drawer and just use in anywhere you need to.

Manage the Drawer State using a Provider that will keep track of currently selected menu item

this class contains state information and the appropriate methods to modify the state. When state updates happen, the call to notifyListeners is call…

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view the
online documentation, which offers tutorials,
samples, guidance on mobile development, and a full API reference.

Top comments (1)