【问题标题】:Flutter : CupertinoTabScaffold - Back button close app on Android from TabView's navigationFlutter : CupertinoTabScaffold - 来自 TabView 导航的 Android 上的后退按钮关闭应用程序
【发布时间】:2020-09-20 07:19:49
【问题描述】:

我有一个使用 CupertinoApp-CupertinoTabScaffold 的应用程序。

我的应用的层次结构

CupertinoApp
- CupertinoTabScaffold
-- CupertinoTabView
--- Home
---- Movie list
----- Movie Detail
--- Search
---- Movie Search List
----- Movie Detail
--- Profile
--- Settings

我刚刚意识到,当我在 Android 上单击返回按钮时,即使在电影详细信息中(从任何选项卡),应用程序也会关闭。

返回按钮应该是真正从电影细节返回。

我搜索了 5 天,找不到任何解决方案或解决此问题的方法。

最好的问候,

Utku Y.

【问题讨论】:

标签: flutter flutter-cupertino


【解决方案1】:

我已经有这个问题很长时间了,经过大量搜索后,我找到了这个完美的解决方案:This solution is 100% accurate.我希望这会有所帮助。 我们正在使用 Keys 的帮助,如果您有不明白的地方可以问我,我已经添加了代码。

    ///these are KEYS which are assigned to every Tab,
///the problem of navigation is solved by these KEYS
final GlobalKey<NavigatorState> firstTabNavKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> secondTabNavKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> thirdTabNavKey = GlobalKey<NavigatorState>();

CupertinoTabController tabController;

@override
void initState() {
  // TODO: implement initState
  super.initState();
  tabController = CupertinoTabController(initialIndex: 0);
}

@override
Widget build(BuildContext context) {
  //making a list of the keys
  final listOfKeys = [firstTabNavKey, secondTabNavKey, thirdTabNavKey];

  List homeScreenList = [
    //list of different screens for different tabs
  ];
  return CupertinoApp(
    //this is important
    home: WillPopScope(
      onWillPop: () async {
        return !await listOfKeys[tabController.index].currentState.maybePop();
      },
      child: CupertinoTabScaffold(
        controller: tabController, //set tabController here
        tabBar: CupertinoTabBar(
          items: [
            ///this is where we are setting aur bottom ICONS
            BottomNavigationBarItem(
                label: 'AddClass',
                icon: Icon(CupertinoIcons.add_circled_solid)),
            BottomNavigationBarItem(
                label: 'Profile', icon: Icon(CupertinoIcons.person_solid)),
            BottomNavigationBarItem(
                label: 'Joined', icon: Icon(CupertinoIcons.xmark_circle_fill)),
          ],
          // currentIndex: pageIndex,
        ),
        tabBuilder: (
          context,
          index,
        ) {
          return CupertinoTabView(
            navigatorKey: listOfKeys[
                index], //set navigatorKey here which was initialized before
            builder: (context) {
              return homeScreenList[index];
            },
          );
        },
      ),
    ),
  );
} 

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 谢谢 Pulkit Prajapat,这是没有 pub 的好解决方案。
  • 很高兴知道它有帮助。
  • 即使在实现了这个之后,我的 Android 手机和模拟器还是崩溃了,或者离开了颤振应用程序,将使用后退按钮。远离 CupertinoTabScaffold,后退按钮可以正常工作。
猜你喜欢
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
相关资源
最近更新 更多