I am learning Flutter and struggling with some state management issue.
I have a HomeScreen widget which contains Scaffold and a BottomNavigationBar. To switch pages based on the selected tab in BottomNavigationBar I am using PageView with PageController. Here is how the build
method of HomeScreen widget looks:
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: _currentIndex == 2,
appBar: AppBar(...properties),
body: PageView(
controller: _pageController,
children: _pages,
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[...items],
currentIndex: _currentIndex,
onTap: _onItemTapped, //This changes state _currentIndex and calls the method _pageController.jumpToPage(_currentIndex);
),
);
}
_currentIndex
initially is 0._pages
is a list containing 3 widgets._pageController
is simple PageController with initialPage set to 0.
If you notice I am using a property extendBodyBehindAppBar: _currentIndex == 2
which is using _currentIndex
state and this is causing issue.
When I tap on the last Tab on the BottomNavigationBar the state the _currentIndex
changes to 2 and thus the extendBodyBehindAppBar
is set as true
which makes entire Scaffold rebuild itself and the state of the PageView is lost.
If comment out the line extendBodyBehindAppBar: _currentIndex == 2
, then even if the Scaffold rebuilds the state of the PageView widget is preserved.
As of my understanding the Flutter should keep the state of child Widgets even if the parent rebuilds because the WidgetTree is not changed or re-arranged. I tried using Key on PageView but nothing worked.
Any help is very much appreciated.