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 ofTab
widgets that represent the individual tabs. EachTab
contains a label (usually aText
widget) and an optional icon.controller
(required when not usingDefaultTabController
): ATabController
that manages the state of theTabBar
and 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 sameTabController
used 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:
In this example:
We wrap our
Scaffold
with aDefaultTabController
to manage the state of the tabs.Inside the
Scaffold
, we define anAppBar
with aTabBar
as itsbottom
property, containing three tabs.The
TabBarView
displays 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
TabController
with a length of 3 to manage the tabs.We set the
vsync
parameter ofTabController
toAnimatedListState()
to provide the controller with the TickerProvider needed for animations.The
TabBar
andTabBarView
both use the samecontroller
property, which is theTabController
we've created.The
Expanded
widget is used to ensure that theTabBarView
takes 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