Flutter stop bottom navigation bar from build classes every time

943

Well, there's really no way that you can prevent a rebuild. I'm going to point you to this answer as it's the best in my opinion.

How to deal with unwanted widget build?

Widgets can be rebuilt at any time for any reason. If you don't want a serious performance impact, keep logic out of your build method as the build method should only be for displaying UI. The BLoC pattern is really good at separating display and logic.

So basically, keep your build methods clean.

Share:
943
Wail Hayaly
Author by

Wail Hayaly

Updated on December 16, 2022

Comments

  • Wail Hayaly
    Wail Hayaly over 1 year

    I have a bottom navigation bar to navigate between classes in the same screen

    my main build widget:

      Widget build(BuildContext context) {
        return SafeArea(
          top: false,
          child: Scaffold(
            body: PageStorage(
              child: Stack(
                children: <Widget>[currentPage, bottomBar()],
              ),
              bucket: bucket,
            ),
          ),
        );
      }
    

    my bottom bar

    Widget bottomBar() {
        return Column(
          children: <Widget>[
            const Expanded(
              child: SizedBox(),
            ),
            BottomBarView(
              tabIconsList: tabIconsList,
              addClick: () {},
              changeIndex: (int index) {
                setState(() {
                  currentTab = index;
                  currentPage = pages[index];
                  print(pages[index]);
                  print(currentTab);
                });
              },
            ),
          ],
        );
      }
    

    The bottom bar is working properly but it every time I press a button it rebuilds the same class over and over again even though I'm using bucket and PageStoorage How can I stop rebuilding classes?

    • anoop4real
      anoop4real over 3 years
      Did you get any solution?
    • Wail Hayaly
      Wail Hayaly over 3 years
      @anoop4real I'm using TabBar instead because it saves the last scroll position of the tab
    • anoop4real
      anoop4real over 3 years
      something like this? stackoverflow.com/questions/51824959/… ..... what about API calls and stuffs...lets say if I have an APi call will it be called every time when switching the tab?
    • Wail Hayaly
      Wail Hayaly over 3 years
      I believe fetching will be called every time, unless you add some state management tools like flutter_bloc, provider, GetX
  • Wail Hayaly
    Wail Hayaly over 4 years
    But it's not impossible, what I exactly mean is to create an app like Instagram which navigates through explore, account or activities without refreshing the data and scroll location
  • Benjamin
    Benjamin over 4 years
    Are you saying that when you press a button on the nav bar it resets and the icon doesn't change?
  • Wail Hayaly
    Wail Hayaly over 4 years
    No, it shows the widget from the very beginning. Like you have a futurebuilder to fetch the data and build it only one time, so every time you press a button on the nav bar it shows the reload(CircularProgressIndicator) and re-fetch the data and show the widget from the top (losing the latest scroll location)