flutter - 如何通过 Button-Press 在 Flutter 中打开 AlertDialog?
问题描述
按下按钮后,我想打开一个 AlertDialog,但前提是变量bool showAlert
为真。
到目前为止,这是我的代码:
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 为真则打开警报),问题是,AlertDialog 没有打开。
有什么解决办法吗?谢谢
解决方案
要显示 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,
),
);
}
},
),
推荐阅读
- git - 如何在此编辑器中输入 Git 提交消息?
- javascript - 如何更改邮件文件夹的顺序?
- haskell - 如何通知 GHC 特定模式匹配是不可能的(例如:列表永远不会为空)?
- google-apps-script - 为全域安装发布附加组件
- css - 如何从 JavaFX 按钮中删除圆角
- twitter-bootstrap - 引导卡体按钮横跨屏幕如何设置宽度换行内容
- python - 点云与python中另一个点云的最佳拟合
- typescript - 达到超时后调用另一个函数
- python - 为什么我在读取数据框时会得到 dtype 'object'?
- aggregate-functions - Tableau - 基于平均值的排名平均值