我正在为现有项目增添动力。在颤动模块中我有它包含抽屉的主页。但在背面按drawer
没有关闭。
@override
Widget build(BuildContext context) {
return Scaffold(
body: HomePage(),
drawer: Drawer(),
);
}
给Scaffold
一个GlobalKey
并将你的小部件包裹在WillPopScope
GlobalKey<ScaffoldState> _globalKey = GlobalKey();
...
@override
Widget build(BuildContext context) {
return Scaffold(
key: _globalKey,
drawer: Drawer(),
body: WillPopScope(
child: HomePage(),
onWillPop: () {
if (_globalKey.currentState.isDrawerOpen) {
Navigator.pop(context); // closes the drawer if opened
return Future.value(false); // won't exit the app
} else {
return Future.value(true); // exits the app
}
},
),
);
}