Flutter: possible to detect when a drawer is open?

后端 未结 4 1697
Happy的楠姐
Happy的楠姐 2021-01-04 13:53

Is it possible to detect when a Drawer is open so that we can run some routine to update its content?

A typical use case I have would be to display the number of fol

4条回答
  •  Happy的楠姐
    2021-01-04 14:33

    I think one simple solution is to override the leading property of your AppBar so you can have access when the menu icon is pressed an run your API calls based on that.

    Yet I may have misunderstood your question because with the use case you provided, you usually need to manage it in a way that you can listen to any change which will update the value automatically so I am not sure what are you trying to trigger when the drawer is open.

    Anyway here is the example.

    class DrawerExample extends StatefulWidget {
      @override
      _DrawerExampleState createState() => new _DrawerExampleState();
    }
    
    class _DrawerExampleState extends State {
      GlobalKey _key = new GlobalKey();
      int _counter =0;
      _handleDrawer(){
          _key.currentState.openDrawer();
    
               setState(() {
              ///DO MY API CALLS
              _counter++;
            });
    
      }
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          key: _key,
          appBar: new AppBar(
            title: new Text("Drawer Example"),
            centerTitle: true,
            leading: new IconButton(icon: new Icon(
              Icons.menu
            ),onPressed:_handleDrawer,),
          ),
          drawer: new Drawer(
            child: new Center(
              child: new Text(_counter.toString(),style: Theme.of(context).textTheme.display1,),
            ),
          ),
        );
      }
    }
    

提交回复
热议问题