How to open drawer on button click in flutter

 Open drawer on button click.


Open drawer on button click. the drawer is used in the scaffold widget. we open the scaffold with slide the screen left to right. sometimes we need to open a scaffold with a button usually we use the button in appBar.

Let's get start


Create a scaffold widget and use a drawer in it.

   
    Scaffold(
      appBar: AppBar(
        title:const Text('Example'),
      ),
      drawer:const Drawer(
        child:
       Text("Drawer")
      ,),
      body: const Center(
        child:  Text(" Slide to open drawer."),
      )
    );
   
   


After this, create a button and open drawer on button click as shown below.

   
    Scaffold(
      appBar: AppBar(
        title:const Text('Example'),
      ),
      drawer:const Drawer(
        child:
       Text("Drawer")
      ,),
      body:  Center(
        child:  Column(
          children: [
            Builder(
              builder: (context) {
                return ElevatedButton(onPressed: (){
                  Scaffold.of(context).openDrawer();
                }, child:const Text("Tap to open drawer"));
              }
            ),
           const Text(" Slide to open drawer."),
          ],
        ),
      )
    );
   


After completing the above steps you can open drawer on button click.



For more information on widgets, packages and solutions dive into the FlutterStuff.

Post a Comment

Previous Post Next Post

Contact Form