【问题标题】:How to open an AlertDialog in Flutter by Button-Press?如何通过 Button-Press 在 Flutter 中打开 AlertDialog?
【发布时间】:2021-03-04 18:23:14
【问题描述】:

按下按钮后,我想打开一个 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 没有打开。

有什么解决办法吗?谢谢

【问题讨论】:

    标签: flutter dart dialog alert


    【解决方案1】:

    要显示 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,
                ),
              );
            }
          },
        ),
    

    【讨论】:

      【解决方案2】:

      使用此代码显示原生 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,
                    );
                  },
                );
              }
            },
          );
      

      【讨论】:

        【解决方案3】:

        看看这段代码,希望对你有帮助

        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
        }
        
        }
        

        【讨论】:

          【解决方案4】:

          另类 GetX 包Get.dialog() 对于许多其他脚手架组件。 Dialog、Bottosheet、Snackbar、StateManagent Getx 和 Obx builder 等等。 访问 pub.dev 并搜索 GetX

          【讨论】:

            猜你喜欢
            • 2020-03-05
            • 2014-09-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-28
            • 2019-05-19
            • 2019-06-25
            相关资源
            最近更新 更多