Flutter 如何按按钮打开颤振中的AlertDialog?
按下按钮后,我想打开一个AlertDialog,但前提是变量Flutter 如何按按钮打开颤振中的AlertDialog?,flutter,dart,dialog,alert,Flutter,Dart,Dialog,Alert,按下按钮后,我想打开一个AlertDialog,但前提是变量bool showAlert为true 这是我到目前为止的代码: FlatButton( child: Text("HIER"), onPressed: () { return AlertDialog( title: Text("HI"), content: Te
bool showAlert
为true
这是我到目前为止的代码:
FlatButton(
child: Text("HIER"),
onPressed: () {
return AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(child: Text("Yes"), onPressed: () {},),
FlatButton(child: Text("No"), onPressed: () {},)
],
elevation: 24,
);
},
),
对于我的问题(如果bool为true,则打开警报),问题是,警报对话框没有打开
有什么解决办法吗?感谢要显示AlertDialog,您需要一个showDialog,因此代码结果如下:
FlatButton(
child: Text("HIER"),
onPressed: () {
if(showAlert){
showDialog(
//if set to true allow to close popup by tapping out of the popup
barrierDismissible: false,
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(child: Text("Yes"), onPressed: () {},),
FlatButton(child: Text("No"), onPressed: () {},)
],
elevation: 24,
),
);
}
},
),
使用以下代码显示本机diolog依赖于平台:
FlatButton(
child: Text("HIER"),
onPressed: () {
if (Platform.isIOS) {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
CupertinoDialogAction(
child: Text("Yes"),
onPressed: () {},
),
CupertinoDialogAction(
child: Text("No"),
onPressed: () {},
)
],
);
},
);
} else {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(
child: Text("Yes"),
onPressed: () {},
),
FlatButton(
child: Text("No"),
onPressed: () {},
)
],
elevation: 24,
);
},
);
}
},
);
查看此代码,希望它能帮助您
openDialog(bool showAlert, BuildContext context) async {
if(!showAlert) return;
var result = await showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("HI"),
content: Text("Are you there?"),
actions: [
FlatButton(child: Text("Yes"), onPressed: () {
Navigator.pop(context, true);
},),
FlatButton(child: Text("No"), onPressed: () {
Navigator.pop(context, false);
},)
],
elevation: 24,
),
);
if(!(result ?? false)) {
// Yes click
} else {
// No click
}
}
另类
GetX包Get.dialog()
以及许多其他脚手架组件。
对话框、Bottosheet、Snackbar、stateManagentgetx和obxbuilder等等。
访问pub.dev并搜索GetX