颤振:处理脚手架上的抽屉状态

时间:2019-05-23 06:25:41

标签: android dart flutter

这是我的Scaffold小部件,我的自定义应用程序栏位于脚手架的主体内,想要从自定义图像打开和关闭抽屉。

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(),
                ),
              ),
            ],
          )
        ],
      ),
    );

这是我的自定义工具栏代码

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')),),
              ],)

            ],));
    }

我的仪表板外观

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须在脚手架上使用GlobalKey

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

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

然后在需要的地方致电openDrawer()

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