Skip to content

Add onChanged callback to DefaultTabController to get current tab index #174474

@mu7mmd

Description

@mu7mmd

Use case

Description:
Currently, DefaultTabController doesn’t provide a direct way to listen to tab changes.
If we want to know the current tab index, we have to use a TabController manually and add a listener, which removes the simplicity of using DefaultTabController.

It would be very useful if DefaultTabController exposed an onChanged callback that returns the current tab index whenever the user changes tabs.

Expected behavior:
When the user switches tabs (by tapping or swiping), the onChanged callback should be triggered with the updated index.

Proposal

DefaultTabController(
  length: 3,
  onChanged: (index) {
    print('Current tab index: $index');
  },
  child: Scaffold(
    appBar: AppBar(
      bottom: TabBar(
        tabs: [
          Tab(text: 'Tab 1'),
          Tab(text: 'Tab 2'),
          Tab(text: 'Tab 3'),
        ],
      ),
    ),
    body: TabBarView(
      children: [
        Center(child: Text('Tab 1')),
        Center(child: Text('Tab 2')),
        Center(child: Text('Tab 3')),
      ],
    ),
  ),
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    in triagePresently being triaged by the triage teamwaiting for customer responseThe Flutter team cannot make further progress on this issue until the original reporter responds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions