答案 0 :(得分:3)
这里有一个基本样本:
@override
Widget build(BuildContext context) {
return Scaffold(
endDrawer: Drawer(),
appBar: AppBar(),
body: Builder(
builder: (context) {
return Center(
child: RaisedButton(
child: Text("open drawer"),
onPressed: () {
Scaffold.of(context).openEndDrawer();
},
),
);
},
),
);
}
或者您可以使用GlobalKey
中的ScaffoldState
:
GlobalKey<ScaffoldState> _key = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _key,
endDrawer: Drawer(),
appBar: AppBar(),
body: Center(
child: RaisedButton(
child: Text("open drawer"),
onPressed: () {
_key.currentState.openEndDrawer();
},
),
),
);
}
}
查看此链接以获取更多信息:https://api.flutter.dev/flutter/material/ScaffoldState/openEndDrawer.html
答案 1 :(得分:0)
创建一个脚手架钥匙,然后将其提供给您的脚手架小部件
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
然后,打开抽屉
_scaffoldKey.currentState.openDrawer()
或
_scaffoldKey.currentState.openEndDrawer()