android - 在 flutter 中打开对话框时检测返回按钮按下

我正在创建一个应用程序,我需要在其中显示一个警报对话框。这不是一个可忽略的对话。但是当我按下 android 上的后退按钮时,它会被关闭。我尝试使用 WillPopScope 小部件来检测回压事件。我能够使用 WillPopScope 检测后退按钮按下,但这在对话框打开时不起作用。任何建议和指导都会非常有帮助。谢谢。

对话框创建 fragment :

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

https://stackoverflow.com/questions/53156666/

相关文章:

firebase - Flutter 中从 Firestore 查询单个文档(cloud_fires

dart - 错误 : The name 'Image' is defined in the lib

ios - 使用 Swift 创建 Flutter 项目

flutter - 如何在不同的屏幕尺寸上测试 Flutter 小部件?

dart - Flutter Firestore 服务器端时间戳

android-studio - 无法将 Flutter 项目迁移到 AndroidX

dart - flutter 位置固定等效

dart - Flutter BottomNavigationBar 不能处理三个以上的项目

flutter - 在 float 操作按钮上的 onPressed 回调中从脚手架显示 snack

android - flutter 意外退出