我正在使用showDialog函数来构建一个对话框,但是我需要避免当用户按下后退按钮时,对话框没有关闭,这是我的代码:
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Hello"),
content: new SingleChildScrollView(
child: Container(),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
},
),
],
);
},
);
我怎样才能让它不关闭?
最佳答案
您需要将 AlertDialon 附在 WillPopScope
上像这样:
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return WillPopScope(
onWillPop: (){},
child:AlertDialog(
title: new Text("Hello"),
content: new SingleChildScrollView(
child: Container(),),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
},
),
],
)
)
},
);
WillPopScope
为您提供 onWillPop
参数,您可以在其中传递 child 弹出时所需的函数。在这种情况下,参数接收一个空函数,因此它不会弹出。
关于Flutter 防止在按下后关闭对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59755211/