Flutter: possible to detect when a drawer is open?

后端 未结 4 1680
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条回答
  •  时光说笑
    2021-01-04 14:56

    Best solution

    ScaffoldState has a useful method isDrawerOpen which provides the status of open/close.

    Example: Here on the back press, it first checks if the drawer is open, if yes then first it will close before exit.

    /// create a key for the scaffold in order to access it later.
    GlobalKey _scaffoldKey = GlobalKey();
    
    @override
    Widget build(context) {
       return WillPopScope(
      child: Scaffold(
        // assign key (important)
        key: _scaffoldKey,
        drawer: SideNavigation(),
      onWillPop: () async {
        // drawer is open then first close it
        if (_scaffoldKey.currentState.isDrawerOpen) {
          Navigator.of(context).pop();
          return false;
        }
        // we can now close the app.
        return true;
      });}
    

提交回复
热议问题