Flutter - 使用 WillPopScope 和自定义对话框来确认应用程序退出

用户1209216

我已经通过这种方式实现了应用程序退出确认:

 return WillPopScope(
      onWillPop: _promptExit,
      child: Container() /*Remaining window layout*/

_promptExit 函数显示对话框以确认退出:

return showDialog(
      context: context,
      builder: (context) => new AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20.0))),
        title: new Text(Strings.prompt_exit_title),
        content: new Text(Strings.prompt_exit_content),
        actions: <Widget>[
          FlatButton(
            child: new Text(Strings.no),
            onPressed: () => Navigator.of(context).pop(false),
          ),
          SizedBox(height: 16),
          FlatButton(
            child: new Text(Strings.yes),
            onPressed: () => Navigator.of(context).pop(true),
          ),
        ],
      ),
    ) ??
        false;

它有效,但我想使用自定义对话框而不是标准对话框:https : //github.com/marcos930807/awesomeDialogs

它在showDialog内部调用,所以我尝试了这种方式:

 AwesomeDialog dlg = AwesomeDialog(
      context: context,
      dialogType: DialogType.QUESTION,
      animType: AnimType.BOTTOMSLIDE,
      title: Strings.prompt_exit_title,
      desc: Strings.prompt_exit_content,
      dismissOnBackKeyPress: false,
      useRootNavigator: true,
      btnCancelOnPress: () {  Navigator.of(context).pop(false);},
      btnOkOnPress: () {   Navigator.of(context).pop(true);},
      btnOkText: Strings.yes,
      btnCancelText: Strings.no
    );

    return dlg.show() ?? false;

但是我不能以这种方式弹出,我正在黑屏。我应该怎么办?

黄灰色

showDialog不会弹出对话框本身。所以你需要使用Navigator.of(context).pop()并获取返回值。

awesomeDialogs包装 pop() 函数本身并对返回值不做任何处理(awesome_dialog.dart

   ...
    pressEvent: () {
      dissmiss();
      btnOkOnPress?.call();
    },
   ...
    pressEvent: () {
      dissmiss();
      btnCancelOnPress?.call();
    },
   ...

dissmiss() {
  if (!isDissmisedBySystem) Navigator.of(context, rootNavigator:useRootNavigator)?.pop();
}
...

所以需要自己处理返回值,不要再次使用pop():

var result;

AwesomeDialog dlg = AwesomeDialog(
  context: context,
  dialogType: DialogType.QUESTION,
  animType: AnimType.BOTTOMSLIDE,
  title: Strings.prompt_exit_title,
  desc: Strings.prompt_exit_content,
  dismissOnBackKeyPress: false,
  useRootNavigator: true,
  btnCancelOnPress: () {result = false;},
  btnOkOnPress: () {result = true;},
  btnOkText: Strings.yes,
  btnCancelText: Strings.no
);

await dlg.show();

return result;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Flutter Bloc 表单状态和 WillPopScope

在Flutter中将嵌套导航器与WillPopScope一起使用

我想使用我的代码将willpopscope代码退出后退按钮应用程序

在 Flutter 中,当将整个应用程序包裹在 WillPopScope 中时,不会调用 onWillPop

如何使用FlutterFragment和自定义入口点将Flutter布局嵌入Android应用程序中?

在Android Studio中使用Flutter应用程序使用自定义代码片段(实时模板)

使用setRouteLeaveHook通过自定义对话框确认导航

如何在自定义的Laravel Nova工具中使用确认对话框?

如何使用自定义确认对话框通过 dropzone 删除多个上传的文件?

Flutter自定义动画对话框

维克斯安装程序 - 到位致命错误对话框中调整大小致命错误对话框或使用自定义对话框

Rails 5.1:如何在rails-ujs中覆盖allowAction以使用自定义确认对话框

使用jQueryUI对话框的Knockoutjs自定义bindig

使用Python创建SPSS自定义对话框

使用自定义逻辑Vuetify打开对话框

在自定义Android对话框中使用ConstraintLayout

如何在 Flutter 中使用自定义应用栏和图像创建 SliverAppBar?

我应该使用什么来代替android api少20中的自定义对话框

Flutter:使用自定义 ErrorWidget

Flutter-使用Flutter创建自定义控件

Flutter-自定义警报对话框未显示

如何在Flutter中显示自定义吐司对话框?

为local_auth Flutter添加自定义对话框

为什么在使用 Navigator.pop 时会忽略 WillPopScope 中的 onWillPop?

WillPopScope我应该在Navigator.pop之后使用return Future.value(true)吗?

如何使用Flutter从错误对话框导航

在退出应用程序之前弹出 Flutter 显示确认

如何使用Flutter和mysql构建聊天应用程序?

使用 pagewise 包和 JSON 的 Flutter 应用程序