我已经有这个问题很长时间了,经过大量搜索后,我找到了这个完美的解决方案:这个解决方案是 100% 准确的。我希望这有帮助。我们正在使用 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];
},
);
},
),
),
);
}