flutter - 在 Flutter 的 CupertinoTabView 中强制重新加载页面

标签 flutter flutter-cupertino cupertinotabbar flutter-bottomnavigation

当用户点击 CupertinoTabView 中的项目时,我想每次都重新加载页面。现在,它只是第一次加载。下一次它只显示以前的状态。我想强制重新加载它。 例如,当用户点击 pageThree 时,它​​是下面代码中 switch case 中的“case 2”,我想重新加载该页面。

这是我的代码:

class HomeScreen extends StatelessWidget {

  int currentIndex = 0;
  final GlobalKey<NavigatorState> pageOneTabNavKey = GlobalKey<NavigatorState>();
  final GlobalKey<NavigatorState> pageTwoTabNavKey = GlobalKey<NavigatorState>();
  final GlobalKey<NavigatorState> pageThreeTabNavKey = GlobalKey<NavigatorState>();
  final GlobalKey<NavigatorState> pageFourTabNavKey = GlobalKey<NavigatorState>();
  final GlobalKey<NavigatorState> pageFiveTabNavKey = GlobalKey<NavigatorState>();
  @override
  Widget build(BuildContext context) {
    return CupertinoTabScaffold(
        tabBar: CupertinoTabBar(
            activeColor: AppColor.pumpkinOrange,
            inactiveColor: Colors.white,
            backgroundColor: AppColor.peacockBlue,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.home)
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.notifications_none),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.star_border),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.person),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.help_outline),
              )
            ],
          onTap: (index) {

              switch (index) {
                case 0:
                  pageOneTabNavKey.currentState.popUntil((r) => r.isFirst);
                  break;
                case 1:
                  pageTwoTabNavKey.currentState.popUntil((r) => r.isFirst);
                  break;
                case 2:
                  pageThreeTabNavKey.currentState.popUntil((r) => r.isFirst);
                  break;
                case 3:
                  pageFourTabNavKey.currentState.popUntil((r) => r.isFirst);
                  break;
                case 4:
                  pageFiveTabNavKey.currentState.popUntil((r) => r.isFirst);
                  break;
              }

            currentIndex = index;

          },
        ),
        tabBuilder: (BuildContext context, int index) {
          return CupertinoTabView(builder: (BuildContext context) {
            switch (index) {
              case 0:
                return CupertinoTabView(
                  navigatorKey: summaryTabNavKey,
                  builder: (BuildContext context) {
                    return MultiBlocProvider(
                      providers: <BlocProvider>[
                        BlocProvider<PageOneBloc>(
                            create: (BuildContext context) =>
                            PageOneBloc(FetchDataRepository())..add(LoadingEvent())
                        ),
                        BlocProvider<DataBloc>(
                            create: (BuildContext context) => DataBloc()
                        )
                      ],
                      child: PageOne(),
                    );
                  },
                );
                break;
              case 1:
                return CupertinoTabView(
                  navigatorKey: pageTwoTabNavKey,
                  builder: (BuildContext context) {
                    return MultiBlocProvider(
                      providers: <BlocProvider>[
                        BlocProvider<PageTwoBloc>(
                          create: (BuildContext context) =>
                          PageTwoBloc(PageTwoManager())
                            ..add(LoadPageTwoEvent()),
                        ),
                        BlocProvider<ScrolleventnotifierBloc>(
                          create: (BuildContext context) =>
                              ScrolleventnotifierBloc(),
                        ),
                      ],
                      child: PageTwo(),
                    );
                  },
                );
                break;
              case 2:
                return CupertinoTabView(
                  navigatorKey: pageThreeTabNavKey,
                  builder: (BuildContext context) {
                    return BlocProvider<PageThreeBloc>(
                      create: (BuildContext context) =>
                      PageThreeBloc()..add(UserReachesPageThreeEvent(noOfItems:5,pageNumber: 1)),
                      child: PageThree(),
                    );
                  },
                );
                break;
              case 3:
                return CupertinoTabView(
                  navigatorKey: pageFourTabNavKey,
                  builder: (BuildContext context) {
                    return BlocProvider<ProfileBloc>(
                      create: (BuildContext context) =>
                      ProfileBloc()..add(UserReachesProfilePageEvent()),
                      child: ProfilePage(),
                    );
                  },
                );
                break;
              case 4:
                return CupertinoTabView(
                  navigatorKey: helpTabNavKey,
                  builder: (BuildContext context) {
                    return BlocProvider<HelpBloc>(
                      create: (BuildContext context) =>
                      HelpBloc(FetchHelpRepository())..add(HelpLoadEvent()),
                      child: HelpPage(),
                    );
                  },
                );
                break;
            }
            return Container();
          });
        });
  }
}

最佳答案

我不确定您给出的示例中有什么问题,但我已将小部件更改为 StatefulWidget,它似乎可以正常工作。 For the currentState on GlobalKey to not be null - widget must be Stateful .

这是您的代码,略有改动 - https://codepen.io/pintom/pen/JjYVxbe

class _HomeScreenState extends State<HomeScreen> {
  final GlobalKey<NavigatorState> pageOneTabNavKey =
      GlobalKey<NavigatorState>();

  final GlobalKey<NavigatorState> pageTwoTabNavKey =
      GlobalKey<NavigatorState>();

  int currentIndex;

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      home: CupertinoTabScaffold(
        tabBar: CupertinoTabBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.notifications_none),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.star_border),
            ),
          ],
          onTap: (index) {
//            print('current i = $currentIndex, clicked = $index');

            if (index == currentIndex) {
              switch (index) {
                case 0:
                  pageOneTabNavKey.currentState.popUntil((r) => r.isFirst);
                  break;
                case 1:
                  pageTwoTabNavKey.currentState.popUntil((r) => r.isFirst);
                  break;
              }
            }

            currentIndex = index;
          },
        ),
        tabBuilder: (BuildContext context, int index) {
          currentIndex = index;

          return CupertinoTabView(
            builder: (BuildContext context) {
              switch (index) {
                case 0:
                  return CupertinoTabView(
                    navigatorKey: pageOneTabNavKey,
                    builder: (BuildContext context) {
                      return TabPage(index);
                    },
                  );
                  break;
                case 1:
                  return CupertinoTabView(
                    navigatorKey: pageTwoTabNavKey,
                    builder: (BuildContext context) {
                      return TabPage(index);
                    },
                  );
                  break;
                default:
                  return Container();
              }
            },
          );
        },
      ),
    );
  }
}

关于flutter - 在 Flutter 的 CupertinoTabView 中强制重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61421963/

相关文章:

flutter - 如何在不需要打开应用程序的情况下显示对话框

ios - 使用 ThemeData 作为 CupertinoApp 的主题

flutter - 如何在 Flutter 中使用大标题导航栏?

flutter - 从 CupertinoTabScaffold 导航出来

flutter - 如何在 Flutter 中正确绑定(bind)有状态小部件中的回调函数?

flutter - 在Flutter中异步加载图片

api - Flutter FutureBuilder 不断被调用

flutter - 在 CupertinoPicker 和 CupertinoDatePicker 中通过 Tap 选择值

flutter - 显示键盘时如何隐藏cupertinoTabBar?或者如何让键盘盖住酒吧?或者始终将标签栏保持在底部?