Flutter:按后退按钮应允许该应用进入后台 [英] Flutter: Pushing back button should allow the app to go to background

查看:357
本文介绍了Flutter:按后退按钮应允许该应用进入后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter BottomSheet来显示信息,并且即使单击后退按钮,也希望保持BottomSheet始终可见,并且要使其正常工作,我已经明确处理过onWillPop并即使用户单击后退按钮也可以使BottomSheet处于可见状态,更改路线等

I am using Flutter BottomSheet to display info, and would like to keep BottomSheet always visible even when back button is clicked, and to make it work I have explicitly handled onWillPop and to keep the BottomSheet in view even when user clicks back button, changing the route etc

BottomSheet的高度为200,我想保留它,但是当单击后退按钮时,允许应用程序进入背景状态.

The BottomSheet has a height of 200, and I want to keep it and yet allow the app to go background state when back button is clicked.

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"路由推送到与其他路由相同的导航器.因此不可能在两者之间弹出一些东西.

Since you are using only one Navigator, Scaffold will push the "BottomSheet" route to the same Navigator as other routes. So it would not be possible to pop something in between.

我建议将Scaffold包裹在另一个导航器中(仅用于推动/弹出底页).现在,您将拥有嵌套的导航器-一个在MaterialApp级别,一个在Scaffold级别.从应用"级别的导航器中弹出的内容-不会触及您的最下层表.......................................................................................................................................................................一旦应用"级别的导航器中没有任何内容,活动就会关闭.

I would suggest wrap Scaffold with another Navigator (just for pushing/poping bottom sheet). You will now have nested Navigators - one at MaterialApp level and one at Scaffold level. pop stuff from the "app" level Navigator - that wouldn't touch your bottom sheet. once there is nothing in "app" level Navigator the activity will close.

我认为这应该可行.

有关嵌套导航器的更多信息

代码

@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:按后退按钮应允许该应用进入后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆