如何解除FlatButton点击上的AlertDialog?

时间:2017-05-24 13:28:55

标签: flutter

我有以下AlertDialog

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text("Location disabled"),
              content: const Text(
                  """
Location is disabled on this device. Please enable it and try again.
                  """),
              actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: _dismissDialog,
                ),
              ],
            ),
        );

如何让_dismissDialog()解雇说AlertDialog

16 个答案:

答案 0 :(得分:46)

Navigator.pop()应该做到这一点。您也可以使用它来返回对话框的结果(如果它向用户显示了选项)

答案 1 :(得分:12)

Navigator.of(context, rootNavigator: true).pop('dialog')

和我一起工作。

答案 2 :(得分:6)

在单击平面按钮时关闭警报对话框的示例

RaisedButton(
        onPressed: () {
          showDialog(
              context: context,
              builder: (context) => AlertDialog(
                    title: Text('Are you sure?'),
                    content: Text('Do you want to remove item?'),
                    actions: <Widget>[
                      FlatButton(
                          onPressed: () => Navigator.of(context).pop(false),//  We can return any object from here
                           child: Text('NO')),
                      FlatButton(
                          onPressed: () => Navigator.of(context).pop(true), //  We can return any object from here
                          child: Text('YES'))
                    ],
                  )).then((value) =>
              print('Selected Alert Option: ' + value.toString()));
        },
        child: Text('Show Alert Dialog'),
      ),

以上代码具有两个独特的功能,可用于提供对话框的回调结果

Navigator.of(context).pop(false)-当我们按下时返回false值 否Navigator.of(context).pop(true)-当我们返回true值 按是

基于这些返回值,我们可以在返回值之外执行一些操作或维护对话框状态值

答案 3 :(得分:4)

您可以使用以下任何一种方式:

Navigator.of(context).pop();
Navigator.pop(context);

答案 4 :(得分:2)

Navigator.pop(_)

为我工作,但是Flutter Team的图库包含使用以下示例:

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

这也有效,我很想效仿他们。

答案 5 :(得分:2)

您可以将AlertDialog包装在异步方法中,以使事情整洁。

  _showAlertConfirmDelete() async {
    // the response will store the .pop value (it can be any object you want)
    var response = await showDialog(
        context: context,
        builder: (context) => AlertDialog(
              title: Text('Warn'),
              content: Text('Really wants to remove the record?'),
              actions: <Widget>[
                FlatButton(
                    onPressed: () => Navigator.of(context)
                        .pop(false), 
                    child: Text('No')),
                FlatButton(
                    onPressed: () => Navigator.of(context).pop(true),
                    child: Text('Yes'))
              ],
            ));
    // do you want to do with the response.
    print(response);
  }

答案 6 :(得分:1)

使用Navigator.pop(context);

示例

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text("Location disabled"),
              content: const Text(
                  """
Location is disabled on this device. Please enable it and try again.
                  """),
              actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: () {
                      Navigator.pop(context);
                    },
                ),
              ],
            ),
        );

答案 7 :(得分:1)

Navigator.of(dialogContext).pop(),否则,如果您从“母版”页面导航到“详细信息”页面,则可以关闭页面

                showDialog(
                  context: context,
                  builder: (dialogContext) {
                    return Dialog(
                      child: Column(
                        children: [
                          Text("Content"),
                          RaisedButton(
                            onPressed: () => Navigator.of(dialogContext).pop(),
                            child: Text("Close"),
                          )
                        ],
                      ),
                    );
                  },
                );

答案 8 :(得分:0)

已接受的答案说明了如何使用“导航器类”关闭对话框。要在不使用导航器的情况下关闭对话框,可以将按钮的onPressed事件设置为以下内容:

setState((){
  thisAlertDialog = null; 
});

如果上面的代码不是不言自明的,则基本上是将FlatButton的Parent AlertDialog设置为null,从而将其关闭。

答案 9 :(得分:0)

完全有效

      RaisedButton(
                child: Text(
                  "Cancel",
                  style: TextStyle(color: Colors.white),
                ),
                color: Colors.blue,
                onPressed: () => Navigator.pop(context),
              ),

答案 10 :(得分:0)

如果您要弹出对话框并导航到另一个视图,则此答案有效。 “ current_user_location”部分是路由器需要知道要导航到哪个视图的字符串。

FlatButton(
           child: Text('NO'),
           onPressed: () {
             Navigator.popAndPushNamed(context, 'current_user_location');
              },
           ),

答案 11 :(得分:0)

为“警报对话框”创建单独的上下文会有所帮助。

showDialog(
  context: context,
  builder: (alertContext) => AlertDialog(
    title: const Text("Location disabled"),
    content: const Text(
        """Location is disabled on this device. Please enable it and try again."""),
    actions: [
      new FlatButton(
        child: const Text("Ok"),
        onPressed: () => Navigator.pop(alertContext),
      ),
    ],
  ),
);

答案 12 :(得分:0)

请使用以下代码关闭对话框

RaisedButton(
     onPressed: () { Navigator.of(context).pop();},
     child: Text("Close",style: TextStyle(color: Colors.white), ),
                color: Colors.black,
           )

答案 13 :(得分:0)

在showDialog中传递它 barrierDismissible : true

答案 14 :(得分:0)

这对我有用Navigator.of(context,rootNavigator:true).pop('dialog')。

Navigator.pop()仅关闭当前页面/屏幕。

答案 15 :(得分:0)

通常Navigator.pop(context);有用。

但是,如果应用程序具有多个导航器对象并且dialogBox没有关闭,请尝试

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

如果您想通过结果通话,请尝试

Navigator.pop(context,result);

OR

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