TabController class - material library (original) (raw)

Coordinates tab selection between a TabBar and a TabBarView.

The index property is the index of the selected tab and the animationrepresents the current scroll positions of the tab bar and the tab 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 aDefaultTabController inherited widget.

link

class MyTabbedPage extends StatefulWidget {
  const MyTabbedPage({ super.key });
  @override
  State<MyTabbedPage> createState() => _MyTabbedPageState();
}

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

  late 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) {
          final String label = tab.text!.toLowerCase();
          return Center(
            child: Text(
              'This is the $label tab',
              style: const TextStyle(fontSize: 36),
            ),
          );
        }).toList(),
      ),
    );
  }
}

link

To create a local project with this code sample, run:
flutter create --sample=material.TabController.2 mysample

Inheritance

Constructors

TabController({int initialIndex = 0, Duration? animationDuration, required int length, required TickerProvider vsync})

Creates an object that manages the state required by TabBar and aTabBarView.

Properties

animationAnimation<double>?

An animation whose value represents the current position of the TabBar's selected tab indicator as well as the scrollOffsets of the TabBarand TabBarView.

no setter

animationDurationDuration

Controls the duration of TabController and TabBarView animations.

no setter

hashCodeint

The hash code for this object.

no setterinherited

hasListenersbool

Whether any listeners are currently registered.

no setterinherited

indexint

The index of the currently selected tab.

getter/setter pair

indexIsChangingbool

True while we're animating from previousIndex to index as a consequence of calling animateTo.

no setter

lengthint

The total number of tabs.

final

offsetdouble

The difference between the animation's value and index.

getter/setter pair

previousIndexint

The index of the previously selected tab.

no setter

runtimeTypeType

A representation of the runtime type of the object.

no setterinherited

Methods

addListener(VoidCallback listener)→ void

Register a closure to be called when the object changes.

inherited

animateTo(int value, {Duration? duration, Curve curve = Curves.ease})→ void

Immediately sets index and previousIndex and then plays theanimation 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 toaddListener will throw after the object is disposed).

override

noSuchMethod(Invocation invocation)→ dynamic

Invoked when a nonexistent method or property is accessed.

inherited

notifyListeners()→ void

Call all the registered listeners.

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

A string representation of this object.

inherited

Operators

operator ==(Object other)→ bool

The equality operator.

inherited