Mah*_*poo 6 flutter flutter-dialog
我想在处理或离开屏幕之前显示警报对话框,或者最晚显示警告 SnackBar,我该怎么做?
我知道如何显示对话框和 SnackBar,但我不知道我在哪里做,或者我什么时候尝试在处理生活钩子时这样做,但它出错了。由于上下文在显示对话框之前被处理。
Igo*_*din 18
您可以使用WillPopScope小部件:
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
final value = await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
content: Text('Are you sure you want to exit?'),
actions: <Widget>[
FlatButton(
child: Text('No'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
FlatButton(
child: Text('Yes, exit'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
);
}
);
return value == true;
},
child: Scaffold(
appBar: AppBar(),
body: SafeArea(
child: Container()
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2932 次 |
| 最近记录: |