2

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?

Wail Hayaly
  • 1,057
  • 1
  • 15
  • 31
  • Did you get any solution? – anoop4real Oct 05 '20 at 21:44
  • @anoop4real I'm using TabBar instead because it saves the last scroll position of the tab – Wail Hayaly Oct 09 '20 at 05:34
  • something like this? https://stackoverflow.com/questions/51824959/tabbar-on-bottom-of-app-with-column ..... what about API calls and stuffs...lets say if I have an APi call will it be called every time when switching the tab? – anoop4real Oct 09 '20 at 09:47
  • I believe fetching will be called every time, unless you add some state management tools like flutter_bloc, provider, GetX – Wail Hayaly Oct 10 '20 at 05:42

1 Answers1

1

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.

Benjamin
  • 5,783
  • 4
  • 25
  • 49
  • 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 – Wail Hayaly Dec 19 '19 at 12:33
  • Are you saying that when you press a button on the nav bar it resets and the icon doesn't change? – Benjamin Dec 19 '19 at 12:36
  • 2
    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) – Wail Hayaly Dec 19 '19 at 12:42