我正在使用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]()));
有什么想法吗?
答案 0 :(得分:1)
由于仅使用一个导航器,所以Scaffold会将“ BottomSheet”路线推向与其他路线相同的导航器。因此不可能在两者之间弹出一些东西。
我建议将Scaffold包裹在另一个导航器中(仅用于推动/弹出底页)。现在,您将拥有嵌套的导航器-一个在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(...),
),
),
);
}
答案 1 :(得分:0)
用 WillPopscope 包裹您的底部工作表小部件
onWillPop: () {
return Future.value(false);
},