Flutter:按下后退按钮应该允许应用程序进入后台
Flutter: Pushing back button should allow the app to go to background
我正在使用 Flutter BottomSheet 来显示信息,并且希望 BottomSheet 始终可见,即使在单击后退按钮时也是如此,为了使其正常工作,我已经明确处理了 onWillPop 并且即使在用户单击时也使 BottomSheet 保持可见后退按钮、更改路线等
BottomSheet 的高度为 200,我想保留它并允许应用程序在单击后退按钮时进入后台状态。
Widget _buildBody(context) => WillPopScope(
onWillPop: () async {
if(navigatorKey.currentState.canPop()) {
navigatorKey.currentState.pop();
return false;
}else {
// Returning true will remove the BottomSheet when back button is pressed, and if you press the back button one more time, the app will go to background state
// return true;
}
},
child: MaterialApp(
navigatorKey: navigatorKey,
onGenerateRoute: (route) => pagesRouteFactories[route.name]()));
有什么想法吗?
由于您只使用一个 Navigator,Scaffold 会将 "BottomSheet" 路由推送到与其他路由相同的 Navigator。所以不可能在两者之间弹出一些东西。
我建议用另一个 Navigator 包装 Scaffold(仅用于 pushing/poping 底部 sheet)。您现在将拥有嵌套的导航器 - 一个在 MaterialApp 级别,一个在 Scaffold 级别。从 "app" 级导航器弹出内容 - 不会触及您的底部 sheet。一旦 "app" 级导航器中没有任何内容,activity 将关闭。
我认为这应该可行。
编辑:代码
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: ()async{
if(Navigator.of(context).canPop()) {
Navigator.of(context).pop();
return false;
}
return true;
},
child: Navigator(
onGenerateRoute: (route) => MaterialPageRoute(
builder: (context) => Scaffold(...),
),
),
);
}
用 WillPopscope
包裹你的底部 sheet 小部件
onWillPop: () {
return Future.value(false);
},
我正在使用 Flutter BottomSheet 来显示信息,并且希望 BottomSheet 始终可见,即使在单击后退按钮时也是如此,为了使其正常工作,我已经明确处理了 onWillPop 并且即使在用户单击时也使 BottomSheet 保持可见后退按钮、更改路线等
BottomSheet 的高度为 200,我想保留它并允许应用程序在单击后退按钮时进入后台状态。
Widget _buildBody(context) => WillPopScope(
onWillPop: () async {
if(navigatorKey.currentState.canPop()) {
navigatorKey.currentState.pop();
return false;
}else {
// Returning true will remove the BottomSheet when back button is pressed, and if you press the back button one more time, the app will go to background state
// return true;
}
},
child: MaterialApp(
navigatorKey: navigatorKey,
onGenerateRoute: (route) => pagesRouteFactories[route.name]()));
有什么想法吗?
由于您只使用一个 Navigator,Scaffold 会将 "BottomSheet" 路由推送到与其他路由相同的 Navigator。所以不可能在两者之间弹出一些东西。
我建议用另一个 Navigator 包装 Scaffold(仅用于 pushing/poping 底部 sheet)。您现在将拥有嵌套的导航器 - 一个在 MaterialApp 级别,一个在 Scaffold 级别。从 "app" 级导航器弹出内容 - 不会触及您的底部 sheet。一旦 "app" 级导航器中没有任何内容,activity 将关闭。
我认为这应该可行。
编辑:代码
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: ()async{
if(Navigator.of(context).canPop()) {
Navigator.of(context).pop();
return false;
}
return true;
},
child: Navigator(
onGenerateRoute: (route) => MaterialPageRoute(
builder: (context) => Scaffold(...),
),
),
);
}
用 WillPopscope
包裹你的底部 sheet 小部件onWillPop: () {
return Future.value(false);
},