TabPageSelector Widget and Attributes

TabPageSelector Widget and Attributes

ยท

2 min read

In Flutter, the TabPageSelector widget is a handy tool when working with tabbed navigation. It provides an elegant and customizable way to visually represent and navigate between pages within a TabBarView. Let's explore the TabPageSelector widget and its essential attributes through a detailed example.

What is the TabPageSelector Widget?

The TabPageSelector widget in Flutter is designed to complement the TabBarView, offering a visual representation of the available pages with a selectable indicator. This widget is particularly useful when dealing with tabbed interfaces, allowing users to easily switch between different content sections.

Attributes of TabPageSelector:

1. controller:

  • The controller attribute is a PageController that manages the state of the TabPageSelector. It is typically synchronized with the TabBarView's controller for seamless navigation.

2. indicatorSize:

  • The indicatorSize attribute defines the size of the indicator that represents the currently selected page.

3. color:

  • The color attribute sets the color of the indicator.

4. selectedColor:

  • The selectedColor attribute determines the color of the indicator for the selected page.

5. indicatorPadding:

  • The indicatorPadding attribute allows you to set padding around the indicator.

Example:

Let's create a simple example where we use the TabPageSelector widget alongside a TabBarView to navigate between different pages.

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 5,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('TabPageSelector Alternative'),
          bottom: const TabBar(
            indicatorSize: TabBarIndicatorSize.label,
            indicatorColor: Colors.blue,
            dividerColor:Color.fromARGB(100, 100, 100, 100),
            tabs: [
              Tab(text: 'Page 1'),
              Tab(text: 'Page 2'),
              Tab(text: 'Page 3'),
              Tab(text: 'Page 4'),
              Tab(text: 'Page 5'),
            ],
          ),
        ),
        body: TabBarView(
          children: [
            Container(color: Colors.blue),
            Container(color: Colors.orange),
            Container(color: Colors.green),
            Container(color: Colors.orange),
            Container(color: Colors.green),
          ],
        ),
      ),
    );
  }
}

In this example:

  • The TabPageSelector widget is used below a TabBarView to provide a visual representation of the available pages.

  • The controller attribute of TabPageSelector is synchronized with the PageController of the TabBarView for consistent navigation.

  • The indicatorSize, color, selectedColor, and indicatorPadding attributes are customized to control the appearance of the indicator.

By incorporating the TabPageSelector widget into your Flutter app, you can enhance the navigation experience, making it more intuitive and user-friendly.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย