我有一个应用程序需要从屏幕 1 移动到屏幕 2 ......现在当用户按下后退按钮时......它应该显示一个对话框......如果用户按下是它必须退出......任何帮助?上层解决方案不起作用
似乎您可以使用WillPopScope。您还需要传递一个回调函数,该函数将指示按下后退按钮时会发生什么。在您的情况下,您可以添加代码以显示AlertDialog,该代码将要求用户确认退出。
您可以简单地将 Scaffold 包装在 WillPopScope 中。
例子:
Widget build(BuildContext context) {
return WillPopScope(
child: /*Your scaffold widget*/
onWillPop: () {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Confirm Exit"),
content: Text("Are you sure you want to exit?"),
actions: <Widget>[
FlatButton(
child: Text("YES"),
onPressed: () {
SystemNavigator.pop();
},
),
FlatButton(
child: Text("NO"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
}
);
return Future.value(true);
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6384 次 |
| 最近记录: |