mt-akar / bottom_nav_layout

A quick and powerful Flutter layout with a bottom navbar.
https://pub.dev/packages/bottom_nav_layout
MIT License
35 stars 11 forks source link

Change Tab programmatically #9

Closed deezaster closed 2 years ago

deezaster commented 2 years ago

First of all thank you very much for this brilliant package!

Now my question: How can I programmatically select a screen? Example: Tab1 (ArticleScreen) is active. When I receive a push message, I want to go to Tab4 (FormationenScreen). Is this possible? Previously I have used named routes and get there. But with tabs I do not know the solution.

:
  return BottomNavLayout(
    pages: [
      (navKey) => NavigatorWithoutRouteNames(
            navKey: navKey,
            initialPage: ArticleScreen(animationController: animationController),
          ),
      (navKey) => NavigatorWithoutRouteNames(
            navKey: navKey,
            initialPage: StageScreen(animationController: animationController),
          ),
      (navKey) => NavigatorWithoutRouteNames(
            navKey: navKey,
            initialPage: GuuggemusigScreen(animationController: animationController),
          ),
      (navKey) => NavigatorWithoutRouteNames(
            navKey: navKey,
            initialPage: FormationenScreen(animationController: animationController),
          ),
      // (navKey) => ArticleScreen(animationController: animationController),
    ],
    bottomNavigationBar: (int currentIndex, Function(int) onTap) => SalomonBottomBar(
      currentIndex: currentIndex,
      onTap: (int index) {
        animationController.reverse().then<dynamic>((data) {
          onTap(index);
          animationController.forward();
        });
      },
      margin: const EdgeInsets.only(left: 8.0, right: 8.0, top: 16.0, bottom: 20.0),
      items: [
        SalomonBottomBarItem(icon: const Icon(LineIcons.bullhorn), title: const Text('News'), selectedColor: AppTheme.bluevereinigte),
        SalomonBottomBarItem(icon: const Icon(LineIcons.music), title: const Text('Guuggerbühnen'), selectedColor: AppTheme.bluevereinigte),
        SalomonBottomBarItem(icon: const Icon(LineIcons.drum), title: const Text('Guuggemusigen'), selectedColor: AppTheme.bluevereinigte),
        SalomonBottomBarItem(icon: const Icon(LineIcons.users), title: const Text('Formationen'), selectedColor: AppTheme.bluevereinigte),
      ],
    ),
    // If savePageState is false, pages cannot be transitioned out.
    savePageState: true,
  );
:
mt-akar commented 2 years ago

Thank you for the compliments. I appreciate it. And Sorry for the late reply.

You can totally change the page programmatically. It is explained right here on the readme.

  1. You create a page stack. var myPageStack = ReorderToFrontPageStack(initialPage: 0);

  2. Set that page stack to the layout. pageStack: myPageStack,

  3. Push to the page stack myPageStack.push(2);