简体   繁体   English

如何关闭 flutter 对话框?

[英]How to dismiss flutter dialog?

I am new to flutter, I want to dismiss my dialog after the task completion.我是 flutter 的新手,我想在任务完成后关闭我的对话框。 I've tried with:我试过:

Navigator.pop(context, true); 

But my screen is getting black and dialog is still up there.但是我的屏幕变黑了,对话仍然存在。 here is my dialog code.这是我的对话代码。

Dialog _dialog = new Dialog(
  child: new Row(
    mainAxisSize: MainAsixSize.min, 
    children: <Widget> [
    new CircularProgressIndicator(), 
    new Text("Loading")]),     

); 

https://docs.flutter.io/flutter/material/showDialog.html says https://docs.flutter.io/flutter/material/showDialog.html

The dialog route created by this method is pushed to the root navigator.此方法创建的对话路由被推送到根导航器。 If the application has multiple Navigator objects, it may be necessary to call Navigator.of(context, rootNavigator: true).pop(result) to close the dialog rather just Navigator.pop(context, result) .如果应用程序有多个 Navigator 对象,则可能需要调用Navigator.of(context, rootNavigator: true).pop(result)来关闭对话框,而不仅仅是Navigator.pop(context, result)

so I'd assume one of these two should do what you want.所以我假设这两个中的一个应该做你想要的。

This code works for me:这段代码对我有用:

  BuildContext dialogContext;
  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      dialogContext = context;
      return Dialog(
        child: new Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            new CircularProgressIndicator(),
            new Text("Loading"),
          ],
        ),
      );
    },
  );

  await _longOperation();
  Navigator.pop(dialogContext);

If you don't want to return any result after showDialog is closed, you can use如果您不想在showDialog关闭后返回任何结果,可以使用

Navigator.pop(context);

If you want to pass result call如果你想通过结果调用

Navigator.pop(context, result);

Example:例子:

showDialog(
    context: context,
    builder: (_) {
      return AlertDialog(
        title: Text('Wanna Exit?'),
        actions: [
          FlatButton(
            onPressed: () => Navigator.pop(context, false), // passing false
            child: Text('No'),
          ),
          FlatButton(
            onPressed: () => Navigator.pop(context, true), // passing true
            child: Text('Yes'),
          ),
        ],
      );
    }).then((exit) {
  if (exit == null) return;

  if (exit) {
    // user pressed Yes button
  } else {
    // user pressed No button
  }
});
//it work conrrectly
onPressed: () {
   Navigator.of(context, rootNavigator: true).pop();
},

Generally Navigator.pop(context);一般Navigator.pop(context); works.作品。

But If the application has multiple Navigator objects and dialogBox doesn't close, then try this但是如果应用程序有多个 Navigator 对象并且对话框没有关闭,那么试试这个

Navigator.of(context, rootNavigator: true).pop();

If you want to pass result call, try如果您想通过结果调用,请尝试

Navigator.pop(context,result);

OR或者

Navigator.of(context, rootNavigator: true).pop(result);

If you don't want to return any result after showDialog is closed, you can use it.如果你不想在 showDialog 关闭后返回任何结果,你可以使用它。

Navigator.pop(context);

If you want to pass the result call.如果要传递结果调用。

Navigator.pop(context, result);

An example will have a code snippet like the below:一个示例将具有如下所示的代码片段:

showDialog(
    context: context,
    builder: (_) {
      return AlertDialog(
        title: Text('Wanna Exit?'),
        actions: [
          FlatButton(
            onPressed: () => Navigator.pop(context, false), // passing false
            child: Text('No'),
          ),
          FlatButton(
            onPressed: () => Navigator.pop(context, true), // passing true
            child: Text('Yes'),
          ),
        ],
      );
    }).then((exit) {
  if (exit == null) return;
  if (exit) {
    // user pressed Yes button
  } else {
    // user pressed No button
  }
});

这将关闭对话框/警报框

Navigator.of(context).pop();

Better to use Completer, because if your operation is too short or the device is too slow, then the dialogContext variable will not be initialized and you can't close the dialog.最好使用 Completer,因为如果你的操作太短或者设备太慢,那么 dialogContext 变量将不会被初始化,你无法关闭对话框。

final dialogContextCompleter = Completer<BuildContext>();
showDialog<void>(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext dialogContext) {
    if(!dialogContextCompleter.isCompleted) {
      dialogContextCompleter.complete(dialogContext);
    }
    return AlertDialog(
      content: CircularProgressIndicator(),
    );
  },
);

// Close progress dialog
final dialogContext = await dialogContextCompleter.future;
Navigator.pop(dialogContext);
showDialog(
  context: context,
  builder: (BuildContext context) => Center(
    child: CircularProgressIndicator(),
  ),
);

await Future<int>.delayed(Duration(seconds: 3));

Navigator.of(context, rootNavigator: true).pop();

Add on to Günter's answers.添加到 Günter 的答案。 If you need to dismiss it when the user clicks elsewhere change this property to true如果您需要在用户单击其他地方时关闭它,请将此属性更改为 true

barrierDismissible: true,

Try this.尝试这个。 It will definitely work.它肯定会起作用。 use the context of pageBuilder in Navigator to pop the dialog from screen.使用 Navigator 中 pageBuilder 的上下文从屏幕弹出对话框。

showGeneralDialog(
     context: context,
     barrierLabel: "XYZ",
     barrierDismissible: true,
     barrierColor: Colors.black.withOpacity(0.7),
     pageBuilder: (dialogContext, __, ___) {
       return Center(
        child: Container(
          height: 240,
          margin: const EdgeInsets.symmetric(horizontal: 15),
          decoration: bd,
          child: SizedBox.expand(
              GestureDetector(
                   onTap: (){
                       Navigator.pop(dialogContext);
                    },
                    child: const Padding(
                       padding: EdgeInsets.fromLTRB(10,0,10,0),
                       child: Icon(Icons.close, size: 26, color:Colors.white),
                     ),
               ),
           )
         ) 
       );
     }
  );

 

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

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