gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:18:01 24 4
gpt4 key购买 nike

当用户点击 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com