简体   繁体   中英

Flutter: Handle drawer state from Scaffold body

This is my Scaffold Widget and My custom app bar is inside the body of scaffold and wants to open and close drawer from custom image.

return Scaffold(
      key: _scaffoldKey,
      drawer: Drawer(
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: <Widget>[
            ListTile(
              title: Text('Item 1'),
              onTap: () {
              },
            ),
          ],
        ),
      ),
      body: Stack(
        children: <Widget>[
          Container(
            decoration: _buildBackground(),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Expanded(
                flex: 13,
                child:MyDrawerWidget(),  // this is my toolbar code and want to handle state of drawer from here
              ),
              Expanded(
                flex: 87,
                child: Container(
                  child: DashBoardContainer(),
                ),
              ),
            ],
          )
        ],
      ),
    );

This is my Custom Toolbar code

Widget MyDrawerWidget(){
      return Container(
          padding: EdgeInsets.only(top:width*0.05),
          decoration: BoxDecoration(
              color: MyColors.greenHeader,
              borderRadius: new BorderRadius.only(
                  bottomLeft: const Radius.circular(15.0),
                  bottomRight: const Radius.circular(15.0))
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[

              Row(children: <Widget>[
                Container(
                  width: 50,
                  padding: EdgeInsets.only(top:width*0.02,left:width*0.02,right:width*0.02,bottom: width*0.02),
                  margin: EdgeInsets.only(left:width*0.02,right:width*0.02),
                  child: FlatButton(
                      onPressed: (){
                        _scaffoldKey.currentState.openDrawer();
                      },
                      padding: EdgeInsets.all(0.0),
                      child: Image.asset('assets/images/menu.png')),),

                Container(
                  height: 35,
                  decoration: BoxDecoration(color: Colors.white),
                  padding: EdgeInsets.only(left: width*0.03,right: width*0.03),
                  child:  DropdownButtonHideUnderline(
                    child: DropdownButton(
                      value: _currentFeedType,
                      items: _dropDownFeedItems,
                      onChanged: changedDropDownFeedItems,
                    ),
                  ),
                ),

                Container(
                  height: 40,
                  padding: EdgeInsets.only(left: width*0.03,right: width*0.01),
                  child:  Center(child: Text('Feed',style: TextStyle(color: Colors.white,fontSize: 18.0),),),
                ),
              ],),


              Row(children: <Widget>[

                Container(
                  width: 50,
                  padding: EdgeInsets.only(top:width*0.04,left:width*0.04,right:width*0.02,bottom: width*0.04),
                  margin: EdgeInsets.only(left:width*0.05,right:width*0.00),
                  child: FlatButton(
                      onPressed: (){
                        Navigator.of(context).pop();
                      },
                      padding: EdgeInsets.all(0.0),
                      child: Image.asset('assets/images/msg.png')),),

                Container(
                  width: 50,
                  padding: EdgeInsets.only(top:width*0.04,left:width*0.04,right:width*0.02,bottom: width*0.04),
                  margin: EdgeInsets.only(left:width*0.00,right:width*0.02),
                  child: FlatButton(
                      onPressed: (){
                        Navigator.of(context).pop();
                      },
                      padding: EdgeInsets.all(0.0),
                      child: Image.asset('assets/images/search.png')),),
              ],)

            ],));
    }

How my dashboard looks like

在此输入图像描述

You have to use a GlobalKey on Scaffold:

  final GlobalKey<YourWidgetClass> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      drawer: Drawer(),
    );
  }

And call openDrawer() on where you need:

onPressed: () => _scaffoldKey.currentState.openDrawer())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM