Tab Page Selector Widget in Flutter
Last Updated :
28 Apr, 2025
This article will teach about Tab Page Selector Widget in a flutter.
What is Tab Page Selector Widget?
- TabPageSelector displays a row of small circular indicators, one per tab.
- Tabpageselector is a crucial part of the TabBar, which functions distinctly.
- It plays a major role in increasing the user experience while exploring the app.
- The selected tab’s indicator will have a different appearance from the others.
- TabPageSelector can be wired up with a controller, which can be declared and initialized
Constructor:
This is the constructor for Tab Page Selector Widget:
Dart
TabPageSelector({ Key? key,
TabController? controller,
double indicatorSize = 12.0,
Color? color,
Color? selectedColor,
BorderStyle? borderStyle
})
TabPageSelector creates a compact widget that indicates which tab has been selected.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Create a List of Widgets with three different Icons
// Creating the List of icons
List <Widget> widgets = const[
Icon(Icons.home),
Icon(Icons.settings),
Icon(Icons.person),
];
Step 3: Use SingleTickerProviderStateMixin
What is SingleTickerProviderStateMixin?
- Provides a single Ticker that is configured to only tick while the current tree is enabled, as defined by TickerMode.
- This mixin only supports vending a single ticker.
- Tab Controller is a given property in the constructor of TabPageSelector so we have created TabController variable and also set the index to 0
class _MyHomePageState extends State <MyHomePage> with SingleTickerProviderStateMixin {
late final TabController controller;
int _index = 0;
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
Step 4: Creating initState and disposeState
- We have set the controller to TabController
- provide length and set the initial index to _index which is 0.
- vsync = this which can be accessible because of SingleTickerProviderStateMixin.
- because we are using a controller so we also need to dispose of the controller
class _MyHomePageState extends State <MyHomePage> with SingleTickerProviderStateMixin {
late final TabController controller;
int _index = 0;
// Creating initState and dispose state
@override
void initState() {
super.initState();
controller = TabController(length: widgets.length, initialIndex: _index, vsync: this);
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
Step 5: Creating an Appbar
@override
Widget build(BuildContext context) {
return Scaffold(
// created an appbar
appBar: AppBar(
title: Text('GeeksforGeeks'),
centerTitle: true,
),
);
}
Output Image:
Step 6: Create a Stack Widget
- we have given the alignment property with alignment.center.
- in children we have TabBarView.
- in TabBarView we have provided children as widgets where these widgets are the list of icons.
- Used Positioned widget to position TabPageSelector at the bottom of the screen.
// inside stack widget we have used TabBarView and have some alignment property
body: Stack(
alignment: Alignment.center,
children: < Widget > [
TabBarView(controller: controller, children: widgets),
//Used Positioned widget to position TabPageSelector at the bottom of screen.
Positioned(
bottom: 40,
child: TabPageSelector(
color: Colors.black38,
controller: controller,
),
),
],
),
Step 7: Create a Floating Action Button
- Use the Button Bar to navigate to next pages.
- provided the logic that if index = 0 then reset the page else navigate to the next page.
- We used controller.animateTo(_index) which will navigate to the next page
- Used Icons.navigate_next icon for ButtonBar by pressing button we will navigate.
// Creating floating action button for navigation
floatingActionButton: ButtonBar(
children: [
FloatingActionButton.small(
onPressed: () {
(_index != widgets.length - 1) ? _index++ : _index = 0;
controller.animateTo(_index);
},
child: Icon(Icons.navigate_next),
hoverElevation: 0,
elevation: 0,
)
],
)
Complete Code:
Dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
// List of icons
List <Widget> widgets = const[
Icon(Icons.home),
Icon(Icons.settings),
Icon(Icons.person),
];
class MyApp extends StatelessWidget {
const MyApp({Key ? key}): super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key ? key}): super(key: key);
@override
State <MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State <MyHomePage> with SingleTickerProviderStateMixin {
late final TabController controller;
int _index = 0;
@override
void initState() {
super.initState();
controller = TabController(
length: widgets.length,
initialIndex: _index,
vsync: this
);
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('GeeksforGeeks'),
centerTitle: true,
),
// Building UI
body: Stack(
alignment: Alignment.center,
children: <Widget> [
TabBarView(
controller: controller,
children: widgets),
Positioned(
bottom: 40,
child: TabPageSelector(color: Colors.black38,
controller: controller,
),
),
],
),
// Creating floating action button for navigation
floatingActionButton: ButtonBar(
children: [
FloatingActionButton.small(onPressed: () {
(_index != widgets.length - 1) ? _index++ : _index = 0;
controller.animateTo(_index);
},
child: Icon(Icons.navigate_next),
hoverElevation: 0,
elevation: 0,
)
],
),
);
}
}
Working Output:
Similar Reads
PageView Widget in Flutter The PageView widget allows the user to transition between different screens in their flutter application. All you need to set it up are a PageViewController and a PageView. Constructor of PageView class:Syntax: PageView({Key key, Axis scrollDirection, bool reverse, PageController controller, ScrollP
3 min read
Flutter - GestureDetector Widget A GestureDetector is a very useful Flutter widget that allows you to recognize and respond to various touch gestures, such as taps, swipe, double taps, drags, and more. It provides a way to make your Flutter app interactive and responsive to user input. In this article, we are going to implement and
4 min read
Flutter - SelectableText Widget Many times you need to select or copy some text like when you are on a web browser it allows us to select or copy any of the text without any issue but on mobile applications, it is not possible. So at times, you need to manually copy or enter such texts. But in the Flutter, with the help of the Sel
3 min read
Table Widget in Flutter Table widget is used to display items in a table layout. There is no need to use Rows and Columns to create a table. If we have multiple rows with the same width of columns then Table widget is the right approach. SliverList or Column will be most suitable if we only want to have a single column. Th
3 min read
OffStage Widget in Flutter Flutter provides an inbuilt widget called âOffstageâ, which is been used to hide or show any widget programmatically depending on user action/event. Offstage Widget is a widget that lays the child out as if it was in the tree, but without painting anything, without making the child available for hit
4 min read