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?

Intermediate

Answer

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.

Common follow-ups: What genuinely happens to overall memory usage if you have a genuinely very large number of separate tabs, each containing quite heavy screens, all kept simultaneously alive together using IndexedStack?;What is the specific practical difference between using IndexedStack and simply rebuilding a genuinely brand new screen from scratch each single time a tab is selected?