What is TabBar in Flutter?
TabBar and TabBarView are widgets that together create a tabbed interface. The TabBar displays a row of tabs (usually at the top of the screen), and the TabBarView displays the content for each tab. Users can switch between tabs by tapping the tab or swiping horizontally on the content. Flutter provides DefaultTabController for simple cases and TabController for more control.
Basic TabBar with DefaultTabController
The simplest way to add tabs is to wrap your Scaffold with DefaultTabController. You specify the number of tabs, and the controller manages the state automatically. Place the TabBar in the AppBar's bottom property, and the TabBarView in the body.
Using TabBarView with Different Content
The TabBarView's children correspond to the tabs in order. You can place any widget, such as ListView, Column, or custom widgets. For performance, consider using ListView or SingleChildScrollView if the content might be large.
Customizing Tab Appearance
TabBar and Tab widgets offer several customization options:
labelColorandunselectedLabelColor: Text color of selected/unselected tabs.
indicator: ADecorationfor the tab indicator (underline). Common options:BoxDecoration,UnderlineTabIndicator.
indicatorSize:TabBarIndicatorSize.label(fits text) orTabBarIndicatorSize.tab(fills the whole tab).
indicatorColor: Color of the indicator.
labelStyleandunselectedLabelStyle: Text styles.
padding: Padding around the entire tab bar.
Tab with Icons and Text
You can combine icons and text using the Tab widget's icon and text properties, or use a custom child.
Using TabController Manually
When you need more control (e.g., programmatically changing tabs, animating to a tab), you can create a TabController manually. You must dispose it in dispose(). Also, you need to provide a TickerProvider (usually this with SingleTickerProviderStateMixin).
Nested Tabs
You can nest tabs by using another DefaultTabController or TabController inside a tab's content. Be careful to manage the state correctly; nested tab controllers need their own TickerProvider.
Common Mistakes
- Forgetting to set the number of tabs in
DefaultTabController:lengthmust match the number of tabs.
- Forgetting to set the number of tabs in
- Mixing
TabBarandTabBarViewwith different controllers: They must use the same controller.
- Mixing
- Not disposing the
TabController: Causes memory leaks.
- Not disposing the
- Using a
TabBarwithout aTabBarViewor vice versa: The tabs won't work correctly.
- Using a
- Overriding the
TabBarView's size:TabBarViewautomatically sizes to the largest child; if you constrain it, you might get clipping.
- Overriding the
Best Practices
- Use
DefaultTabControllerfor simple, static tabs.
- Use
- Use manual
TabControllerwhen you need to change tabs programmatically.
- Use manual
- Always dispose of
TabControllerindispose.
- Always dispose of
- For performance, avoid rebuilding large content inside
TabBarViewunnecessarily; useconstorAutomaticKeepAliveClientMixinto preserve state.
- For performance, avoid rebuilding large content inside
- Consider using
Scrollabletabs if the number of tabs is large.
- Consider using
Key Takeaways
TabBardisplays the tabs;TabBarViewdisplays the content.
DefaultTabControllersimplifies tab management for common cases.
- Use
TabControllerfor custom control (animation, programmatic switching).
- Use
- Customize tab appearance with
labelColor,indicator,indicatorSize, etc.
- Customize tab appearance with
- Always match the number of tabs with the number of children in
TabBarView.
- Always match the number of tabs with the number of children in