3

当用户点击 中的项目时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();
          });
        });
  }
}
4

1 回答 1

0

我不确定您给定的示例中有什么问题,但我已将小部件更改为 StatefulWidget 并且它似乎正在工作。为了使 GlobalKey 上的 currentState 不为空 - 小部件必须是有状态的。

这是您的代码,略有改动 - 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();
              }
            },
          );
        },
      ),
    );
  }
}
于 2020-05-26T09:22:34.423 回答