'Flutter stop bottom navigation bar from build classes every time

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?



Solution 1:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamin