gpt4 book ai didi

dart - 在 Flutter 中按后退键关闭抽屉

转载 作者:行者123 更新时间:2023-12-03 03:17:21 25 4
gpt4 key购买 nike

我正在为现有项目添加 flutter。在 flutter 模块中,我有主页,它包含抽屉。但是在后面按 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
}
},
),
);
}

关于dart - 在 Flutter 中按后退键关闭抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55124167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com