gpt4 book ai didi

Flutter 防止在按下后关闭对话框

转载 作者:行者123 更新时间:2023-12-03 13:29:23 33 4
gpt4 key购买 nike

我正在使用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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com