我的第一个 flutter 应用程序中有一个材质设计的导航抽屉。这项工作很好,但我没有找到任何方法来关闭后退按钮按下时的导航抽屉,如果它在使用时打开WillPopScope以显示AlertDialog. AlertDialog当回按时,应用程序只是显示而不是关闭抽屉。如果已经打开,我希望抽屉应该关闭,AlertDialog否则显示。
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: onBackPressed,
child: Scaffold(
appBar: AppBar(
title: Text("Home"),
),
drawer: Drawer(
),
body: Center(
child: Text("Home"),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
onBackPressed显示关闭应用程序的对话框。
Future<bool> onBackPressed() {
return showDialog(
barrierDismissible: false,
context: context,
builder: (context) => AlertDialog(
title: Text("Do you want to exit?"),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context, false),
child: Text("No")),
FlatButton(
onPressed: () => Navigator.pop(context, true),
child: Text("Yes"))
],
));
} …Run Code Online (Sandbox Code Playgroud)