我有 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();
},
)
但是抽屉打不开,怎么办?
最佳答案
您需要将 IconButton 小部件包装在 Builder 小部件中以打开抽屉。引用以下代码:
Builder(
builder: (BuildContext context) {
return new IconButton(
icon: Icon(Icons.category),
onPressed: () {
Scaffold.of(context).openDrawer();
},
);
},
),
关于flutter - 从不同的有状态小部件打开抽屉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57249369/