在颤动中打开对话框时检测后退按钮按下
Detect back button press while dialog is open in flutter
我正在快速创建一个应用程序,我需要在其中显示一个警告对话框。这不是一个可忽略的对话框。但是当我按下 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),
),
],
);
});}
后退按钮不会关闭对话框。
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'),
),
],
),
);
},
);
停止通过 Android 后退按钮关闭对话框的三种方法
选项一:
onWillPop: () {
return Future.value(false);
},
选项二:
onWillPop: () async {
return false;
},
选项三:
onWillPop: () {}, // This will give surpress warning, try to avoid this one.
因为我的声誉不足以评论已接受的答案,我想为 onPressed: () {}
提供其他选择。您可以使用 onPressed: () => null
。不会弹出警告。
要实现此行为,您可以覆盖 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,
);
}
然后替换路由器中 CustomMaterialPageRoute
上的 MaterialPageRoute
。
然后在 iOS 上滑动即可工作,WillPopScope 小部件也将工作。
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onBackPressed,
child: Scaffold(
),
);
}
Future<bool> _onBackPressed() async {
return await showDialog(
context: context, builder: (context) => ExitAppDialogBox());
}
我正在快速创建一个应用程序,我需要在其中显示一个警告对话框。这不是一个可忽略的对话框。但是当我按下 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),
),
],
);
});}
后退按钮不会关闭对话框。
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'),
),
],
),
);
},
);
停止通过 Android 后退按钮关闭对话框的三种方法
选项一:
onWillPop: () {
return Future.value(false);
},
选项二:
onWillPop: () async {
return false;
},
选项三:
onWillPop: () {}, // This will give surpress warning, try to avoid this one.
因为我的声誉不足以评论已接受的答案,我想为 onPressed: () {}
提供其他选择。您可以使用 onPressed: () => null
。不会弹出警告。
要实现此行为,您可以覆盖 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,
);
}
然后替换路由器中 CustomMaterialPageRoute
上的 MaterialPageRoute
。
然后在 iOS 上滑动即可工作,WillPopScope 小部件也将工作。
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onBackPressed,
child: Scaffold(
),
);
}
Future<bool> _onBackPressed() async {
return await showDialog(
context: context, builder: (context) => ExitAppDialogBox());
}