【问题标题】:How to dismiss an AlertDialog on a FlatButton click?如何在 FlatButton 单击时关闭 AlertDialog?
【发布时间】:2017-10-24 21:11:25
【问题描述】:

我有以下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

【问题讨论】:

    标签: android ios flutter flutter-alertdialog


    【解决方案1】:

    这工作for me Navigator.of(context, rootNavigator: true).pop('dialog')

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

    【讨论】:

      【解决方案2】:

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

      但是如果应用程序有多个 Navigator 对象并且dialogBox 没有关闭,那么试试这个

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

      如果你想通过结果调用,试试

      Navigator.pop(context,result);
      

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

      【讨论】:

      • 这是最好的答案
      【解决方案3】:

      这足以在任何回调中关闭对话框添加,例如
      onpressed,ontap

      Navigator.of(context).pop();
      

          AlertDialog(
                title: Center(child: Text("$title")),
                insetPadding: EdgeInsets.zero,
                titlePadding: EdgeInsets.only(top: 14.0, bottom: 4),
                content: Container(
                  height: 50,
                  child: TextFormField(
                    controller: find_controller,
                    decoration: InputDecoration(
                      suffixIcon: context.watch<MediaProvider>().isChangeDialog
                          ? IconButton(
                              onPressed: () {
                                clearController(find_controller);
                              },
                              icon: Icon(Icons.clear))
                          : null,
                      border: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.deepPurpleAccent)),
                      hintText: 'Id',
                    ),
                    onChanged: (val) {
                      if (val.isNotEmpty)
                        context.read<MediaProvider>().isChangeDialog = true;
                      else
                        context.read<MediaProvider>().isChangeDialog = false;
                    },
                  ),
                ),
                actions: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(4.0),
                        child: OutlinedButton(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Align(
                                  child: Padding(
                                    padding: const EdgeInsets.symmetric(horizontal: 12.0),
                                    child: Icon(Icons.clear),
                                  ),
                                ),
                                Text("Cancel")
                              ],
                            ),
                            onPressed: () {
                              context.read<MediaProvider>().isChangeDialog = false;
      //========================this enough to dismisss dialog
                              Navigator.of(context).pop();
                            }),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(4.0),
                        child: ElevatedButton(
                            onPressed: context.watch<MediaProvider>().isChangeDialog
                                ? () {
                                    context.read<MediaProvider>().isChangeDialog = false;
                                    okCallback;
                                  }
                                : null,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Align(
                                  child: Padding(
                                    padding: const EdgeInsets.symmetric(horizontal: 12.0),
                                    child: Icon(Icons.check),
                                  ),
                                ),
                                Text("OK")
                              ],
                            )),
                      )
                    ],
                  ),
                ],
              );
      

      【讨论】:

        【解决方案4】:

        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"),
                                  )
                                ],
                              ),
                            );
                          },
                        );
        

        【讨论】:

          【解决方案5】:

          在 showDialog 中传递它 barrierDismissible : true

          【讨论】:

            【解决方案6】:

            您可以将 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);
              }
            

            【讨论】:

              【解决方案7】:

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

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

              【讨论】:

                【解决方案8】:

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

                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),
                      ),
                    ],
                  ),
                );
                

                【讨论】:

                  【解决方案9】:

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

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

                  【讨论】:

                    【解决方案10】:

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

                    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 值 NO Navigator.of(context).pop(true) -- 当我们返回真值时 按是

                    根据这些返回值,我们可以在它之外进行一些操作或者维护对话状态值

                    【讨论】:

                    • pop(false) 会做什么? pop(true) 会做什么?无论如何,在这两种情况下,我们都希望解除 AlertDialog。
                    • @user3410835:修改代码,请看
                    【解决方案11】:

                    如果您不想返回任何结果,请使用其中任何一个:

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

                    但是如果你确实想返回一些结果,请看这个

                    例子:

                    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
                      }
                    });
                    

                    【讨论】:

                    • 这两行代码有什么区别??
                    • @user3410835 没区别,其实Navigator.pop()调用的是第一行。
                    • 如何使 AlertDialog 可关闭 = false ?因此,当我单击对话框外的屏幕时,不会关闭该对话框。
                    • @user3410835 showDialog() 有一个名为 barrierDismissible 的属性,您可以将其设置为 false 或 true。
                    【解决方案12】:

                    使用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);
                                        },
                                    ),
                                  ],
                                ),
                            );
                    

                    【讨论】:

                      【解决方案13】:

                      这很好用

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

                      【讨论】:

                        【解决方案14】:

                        接受的答案说明了如何使用 Navigator 类关闭对话框。要在不使用 Navigator 的情况下关闭对话框,您可以将按钮的 onPressed 事件设置为:

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

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

                        【讨论】:

                          【解决方案15】:
                          Navigator.pop(_)
                          

                          为我工作,但 Flutter 团队的图库包含一个使用示例:

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

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

                          【讨论】:

                          • 我从另一个 .dart 文件调用自定义 AlertDialog 并使用 Navigator.of(context, rootNavigator: true).pop();工作谢谢。
                          • 我一直使用第一个版本...但只是遇到了第二个版本的示例,但第一个版本删除了它下面的屏幕。
                          • 为我工作谢谢:)
                          【解决方案16】:
                          Navigator.of(context, rootNavigator: true).pop('dialog')
                          

                          和我一起工作。

                          【讨论】:

                          • 接受的答案导致我的整个页面消失,这是隐藏对话框的正确答案
                          • 这是关闭对话框的更好方法,我正在尝试上述解决方案,但它弹出了我的另一个视图。
                          • 接受的答案导致我的页面也消失了,这是隐藏对话框的正确答案。
                          • 答案仍然会导致整个视图弹出。
                          • 什么是 rootNavigator ?
                          【解决方案17】:

                          Navigator.pop() 应该可以解决问题。您还可以使用它来返回对话框的结果(如果它为用户提供了选择)

                          【讨论】:

                          • 谢谢你,成功了。调用 Navigator.pop() 按预期关闭对话框。我目前的onPressed如下:onPressed: () =&gt; Navigator.pop(context),
                          • @Collin,我创建了一个函数来显示另一个函数的对话框。 void showLoader(context) { showDialog( context: context, builder: (BuildContext context) { return Container( width: double.infinity, height: double.infinity, decoration: BoxDecoration( color: Colors.black.withOpacity(0.05), ) , child: Center(child: Text('hello friends'), ), ); }, );请建议我如何隐藏此显示对话框。谢谢。
                          • 也为我工作!
                          猜你喜欢
                          • 1970-01-01
                          • 2020-07-02
                          • 2014-02-23
                          • 2019-08-19
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2012-05-04
                          • 2020-07-16
                          相关资源
                          最近更新 更多