0
votes

I have this problem. My home page has a bottomNavigationBar with 2 pages A and B. Pages A and B have buttons that when tapped navigate to other pages which don't have bottom Navigation Bars. Now, when I am in these other pages that don't have bottom navigation bars and would like to return to my home page, that is the pages with bottom Navigation bars (A & B), using "Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => A()), (Route route) => false),", the bottom Navigation Bar in page A disappears. The same happens when I navigate to page B. How can I "pushAndRemoveUntil" to either pages (A & B) in my home page without losing the bottom Navigation Bar?

This is how I have implemented the bottom Navigation Bar:

return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: PageView(
        children: <Widget>[
          A(),
          B(),
        ],
        controller: pageController,
        onPageChanged: onPageChanged,
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: CupertinoTabBar(
        backgroundColor: appbar_Color,
        currentIndex: pageIndex,
        onTap: onTap,
        activeColor: Theme.of(context).primaryColor,
        items: [
          BottomNavigationBarItem(
            title: Text("Page A"),
            icon: Icon(Icons.whatshot),
          ),
          BottomNavigationBarItem(
            title: Text("Page B"),
            icon: Icon(Icons.notifications_active),
          ),
        ],
      ),
    );

my onTap function:

onTap(int pageIndex) {
    pageController.animateToPage(pageIndex,
        duration: Duration(milliseconds: 100), curve: Curves.easeInOut);
  }

Thank you so much.

2
Hi I think the proplem is: Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => A()), (Route route) => false). It navigator to A class not the class contains A & B. You should be navigator to the class contain A & B class. - darkness

2 Answers

0
votes

You just need to call Navigator.pop(context) on other pages as they are just a stack on top of your current Scaffold containing PageView of 2 Pages. Just pop the page out and you will be redirected with the same screen.

0
votes

It doesn't seem that you want to use pushAndRemoveUntil that push the given route onto the navigator, and then remove all the previous routes until the predicate returns true. It seems that you want to use pop (one view back) or popUntil (back until finding requested one)

This command will take you directly to the first view in the stack

Navigator.of(context).popUntil((route) => route.isFirst);

This other command will go one view back in the stack

Navigator.of(context).pop();