我正在使用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: () {
},
),
],
);
},
);
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它不关闭?
Ade*_*los 16
您需要WillPopScope像这样将 AlertDialon 括起来:
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: () {
},
),
],
)
)
},
);
Run Code Online (Sandbox Code Playgroud)
在WillPopScope为您提供一个onWillPop参数,在那里你可以通过你想要什么时,孩子流行的功能。在这种情况下,参数接收一个空函数,因此它不会弹出。
Car*_*yes 14
您需要使用一个WillPopScope. 它将使用 on 函数onWillPop来确定对话框是否关闭。在这种情况下始终为 false,因此用户无法使用后退按钮关闭对话框。
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return WillPopScope(
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: () {
Navigator.of(context).pop();
},
),
],
),
onWillPop: () async {
return false;
},
);
},
);
Run Code Online (Sandbox Code Playgroud)
您也可以通过设置onWillPop: () => Future.value(false)inside 来完成WillPopScope。现在onWillPop:(){}正如接受的答案中提到的那样给出警告。
警告消息: 此函数的返回类型为“Future”,但不以 return 语句结尾。尝试添加 return 语句,或将返回类型更改为“void”。
所以,使用onWillPop: () => Future.value(false)oronWillPop: () async {return false;},代替。
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return WillPopScope(
child: AlertDialog(
................
),
onWillPop: () => Future.value(false),
);
},
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3820 次 |
| 最近记录: |