I have been having this issue for a lot of time, after lot of searching i find this perfect solution: This solution is 100% accurate. I hope this helps.
We are taking help of Keys and I have added the code if you don't understand something do ask me.
///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];
},
);
},
),
),
);
}