Flutter TabBar: A complete tutorial with examples
To implement TabBar in
your Flutter app, Follow the following steps:
In this tutorial, we’ll tell you everything you need to know about TabBar in
Flutter, show you how to implement tabs in your Flutter app, and walk you
through some TabBar examples.
In This Blog, we will Cover The Following Topics
- Setting
up Default or Simple TabBar in Flutter
- How
to customize or Colored the tab indicator in TabBar
- Making
scrollable tabs with TabBar
- Changing
tabs programmatically
- Listening
for tab change event
- How
to implement TabBar without AppBar
- Preserving
the state of tabs
- Setting Up Default or Simple TabBar In your flutter
app
To Implement a Default TabBar in your Flutter app Simply Follow
the three-Step Below
Step 1: Wrap the Scaffold widget inside DefaultTabController.
Step 2: Place the TabBar widget as the bottom property of AppBar
Step 3: Provide TabBarView in the body of the AppBar. TabBarView is like PageView,
which is used mostly with TabBar because it shows the widget based on the
currently selected tab
now Let's write the Code for Default or Simple TabBar In your flutter app
Below are some examples of ways you can modify the TabIndicator to improve the user experience and overall appearance of your app.
Now Let us Modify The TabIndicator In Different Ways,
Tab color
To alter the color of a tab indicator: Add, indicatorColor: Colors.amberaccents; Property to TabBar
TabBar(
indicatorColor: Colors.amberAccent,
tabs: [],
)
Here is a full Source code with TabIndicator Color changed to amberAccent.Tab size
To alter the Size of the TabBar indicator :
Add, indicatorSize: TabBarIndicatorSize.label, Property
to TabBar This makes The indicator size equal to the width of The labels the
Default value is TabBarIndicatorSize.tab.
TabBar(
indicatorSize: TabBarIndicatorSize.label,
tabs: [],
)
Here you Can use a full source code with TabBar indicator size is equal to indicatorSize: TabBarIndicatorSize.label, which means the indicator size equal to the width of the label
Tab height
To alter the Size of TabBar indicator Weight: Add,
TabBar(
indicatorWeight: 10,
tabs: [],
)
Change the indicator
You can change the indicator itself, and Add your own Custom Indicator
TabBar(
indicator: BoxDecoration(
borderRadius: BorderRadius.circular(50), // Creates border
color: Colors.greenAccent), //Change background color from here
tabs: [],
)
Background image
To set a background image with TabBarIndicator:
indicator: BoxDecoration(
color: Colors.greenAccent,
image: DecorationImage(
image: AssetImage('assets/images/placeholder.png'),
fit: BoxFit.fitWidth)), Making scrollable tabs with TabBar
Making scrollable tabs with TabBar
The TabBar widget has a property dedicated to make horizontally scrollable tabs. Set the isscrollable to True, and the job is done. You’ll have to set it explicitly because it defaults to False
Vertically scrollable tabs with AppBar
Hidden Appbar is the Appbar, when we are scrolling the main body of the Application, the Appbar also scrolled and goes to hidden. Here is The Source code to achieve this
DefaultTabController(Sorce
length: 5,
child: Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
new SliverAppBar(
title: Text('Tabs Demo'),
pinned: true,
floating: true,
bottom: TabBar(
isScrollable: true,
tabs: [
Tab(child: Text('Flight')),
Tab(child: Text('Train')),
Tab(child: Text('Car')),
Tab(child: Text('Cycle')),
Tab(child: Text('Boat')),
],
),
),
];
},
body: TabBarView(
children: <Widget>[
Icon(Icons.flight, size: 350),
Icon(Icons.directions_transit, size: 350),
Icon(Icons.directions_car, size: 350),
Icon(Icons.directions_bike, size: 350),
Icon(Icons.directions_boat, size: 350),
],
),
)),
);
Changing tabs programmatically
TabController
to move to the next page with the click of a button: assign initial TabController with 0 (zero-index ) and Increment this index when Button PressedTabController _controller= initialindex=0;
Listening for tab change event
You may want to perform some operations when a specific tab is open. This callback comes in handy when you want to perform initializing something again when a particular tab is open or destroy something when the tab is not open.@override
void initState() {
// TODO: implement initState
super.initState();
_controller = TabController(length: 5, vsync: this);
_controller.addListener(() {
setState(() {
_selectedIndex = _controller.index;
});
print("Selected Index: " + _controller.index.toString());
});
}
_controller.addListener(() {
setState(() {
_selectedIndex = _controller.index;
});
print("Selected Index: " + _controller.index.toString());
});
onTap
of TabBar for the tab change event, like this:bottom: TabBar(onTap: (){
},
Leave a Comment