我正在使用 Flutter BottomSheet 来显示信息,并且希望即使单击后退按钮也能保持 BottomSheet 始终可见,并且为了使其工作,我已经显式处理了 onWillPop 并保持 BottomSheet 即使在用户单击后退按钮时也处于 View 中、改变路线等
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]()));
有什么想法吗?
最佳答案
由于您只使用一个导航器,Scaffold 会将“BottomSheet”路线推送到与其他路线相同的导航器。所以不可能在两者之间插入一些东西。
我建议用另一个导航器包裹脚手架(仅用于推/弹出底板)。您现在将拥有嵌套的导航器 - 一个位于 MaterialApp 级别,一个位于 Scaffold 级别。从“应用程序”级别导航器中弹出内容 - 这不会触及您的底部工作表。一旦“应用程序”级导航器中没有任何内容,事件将关闭。
我认为这应该有效。
编辑:代码
@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(...),
),
),
);
}
关于Flutter:按后退按钮应该允许应用程序转到后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54397043/