我以这样的方式实现了app退出确认:
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;但我不能这样弹,我的屏幕变黑了。我该怎么办?
发布于 2021-02-04 11:09:00
是的,你是对的。所以它应该是这样的:
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);
}发布于 2021-02-04 11:01:36
showDialog本身不会弹出对话框。因此,您需要使用Navigator.of(context).pop()并获得返回值。
awesomeDialogs包装pop()函数本身,不对返回值(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;https://stackoverflow.com/questions/66043613
复制相似问题