我在Flutter 中创建应用程序,需要在其中显示警告对话框.而且这不是一个不可忽视的对话.但当我在Android上按下后退按钮时,它被关闭了.我已经try 使用WillPopScope小部件来检测背压事件.我可以使用WillPopScope检测到按下Back按钮,但在对话框打开时这不起作用.任何建议和指南都会很有帮助.谢谢.

对话框创建代码段:

void buildMaterialDialog(
  String dialogTitle,
  String dialogContent,
  String negativeBtnText,
  String positiveBtnText,
  String positiveTextUri) {

showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return new AlertDialog(
        title: new Text(dialogTitle),
        content: new Text(dialogContent),
        actions: <Widget>[
          new FlatButton(
            onPressed: () {
              //Function called
              _updateDialogNegBtnClicked(isCancelable);
            },
            child: new Text(negativeBtnText),
          ),
          new FlatButton(
            onPressed: () => launch(positiveTextUri),
            child: new Text(positiveBtnText),
          ),
        ],
      );
    });}

推荐答案

后退按钮不会关闭对话框.

showDialog(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: AlertDialog(
        title: Text('Title'),
        content: Text('This is Demo'),
        actions: <Widget>[
          FlatButton(
            onPressed: () => Navigator.pop(context),
            child: Text('Go Back'),
          ),
        ],
      ),
    );
  },
);

Flutter相关问答推荐

Flutter -如何从布局填充(对称水平)中排除小部件?

使用Flutter 提供程序包加载状态应用程序时,在自定义按钮中显示CircularProgressIndicator

文本未设置在集装箱摆动的中心

Flutter 块消费者仅对特定的状态属性更改做出react

如何将WebViewWidget和WillPopScope结合起来

Flutter如何将2个以上的值传递到ListView上的另一个屏幕?

如何在flutter中使用youtube_explod_start加载下一页

尽管海拔设置为 0,如何删除 appBar 下的下划线

hooks_riverpod 有用途吗

如何从列表中更新provider变量:Flutter列表和Provider优化指南

Riverpod 2.3.6:AutoDisposeNotifier和ref.onDispose自动处理

在我使用 flutter_riverpod stateprovider 重新保存代码之前,UI 主题状态不会更新

Flutter:如何在 bottomsheet 中创建一个日期 Select 器,如下图所示

面对Flutter 中的行和列问题

Flutterfire Configure - 未处理的异常和没有 firebase 初始化

如何在向下滚动时隐藏顶部卡片并在向上滚动时出现?

如何从 Firebase `get()` 获取 `DocumentReference`

Flutter 错误:正文可能正常完成,导致返回null

Flutter如何从左到右连续 Select 多个列表项而不跳过任何

在 Flutter 中单击时切换按钮 colored颜色 没有改变