TabController class

Coordinates tab selection between a TabBar and a TabBarView.

The index property is the index of the selected tab and the animation represents the current scroll positions of the tab bar and the tar bar view. The selected tab's index can be changed with animateTo.

A stateful widget that builds a TabBar or a TabBarView can create a TabController and share it directly.

When the TabBar and TabBarView don't have a convenient stateful ancestor, a TabController can be shared by providing a DefaultTabController inherited widget.

This widget introduces a Scaffold with an AppBar and a TabBar.
class MyTabbedPage extends StatefulWidget {
  const MyTabbedPage({ Key key }) : super(key: key);
  @override
  _MyTabbedPageState createState() => _MyTabbedPageState();
}

class _MyTabbedPageState extends State<MyTabbedPage> with SingleTickerProviderStateMixin {
  final List<Tab> myTabs = <Tab>[
    Tab(text: 'LEFT'),
    Tab(text: 'RIGHT'),
  ];

  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: myTabs.length);
  }

 @override
 void dispose() {
   _tabController.dispose();
   super.dispose();
 }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          controller: _tabController,
          tabs: myTabs,
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: myTabs.map((Tab tab) {
          return Center(child: Text(tab.text));
        }).toList(),
      ),
    );
  }
}

Inheritance

Constructors

TabController({int initialIndex: 0, @required int length, @required TickerProvider vsync })
Creates an object that manages the state required by TabBar and a TabBarView. [...]

Properties

animation Animation<double>
An animation whose value represents the current position of the TabBar's selected tab indicator as well as the scrollOffsets of the TabBar and TabBarView. [...]
read-only
index int
The index of the currently selected tab. Changing the index also updates previousIndex, sets the animation's value to index, resets indexIsChanging to false, and notifies listeners. [...]
read / write
indexIsChanging bool
True while we're animating from previousIndex to index.
read-only
length int
The total number of tabs. Typically greater than one.
final
offset double
The difference between the animation's value and index. The offset value must be between -1.0 and 1.0. [...]
read / write
previousIndex int
The index of the previously selected tab. Initially the same as index.
read-only
hashCode int
The hash code for this object. [...]
read-only, inherited
hasListeners bool
Whether any listeners are currently registered. [...]
@protected, read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

animateTo(int value, { Duration duration: kTabScrollDuration, Curve curve: Curves.ease }) → void
Immediately sets index and previousIndex and then plays the animation from its current value to index. [...]
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener and removeListener will throw after the object is disposed). [...]
override
addListener(VoidCallback listener) → void
Register a closure to be called when the object changes. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
notifyListeners() → void
Call all the registered listeners. [...]
@protected, inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited