如何通过按下按钮在 Flutter 中打开 AlertDialog?

How to open an AlertDialog in Flutter by Button-Press?

按下按钮后,我想打开一个 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,
            ),
          );
        }
      },
    ),

使用此代码显示本机对话取决于平台:

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() 以及许多其他脚手架组件。 Dialog、Bottosheet、Snackbar、StateManagent Getx 和 Obx 生成器等等。 访问 pub.dev 并搜索 GetX