PersistentBottomNavBar icon indicating copy to clipboard operation
PersistentBottomNavBar copied to clipboard

onPressed option: What is the default action of PersistentBottomNavBarItem?

Open jbt-cii opened this issue 4 years ago • 3 comments

Hello,

I would like to call a function when an item is clicked. I saw that PersistentBottomNavBarItem has a specific option named "onPressed" to manage such a thing. My problem is that I don't really find how to keep the "default" behavior of the PersistentBottomNavBarItem. I saw that I can call several Future in order to push a screen:

          pushNewScreenWithRouteSettings(
            globalContext!,
            settings: RouteSettings(name: '/LogOut'),
            screen: LogOut(),
            pageTransitionAnimation: PageTransitionAnimation.cupertino,
            withNavBar: true,
          );

          pushDynamicScreen(
            globalContext!,
            screen: getPageRoute(PageTransitionAnimation.cupertino, enterPage: LogOut()),
            withNavBar: true,
          );

          pushNewScreen(
            globalContext!,
            screen: LogOut(),
            withNavBar: true,
          );

Reading somes issues I understood how to keep the navBar visible: https://github.com/BilalShahid13/PersistentBottomNavBar/issues/103 https://github.com/BilalShahid13/PersistentBottomNavBar/issues/179

But during my tests I only suceeded to have almost what I want with the "pushNewScreen" approach. With "pushNewScreenWithRouteSettings" or "pushDynamicScreen" I obtained strange behavior => clicking several time on the different items, they replaced the screen wanted for some items. But with "pushNewScreen" that works only the first time we click on the item. If I click on another item and click again on this item it does not work anymore. Also this item does not keep the behavior of the style I use: NavBarStyle.style3

So I try to understand how to keep the default behavior "PersistentBottomNavBarItem" to try to reproduce it in the "onPressed" part.

In fact my real problem is that the navigation bar and its items are loaded once. Then when we click on an item we cannot add an action (for instance I try to manage a timout since the last action on the app).

How can I simply do that with "PersistentBottomNavBar" (I want to keep the "navBar" and the effect given by the "style3")?

Thanks a lot for any help :-)

jbt-cii avatar Sep 09 '21 08:09 jbt-cii

I found a solution! Not at the level of the "PersistentBottomNavBarItem" but at the level of the "PersistentTabView" declaration. I added an action in the "selectedTabScreenContext" part:

  @override
  Widget build(BuildContext context) {
    return PersistentTabView(
      context,
      controller: _controller,
      screens: _buildScreens(),
      items: _navBarsItems(),
...
      selectedTabScreenContext: (context) {
        globalContext = context;
        
        WidgetsBinding.instance!.addPostFrameCallback((_) {
          mySpecificAction();
        });
      },
    );
  }

jbt-cii avatar Sep 09 '21 08:09 jbt-cii

I still keep this issue open to see if someone can explain me this: What is the default behavior "PersistentBottomNavBarItem" to try to reproduce it in the "onPressed" part?

jbt-cii avatar Sep 09 '21 08:09 jbt-cii

do you happens to know,

on tap of the bottomNavBarItem could return which item is tapped?

ie: if i have 4 tab, i want to get the index of which one is tapped and execute certain method in case of tab #3 is tapped

vontdeux avatar Oct 01 '21 03:10 vontdeux