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

Flutter - use WillPopScope with custom dialog to confirm app exit

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

 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() 并获得 return 值。

awesomeDialogs 包装 pop() 函数本身而不对 return 值做任何事情 (awesome_dialog.dart)

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

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

所以你需要自己处理return值,不要再使用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;

是的,你是对的。它应该是这样的:

Future<bool> _promptExit()  async {

    bool canExit;

    AwesomeDialog dlg = AwesomeDialog(
      context: context,
      dialogType: DialogType.QUESTION,
      animType: AnimType.BOTTOMSLIDE,
      title: Strings.prompt_exit_title,
      desc: Strings.prompt_exit_content,
      dismissOnTouchOutside: true,
      btnCancelOnPress: () => canExit = false,
      btnOkOnPress: () => canExit = true,
      btnOkText: Strings.yes,
      btnCancelText: Strings.no
    );

    await dlg.show();
    return Future.value(canExit);
}