Flutter › Navigation & Routing in Flutter
How do you properly implement a bottom navigation bar in Flutter, correctly switching between several genuinely separate main top level screens while properly preserving each individual tab's own current specific state?
IntermediateAnswer
Implementing a bottom navigation bar typically involves using a BottomNavigationBar widget together with an IndexedStack, which properly keeps every single one of your several genuinely separate tab screens genuinely alive simultaneously within memory while only actually visually displaying whichever one particular currently selected tab happens to be genuinely active, correctly preserving each individual tab's own current specific scroll position and internal state even as a user actually switches back and forth between them, rather than an approach that would otherwise completely rebuild a given tab's own screen entirely from scratch every single time it is genuinely selected again.
IndexedStack(
index: currentTabIndex,
children: [HomeTab(), SearchTab(), ProfileTab()],
)Real-world example A shopping app uses IndexedStack together with a BottomNavigationBar to correctly preserve a user's own current scroll position within their search results tab, even after they briefly switch over to check their profile tab and then genuinely switch back again.