Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何按按钮打开颤振中的AlertDialog?_Flutter_Dart_Dialog_Alert - Fatal编程技术网

Flutter 如何按按钮打开颤振中的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

按下按钮后,我想打开一个AlertDialog,但前提是变量
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