Top 4 Flutter development courses to Learn Flutter in 2020
Check these flutter development courses which are in sales right now
The default tabs styles provided by the flutter SDK is not much appealing. But It doesn’t mean you cannot customize the look and feel of the tab. Customizing the style of the tabs indicator in Flutter can be done with simple lines of code without implementing our own widget.
In this article, I will show you how to add 5 different tab styles for your next flutter project.
First, you need to create a basic tab using DefaultTabController class. Assign DefaultTabController to a home property of the MaterialApp widget. As the child of DefaultTabController, you can use Scaffold with the Appbar and the body. Assign Appbar widget to Appbar property of the Scaffold to make the header part of the tabs. For the body property of scaffold, you can assign TabBarView widget with the 3 child Widgets to show the tab content item when you click.
This is the full code for default tab.
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
elevation: 0,
bottom: TabBar(indicatorSize: TabBarIndicatorSize.label, tabs: [
Tab(
child: Align(
alignment: Alignment.center,
child: Text("APPS"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("MOVIES"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("GAMES"),
),
),
]),
),
body: TabBarView(children: [
Icon(Icons.apps),
Icon(Icons.movie),
Icon(Icons.games),
]),
)
)
As a First style, we are going to add a round corner style to the tab indicator. First of all, I will give a brief description of the param which we are going to modify.
Round corner style can be done by adding BoxDecoration with borderRadius 50. In here, we are adding a red colour border to each tab headers. When someone selects the tab It will fill with the red colour. If you are not interested in the border, you can just remove the border and keep it simple.
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
bottom: TabBar(
unselectedLabelColor: Colors.redAccent,
indicatorSize: TabBarIndicatorSize.label,
indicator: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.redAccent),
tabs: [
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.redAccent, width: 1)),
child: Align(
alignment: Alignment.center,
child: Text("APPS"),
),
),
),
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.redAccent, width: 1)),
child: Align(
alignment: Alignment.center,
child: Text("MOVIES"),
),
),
),
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.redAccent, width: 1)),
child: Align(
alignment: Alignment.center,
child: Text("GAMES"),
),
),
),
]),
),
body: TabBarView(children: [
Icon(Icons.apps),
Icon(Icons.movie),
Icon(Icons.games),
]),
))
We are going to remove the style which I was added to each tab in the previous method. After removing, added a gradient to BoxDecoration. You can use the LinearGradient widget with two colours get the gradient effect. You can change the gradient-based on your preferences.
(Want to learn more about Gradient? Check here)
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
bottom: TabBar(
unselectedLabelColor: Colors.redAccent,
indicatorSize: TabBarIndicatorSize.tab,
indicator: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.orangeAccent]),
borderRadius: BorderRadius.circular(50),
color: Colors.redAccent),
tabs: [
Tab(
child: Align(
alignment: Alignment.center,
child: Text("APPS"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("MOVIES"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("GAMES"),
),
),
]),
),
body: TabBarView(children: [
Icon(Icons.apps),
Icon(Icons.movie),
Icon(Icons.games),
]),
)
)
Square style can be done by changing small code in the previous one. BorderRadius of the boxDecoration can be changed by adding 10 for both leftTop and rightTop. Then I change the Appbar backgroundColor to the red to make it look better.
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.redAccent,
elevation: 0,
bottom: TabBar(
labelColor: Colors.redAccent,
unselectedLabelColor: Colors.white,
indicatorSize: TabBarIndicatorSize.label,
indicator: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10)),
color: Colors.white),
tabs: [
Tab(
child: Align(
alignment: Alignment.center,
child: Text("APPS"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("MOVIES"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("GAMES"),
),
),
]
),
),
body: TabBarView(children: [
Icon(Icons.apps),
Icon(Icons.movie),
Icon(Icons.games),
]),
)
)
You can get Diamond tab style by adding ShapeDecoration with BeveledRectangleBorder for the shape parameter for the ShapeDecoration widget. BeveledRectangleBorder will allow you to add flatten corner instead of round corners.
The line segments that connect the rectangle's four sides will begin and at locations offset by the corresponding border radius, but not farther than the side's center. If all the border radii exceed the sides' half widths/heights the resulting shape is diamond made by connecting the centers of the sides.
- flutter.dev - BeveledRectangleBorder
In here we use borderRadius as 10 to make it look like this.
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
bottom: TabBar(
unselectedLabelColor: Colors.redAccent,
indicatorPadding: EdgeInsets.only(left: 30, right: 30),
indicator: ShapeDecoration(
color: Colors.redAccent,
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10),
side: BorderSide(
color: Colors.redAccent,
)
)
),
tabs: [
Tab(
child: Align(
alignment: Alignment.center,
child: Text("APPS"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("MOVIES"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("GAMES"),
),
),
]),
),
body: TabBarView(children: [
Icon(Icons.apps),
Icon(Icons.movie),
Icon(Icons.games),
]),
)
)
Likewise, by changing the borderRadius of the BeveledRectangleBorder, you can achieve different shapes. You can change borderRadius to 20 to get a different shape. You can try different styles by changing this borderRadius value.
DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
bottom: TabBar(
unselectedLabelColor: Colors.redAccent,
indicatorPadding: EdgeInsets.only(left: 30, right: 30),
indicator: ShapeDecoration(
color: Colors.redAccent,
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: BorderSide(
color: Colors.redAccent,
))),
tabs: [
Tab(
child: Align(
alignment: Alignment.center,
child: Text("APPS"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("MOVIES"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("GAMES"),
),
),
]),
),
body: TabBarView(children: [
Icon(Icons.apps),
Icon(Icons.movie),
Icon(Icons.games),
]),
)
)
I hope you get a better understanding of how to change the tab style by a few lines of code. If you are like to watch this in action, please watch the video mentioned below.
Check these flutter development courses which are in sales right now