PersistentBottomNavBar
PersistentBottomNavBar copied to clipboard
Pop all screen when switching back to a tab
This is my current configuration. I would like to have the following flow:
- Render first TabItem initial screen;
- Push and render new screen inside the first TabItem with this script:
onPressed: () => pushNewScreen(
context,
screen: MyGroupsScreen(),
),
- Click on second TabItem and render a initial screen
- Return to first TabItem by clicking on the tab and render the initial screen;
What is happening at the moment is that the initial screen is not rendering when returning to a previous TabItem. Am I configuring something wrong? What should i do here?
return PersistentTabView(
context,
controller: _controller,
screens: _buildScreens(),
items: _navBarsItems(),
confineInSafeArea: true,
backgroundColor: Colors.white,
handleAndroidBackButtonPress: true,
resizeToAvoidBottomInset: true,
stateManagement: true,
hideNavigationBarWhenKeyboardShows: true,
decoration: NavBarDecoration(
colorBehindNavBar: Colors.white,
border: Border(
top: BorderSide(color: ColorPalette.veryLightBlue, width: 1.0),
),
),
padding: NavBarPadding.all(0.0),
popAllScreensOnTapOfSelectedTab: true,
popActionScreens: PopActionScreensType.all,
itemAnimationProperties: ItemAnimationProperties(
duration: Duration(milliseconds: 200),
curve: Curves.ease,
),
screenTransitionAnimation: ScreenTransitionAnimation(
animateTabTransition: true,
curve: Curves.ease,
duration: Duration(milliseconds: 200),
),
navBarStyle: NavBarStyle.style3,
);
I had the same issue as you do.
popAllScreensOnTapOfSelectedTab only pops the screen if an already selected tab is pressed/tapped again. All the screens pushed on that particular tab will pop until the first screen in the stack. popActionScreens does not seem to work for me either, I am also not sure what an ActionScreen is supposed to be...
I found a solution that works for me, adding the following function in the PersistsentTabBarView:
selectedTabScreenContext: (context) {
if (context != null) {
WidgetsBinding.instance?.addPostFrameCallback((_) {
Navigator.of(context)
.popUntil((route) => route.isFirst);
});
}
}
If you only want to pop specific tabs to the root you can achieve this by checking for the tabIndex:
selectedTabScreenContext: (context) {
if (context != null) {
PersistentTabController _controller =
Provider.of<PersistentTabController>(context,
listen: false);
if (_controller.index == 0)
WidgetsBinding.instance?.addPostFrameCallback((_) {
Navigator.of(context)
.popUntil((route) => route.isFirst);
});
}
},