android - Flutter:从 Scaffold 主体处理抽屉状态
问题描述
这是我的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')),),
],)
],));
}
我的仪表板的外观
解决方案
您必须GlobalKey
在脚手架上使用 a:
final GlobalKey<YourWidgetClass> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
drawer: Drawer(),
);
}
并openDrawer()
在您需要的地方打电话:
onPressed: () => _scaffoldKey.currentState.openDrawer())
推荐阅读
- javascript - 为什么这个函数不能返回给定数组的所有排列?
- php - Google Calendar API:创建活动时无法设置AllowedConferenceSolutionTypes 以包括hangoutsMeet
- php - 电子 PHP | 构建后的空白窗口
- c++ - 我怎样才能绕道断言?
- android - SharedElementTransition 没有使用 Glide 正确地动画图像缩放
- python - 为什么我的服务器没有回显或显示消息?
- c++ - 库是否包含在预处理器或链接中
- google-apps-script - 从 Google 表格将数据矩阵读入脚本函数
- apache-kafka - 擦除所有 Kafka 数据?
- paypal - PayPal Payflow Recurring Billing:重新激活个人资料