如何在 Flutter 中关闭警报对话框?

Sim*_*ani 5 popup dart flutter

我正在为我的应用程序中的弹出窗口使用警报对话框。当触发 onTap 时,弹出窗口会被调用,但是当我按下“取消”按钮时,弹出窗口不会关闭,并且弹出窗口后面的整个屏幕会变黑。这是我的弹出窗口代码。

import 'package:flutter/material.dart';

class FancyAlertDialog {

  static showFancyAlertDialog(
    BuildContext context,
    String title,
    String message,
     {
    bool dismissable = true,
    Icon icon,
    @required String labelPositiveButton,
    @required String labelNegativeButton,
    @required VoidCallback onTapPositiveButton,
    @required VoidCallback onTapNegativeButton,
  }) {
    assert(context != null, 'context is null!!!');
    assert(title != null, 'title is null!!!');
    assert(message != null, 'message is null!!!');
    assert(labelPositiveButton != null, 'labelPositiveButton is null');
    assert(labelNegativeButton != null, 'labelNegativeButton is null');
    assert(onTapPositiveButton != null, 'onTapPositiveButton is null');
    assert(onTapNegativeButton != null, 'onTapNegativeButton is null');
    return showDialog(
      context: context,
      barrierDismissible: dismissable,
      child: Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(4.0),
          ),
        ),
        child: Wrap(
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(4.0),
                  topRight: Radius.circular(4.0),
                ),
                color:Colors.red,
              ),
              padding: EdgeInsets.symmetric(vertical: 5.0),
              child: Stack(
                children: <Widget>[
                  Align(
                    child: icon ?? Container(height:0),
                    alignment: Alignment.topRight,
                  )
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(
                left: 16.0,
                top: 2.0,
                right: 16.0,
                bottom: 8.0,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Center(
                    child: Text(
                      title,
                      style: Theme.of(context).textTheme.subtitle,
                    ),
                  ),
                  SizedBox(height: 8.0),
                  Text(
                    message,
                    textAlign: TextAlign.center,
                    style: Theme.of(context).textTheme.caption,
                  ),
                  SizedBox(height: 16.0),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: RaisedButton(
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                              Radius.circular(16.0),
                            ),
                          ),
                          color: Colors.grey,
                          child: Text(
                            labelNegativeButton.toUpperCase(),
                            style: TextStyle(
                              color: Colors.white,
                            ),
                          ),
                          onPressed: onTapNegativeButton,
                        ),
                      ),
                      SizedBox(width: 16.0),
                      Expanded(
                        child: RaisedButton(
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                              Radius.circular(16.0),
                            ),
                          ),
                          color: Colors.red,
                          child: Text(
                            labelPositiveButton.toUpperCase(),
                            style: TextStyle(
                              color: Colors.white,
                            ),
                          ),
                          onPressed: onTapPositiveButton,
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

这就是我调用弹出窗口的方式。

 FancyAlertDialog.showFancyAlertDialog(
              context,
              'Info Fancy Alert Dialog Box',
              'This is a info alert dialog box. This plugin is used to help you easily create fancy dialog',
              icon: Icon(
                Icons.clear,
                color: Colors.black,
              ),
              labelPositiveButton: 'OKAY',
              onTapPositiveButton: () {
                Navigator.pop(context);
                print('tap positive button');
              },
              labelNegativeButton: 'Cancel',
              onTapNegativeButton: () {
                Navigator.pop(context);
                print('tap negative button');
              },
            );
Run Code Online (Sandbox Code Playgroud)

这是当我按下取消按钮时屏幕的样子: 在此输入图像描述

小智 7

在 onTap() 中尝试一下:

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


dum*_*azy 5

context我假设您在调用时使用了错误的对象Navigator.pop(context)。此时导航器还不知道该对话框。

首先,提供一个新BuildContextshowDialog. 有两种方法可以做到这一点:

  • child为函数中的参数 (now Dialog)创建一个新的小部件showDialog
  • child( Dialog) 包裹起来,Builder提供新的BuildContext

那么你应该contextNavigator.pop(context)通话中获得新的信息。同样,有两种方法可以做到这一点:

  • 从对话框本身弹出
  • 将上下文对象作为参数传递给onTapPositiveButtonandonTapNegativeButton

有关的更多信息Builder也可以在这里找到:https://www.youtube.com/watch ?v=xXNOkIuSYuA