我有3个StatefulWidget,MainPage,CustomAppbar和ProfileCustomAppbar。在我的MainPage中,我像这样填充抽屉:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: (currentPage == profilePage)
? ProfileCustomAppbar(
height: 60,
)
: CustomAppbar(
height: 60,
),
endDrawer: populateDrawer(),
body: Container(
decoration: BoxDecoration(color: Colors.white),
child: currentPage,
),
),
);
}
populateDrawer() {
return Theme(
data: Theme.of(context).copyWith(
canvasColor:
Colors.white //This will change the drawer background to blue.
//other styles
),
child: Drawer(
child: Column(
children: <Widget>[
DrawerHeader(
child: Center(
child: Image.asset("assets/images/lf_logo.png",
height: 100, width: 100),
),
),
Divider(color: Configuration.PrimaryColor),
],
),
),
);
}
现在我要从ProfileCustomAppbar
打开抽屉,这就是我所说的抽屉:
IconButton(
icon: Icon(Icons.settings, color: Colors.brown,),
onPressed: () {
Scaffold.of(context).openDrawer();
},
)
但是抽屉没有打开,如何修理?
答案 0 :(得分:1)
使用脚手架的钥匙,然后使用该钥匙获取正确的上下文
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
...
然后
IconButton(
icon: Icon(
Icons.settings,
color: Colors.brown,
),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
},
)
答案 1 :(得分:0)
您需要将IconButton小部件包装在用于打开抽屉的Builder小部件内。请参考以下代码:
Builder(
builder: (BuildContext context) {
return new IconButton(
icon: Icon(Icons.category),
onPressed: () {
Scaffold.of(context).openDrawer();
},
);
},
),
答案 2 :(得分:0)
这是我的解决方法,
首先,我在父窗口小部件中创建新功能
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.7.1
loaded via a namespace (and not attached):
[1] magrittr_1.5 tools_3.3.3 yaml_2.2.0 Rcpp_1.0.0 stringi_1.3.2 stringr_1.3.1
然后我在另一个有状态的小部件中调用了函数
openTheDrawer(){
_scaffoldKey.currentState.openEndDrawer();
}
答案 3 :(得分:0)
您可以创建一个回调函数,该函数将调用_scaffoldKey.currentState.openEndDrawer();
_scaffoldKey
所属的位置。然后,将该回调函数传递到要调用它的位置。