Which widget handles the tab switching logic for TabBar automatically?
DefaultTabController wraps the scaffold and manages the selected tab index automatically: no setState needed.
Build tab-based navigation with BottomNavigationBar and TabBar
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Two essential patterns for organizing app sections:
BottomNavigationBar is best for primary app sections that users switch between often. TabBar works well for categorizing content within a single screen.
1// ── BottomNavigationBar ──2class MainScreen extends StatefulWidget {3@override4State<MainScreen> createState() => _MainScreenState();5}67class _MainScreenState extends State<MainScreen> {8int _currentIndex = 0;910final _pages = const [11HomeScreen(),12SearchScreen(),13ProfileScreen(),14];1516@override17Widget build(BuildContext context) {18return Scaffold(19body: _pages[_currentIndex],20bottomNavigationBar: BottomNavigationBar(21currentIndex: _currentIndex,22onTap: (index) => setState(() => _currentIndex = index),23items: const [24BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),25BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),26BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),27],28),29);30}31}
1// ── TabBar with TabBarView ──2class CategoryScreen extends StatelessWidget {3@override4Widget build(BuildContext context) {5return DefaultTabController(6length: 3, // number of tabs7child: Scaffold(8appBar: AppBar(9title: const Text('Categories'),10bottom: const TabBar(11tabs: [12Tab(icon: Icon(Icons.phone), text: 'Mobile'),13Tab(icon: Icon(Icons.laptop), text: 'Web'),14Tab(icon: Icon(Icons.desktop_mac), text: 'Desktop'),15],16),17),18body: const TabBarView(19children: [20Center(child: Text('Mobile Content')),21Center(child: Text('Web Content')),22Center(child: Text('Desktop Content')),23],24),25),26);27}28}
BottomNavigationBar - primary app sections (2-5 tabs, swiping not expected) TabBar - content categories within a screen (swiping between tabs is expected)
In tabbed apps, users expect each tab to remember scroll position and local state.
For BottomNavigationBar flows, consider keeping tab pages alive (for example with IndexedStack) instead of recreating each tab on every tap.
Build tab based navigation with BottomNavigationBar and TabBar. Navigation manages how users move between screens and return to previous states.
Connect each concept to real app features you can build.
Design route names and arguments before wiring screen UI.
Poor routing decisions create confusing UX and fragile deep-link behavior.
DefaultTabController wraps the scaffold and manages the selected tab index automatically: no setState needed.
Scaffold has a bottomNavigationBar property specifically for this widget, placing it at the bottom of the screen.
BottomNavigationBar's onTap gives the tapped index. Call setState to update _currentIndex, which changes the displayed page.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue