Search code examples
flutternavigation-drawerappbar

How can I add a drawer in the app bar in flutter?


I am trying to add a drawer in my appbar but i couldnt manage to fix it. My code looks like this, please help!

Widget appBarMain(BuildContext context) {
  return AppBar(
    centerTitle: false,
    title: Image.asset(
      'assets/images/logox.png',
      height: 45,
    ),
    flexibleSpace: Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: <Color>[Colors.pink[400], Colors.blue[400]]),
      ),
    ),
  );
}

Solution

  • add a drawer with Scaffold:

    return Scaffold(
          appBar: appBarMain(context),
          body: Center(child: Text('My Page!')),
          drawer: Drawer(
            // Add a ListView to the drawer. This ensures the user can scroll
            // through the options in the drawer if there isn't enough vertical
            // space to fit everything.
            child: ListView(
              // Important: Remove any padding from the ListView.
              padding: EdgeInsets.zero,
              children: <Widget>[
                DrawerHeader(
                  child: Text('Drawer Header'),
                  decoration: BoxDecoration(
                    color: Colors.blue,
                  ),
                ),
                ListTile(
                  title: Text('Item 1'),
                  onTap: () {
                    // Update the state of the app
                    // ...
                    // Then close the drawer
                    Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text('Item 2'),
                  onTap: () {
                    // Update the state of the app
                    // ...
                    // Then close the drawer
                    Navigator.pop(context);
                  },
                ),
              ],
            ),
          ),
        );