在弹出对话框时检测后退按钮的按下情况

时间:2018-11-05 14:45:33

标签: android user-interface dialog flutter flutter-layout

我正在快速创建一个应用程序,需要在其中显示一个警报对话框。这不是可忽略的对话框。但是,当我在android上按返回按钮时,它就被解雇了。我已经尝试过使用WillPopScope小部件来检测按下事件。我可以使用WillPopScope来检测后退按钮的按下,但是在对话框打开时这无法正常工作。任何建议和指南都会非常有帮助。谢谢。

对话框创建摘要:

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

4 个答案:

答案 0 :(得分:10)

返回按钮不会关闭对话框。

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

答案 1 :(得分:0)

三种方法来阻止对话框被Android后退按钮关闭

选项一:

           onWillPop: () {
                          return Future.value(false);
                        },

选项二:

    onWillPop: () async {
                          return false;
                        },

选项三:

 onWillPop: () {}, // This will give surpress warning, try to avoid this one.

答案 2 :(得分:0)

由于我的声誉不足以对接受的答案发表评论,因此我想为onPressed: () {}提供其他选择。您可以使用onPressed: () => null。没有警告会弹出。

答案 3 :(得分:0)

要实现此行为,您可以覆盖 MaterialPageRoute 中的 hasScopedWillPopCallback getter。

class CustomMaterialPageRoute extends MaterialPageRoute {
  @protected
  bool get hasScopedWillPopCallback {
    return false;
  }
  CustomMaterialPageRoute({
    @required WidgetBuilder builder,
    RouteSettings settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
  }) : super(
          builder: builder,
          settings: settings,
          maintainState: maintainState,
          fullscreenDialog: fullscreenDialog,
        );
}

然后在您的路由器中替换 MaterialPageRoute 上的 CustomMaterialPageRoute。 然后在 iOS 上滑动即可使用,WillPopScope 小部件也可以使用。