Search code examples
flutterflutter-layoutdrawerlayout

Drawer not show up at SliverAppBar


I want to add a Drawer inside my SliverAppBar my code doesn't have any error inside but when I run the code the drawer does not show up.

Here's my code

body: CustomScrollView(
    slivers: <Widget>[
      SliverLayoutBuilder(
        builder: (BuildContext context, constraints) {
          final scrolled = constraints.scrollOffset > 50;
          final scrolledicon = constraints.scrollOffset > 0;

          return SliverAppBar(
            expandedHeight: 20,
            toolbarHeight: 65,
            leading: Padding(
              padding: const EdgeInsets.only(top: 2),
              child: Icon(DBIcons.logo, color: Colors.red, size: 50),
            ),
            actions: [
              Padding(
                padding: const EdgeInsets.all(10),
                child: IconButton(
                  icon: Icon(Icons.menu),
                  color: scrolledicon ? Colors.white : Colors.black,
                  onPressed: (){
                    Drawer(
                      child: Center(
                        child: Text('This is Drawer'),
                      ),
                    );
                  },
                ),
              ),
            ],
            backgroundColor: scrolled ? Colors.black : Colors.transparent,
            elevation: 0,
            pinned: true,
          );
        },
      ),

Solution

  • Please have a look here.

    In your Scaffold, you have to register a drawer

    Scaffold(
        ...
        drawer: Drawer(child: Center(
                        child: Text('This is Drawer'),
        ),
        ...
      );
    

    With onPressed you send the request to open the Drawer to the Scaffold:

    onPressed: () => Scaffold.of(context).openDrawer(),