0

I have this problem, i have a home page where it has tabs. I like when i switch tabs to make the TabBar show black the tab that is selected and also i want to change the color of the whole Scaffold. So i made also a custom controller and used it like this:

TabController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TabController(vsync: this, length: 5);
    _controller.index = 1;
    _controller.addListener(() {
      if (!_controller.indexIsChanging) {
         setState(() {
          scaffoldColor = colors[_controller.index];
         });
      }
    });
  }

The thing is that in this way all of my tabs are going to be rebuild and this is very bad because i have heavy tasks in few of them. I also have used AutomaticKeepAliveClientMixin in all of the tabs but it didn't fix the problem. By the way i used it like this:

class Tab1 extends StatefulWidget {
  @override
  _Tab1State createState() => _Tab1State();
}

class _Tab1State extends State<Tab1> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    print("Tab 1 Has been built");
    return Text("TAB 1");
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

Panagiss
  • 3,154
  • 2
  • 20
  • 34

1 Answers1

0

If for heavy tasks you mean a Future, you should place it inside initState.

See this answer: How to load async Stream only one time in Flutter?.

LateBoy
  • 99
  • 2
  • 9
  • no it isn't like that. But it is crucial not to rebuild all of the tabs of the TabBarView – Panagiss Nov 11 '20 at 17:02
  • If your tabs are different bodies of the same Scaffold, I don't know how you can change the color of the Scaffold without rebuilding it. To keep the state of the tabs I used IndexedStack. – LateBoy Nov 11 '20 at 17:08
  • my tabs are different widgets. But yeah are in the same scaffold cause in the AppBar i have the TabBar. So yeah... – Panagiss Nov 11 '20 at 17:19