Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the drawer is closed on flutter?

Tags:

flutter

dart

As title. It since that we can detect the drawer is opened, but is this possible to check it is closed or not? Thanks.

like image 484
DNB5brims Avatar asked Sep 17 '18 15:09

DNB5brims


People also ask

How do you close the drawer in Flutter?

You can do this by using the Navigator . When a user opens the drawer, Flutter adds the drawer to the navigation stack. Therefore, to close the drawer, call Navigator. pop(context) .

How do you close the scaffolds drawer after an item is tapped?

Navigator. pop() will pop the Drawer route off the stack and cause it to close.

How do you use drawer without AppBar Flutter?

You have your own custom Menu button to open/close drawer. You don't want to use AppBar as well. In that case you can use GlobalKey<ScaffoldState>() object to open Drawer. Save this answer.

How do you pop a drawer Flutter?

The navigation drawer is added using the Drawer widget. It can be opened via swipe gesture or by clicking on the menu icon in the app bar. Typically, the navigation drawer opens up from the left side of the screen, but you can also configure it to open from the right side (for the LTR text settings).


2 Answers

I have added this feature in Flutter 2.0.0. Make sure you are using Flutter SDK version >= 2.0.0 to use this.

Simply use a callback in Scaffold

return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      drawer: NavDrawer(),
      onDrawerChanged: (isOpen) {
        // write your callback implementation here
        print('drawer callback isOpen=$isOpen');
      },
      endDrawer: NavDrawerEnd(),
      onEndDrawerChanged: (isOpen) {
        // write your callback implementation here
        print('end drawer callback isOpen=$isOpen');
      },
      body:
      ...

Pull request merged in 2.0.0: https://github.com/flutter/flutter/pull/67249

Happy coding!

like image 187
bikram Avatar answered Sep 28 '22 01:09

bikram


Declare a GlobalKey to reference your drawer:

GlobalKey _drawerKey = GlobalKey();

Put the key in your Drawer:

 drawer: Drawer(
                  key: _drawerKey,

Check if your drawer is visible:

 final RenderBox box = _drawerKey.currentContext?.findRenderObject();
 if (box != null){
    //is visible
 } else {
   //not visible  
} 
like image 28
diegoveloper Avatar answered Sep 28 '22 02:09

diegoveloper