简体   繁体   English

如何检查警报对话框是否在颤振中打开

[英]How to check whether Alert Dialog is open in flutter

I am working on my flutter application and I want to check whether the alert dialog is open or not on the screen .我正在处理我的颤振应用程序,我想检查屏幕上是否打开了警报对话框。 Can anyone tell me how to do that, basically I want to do some stuff just before and after alert dialog opens and closes.谁能告诉我怎么做,基本上我想在警报对话框打开和关闭之前和之后做一些事情。

First thing is you will be showing dialog yourself.第一件事是您将自己显示对话框。 So, you can use a bool value to track it.因此,您可以使用bool值来跟踪它。

Like this.像这样。

bool _isDialogShowing = false;

void _showDialog() {
  _isDialogShowing = true; // set it `true` since dialog is being displayed
  showDialog(
    context: context,
    builder: (context) {
      return AlertDialog(
        title: Text("Title"),
        actions: <Widget>[
          FlatButton(
            child: Text("CANCEL"),
            onPressed: () {
              _isDialogShowing = false; // set it `false` since dialog is closed
              Navigator.of(context).pop();
            },
          )
        ],
      );
    },
  );
}

To listen for back button, you can wrap your root widget in WillPopScope and handle things in onWillPop() accordingly.要监听后退按钮,您可以将根小部件包装在WillPopScope并相应地在onWillPop()处理。

try this !!!试试这个 !!!

Future _dialog;

_checkAndShowDialog() async {

    if (_dialog == null) {
      _dialog = showMyDialog();
      await _dialog;
      _dialog = null;
    } else {
     //do nothing
    }

}

//dialog should return future
Future showMyDialog() {
    return showDialog(
        context: _context,
        child: Container(child: Text("I am dialog"),) );
  }

I had the same problem.我有同样的问题。 Maybe my solution would do any better for someone:也许我的解决方案对某人会更好:

_isOpen = true;
showDialog(
    context: context,
    child: AlertDialog(
      title: Text("Some title!"),
      content: Text("Some content!"),
    )).then((_) => _isOpen = false);

The then will run when the alert closes. then将在警报关闭时运行。

In your case its better to use a full screen dialog then u can create frosted glass effect and in center u can add alert box like container and decorate it.在您的情况下,最好使用全屏对话框,然后您可以创建磨砂玻璃效果,并在中心添加警告框,如容器并对其进行装饰。 To make modal barier like effect wrap outter frosted glass container with inkWell or gesture detector and on tap pop the screen为了使模态像效果一样,用inkWell或手势检测器包裹在磨砂玻璃容器外面,然后点击弹出屏幕

You can assign the alert dialog to a variable and use the build method to populate it.您可以将警报对话框分配给一个变量并使用 build 方法来填充它。 Later check the variable to know whether the dialog is being shown or not.稍后检查变量以了解对话框是否正在显示。

"so I want to add a frosted glass background behind my alert , so what i want to do is before pushing alert dialog i want to change my screen to frosted glass bg and then after poping up alert i want to remove that bg using setState .That's why I want to listen to the alert dialog. Hope this all makes sense to you! Thanks !" “所以我想在我的警报后面添加一个磨砂玻璃背景,所以我想要做的是在推送警报对话框之前我想将我的屏幕更改为磨砂玻璃背景,然后在弹出警告后我想使用 setState 删除该背景。这就是我想听警报对话框的原因。希望这一切对你有意义!谢谢!”

This is what you responded as the purpose of your dialog.这就是您作为对话目的的回应。

You can await a dialog like this:您可以等待这样的对话框:

<do setup code here>

await showDialog(...);

<do final code here>

Regardless of how you show the dialog I don't see a flaw on this.无论您如何显示对话框,我都看不到这方面的缺陷。 If you don't have this on an async function you can always call .then and do stuff there:如果你在async函数上没有这个,你可以随时调用.then并在那里做一些事情:

<do setup code here>

showDialog(...).then((_) => <do final code here>);

you can use this method:你可以使用这个方法:

_isThereCurrentDialogShowing(BuildContext context) =>
  ModalRoute.of(context)?.isCurrent != true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM