TabBarAndTabBarView
The TabBar and TabBarView widgets in Flutter are essential components for creating tabbed user interfaces. They allow users to switch between different content sections or views by selecting tabs. Here's an overview of these two widgets:
TabBar Widget:
TabBar Widget:The TabBar widget is used to define a set of tabs that users can interact with. It typically works in conjunction with a DefaultTabController or TabController to manage the state and behavior of the tabs. Key properties and features of the TabBar widget include:
tabs(required): A list ofTabwidgets that represent the individual tabs. EachTabcontains a label (usually aTextwidget) and an optional icon.controller(required when not usingDefaultTabController): ATabControllerthat manages the state of theTabBarand is responsible for synchronizing the selected tab with theTabBarView.isScrollable: If set totrue, the tabs will be scrollable if there are too many to fit within the available space.**
labelColor,unselectedLabelColor: Define the color of the tab labels when they are selected and unselected.**
indicatorColor: Sets the color of the selected tab indicator (the line under the selected tab).**
indicatorWeight: Specifies the thickness of the selected tab indicator.
TabBarView Widget:
TabBarView Widget:The TabBarView widget is used to display the content associated with the tabs. It works in conjunction with the TabBar and the same TabController to ensure that the content matches the selected tab. Key properties and features of the TabBarView widget include:
controller(required): The sameTabControllerused for theTabBar.children(required): A list of widgets, typically views or screens, that correspond to each tab. The order of the children should match the order of the tabs in theTabBar.
Example Usage:
Here's a simple example of how to use TabBar and TabBarView in Flutter:
DefaultTabController(
length: 3, // Number of tabs
child: Scaffold(
appBar: AppBar(
title: Text('Tabbed Example'),
bottom: TabBar(
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
],
),
),
body: TabBarView(
children: [
Center(child: Text('Tab 1 Content')),
Center(child: Text('Tab 2 Content')),
Center(child: Text('Tab 3 Content')),
],
),
),
)In this example:
We wrap our
Scaffoldwith aDefaultTabControllerto manage the state of the tabs.Inside the
Scaffold, we define anAppBarwith aTabBaras itsbottomproperty, containing three tabs.The
TabBarViewdisplays the corresponding content for each tab.
When the user selects a tab, the content in the TabBarView is updated to match the selected tab. This allows you to create tabbed interfaces for various purposes, such as navigation, settings, or displaying different views of data.
Also, you can use TabController without DefaultTabController if you prefer to manage the tab controller manually. Here's an example of how to do that:
In this example:
We create a
TabControllerwith a length of 3 to manage the tabs.We set the
vsyncparameter ofTabControllertoAnimatedListState()to provide the controller with the TickerProvider needed for animations.The
TabBarandTabBarViewboth use the samecontrollerproperty, which is theTabControllerwe've created.The
Expandedwidget is used to ensure that theTabBarViewtakes up the remaining available space in the column.
This approach gives you more control over the tab controller and is useful when you want to manage the tab controller's state explicitly.
Last updated