// Copyright 2014 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Flutter code sample for TabController import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); static const String _title = 'Flutter Code Sample'; @override Widget build(BuildContext context) { return const MaterialApp( title: _title, home: MyStatelessWidget(), ); } } const List<Tab> tabs = <Tab>[ Tab(text: 'Zeroth'), Tab(text: 'First'), Tab(text: 'Second'), ]; class MyStatelessWidget extends StatelessWidget { const MyStatelessWidget({super.key}); @override Widget build(BuildContext context) { return DefaultTabController( length: tabs.length, // The Builder widget is used to have a different BuildContext to access // closest DefaultTabController. child: Builder(builder: (BuildContext context) { final TabController tabController = DefaultTabController.of(context)!; tabController.addListener(() { if (!tabController.indexIsChanging) { // Your code goes here. // To get index of current tab use tabController.index } }); return Scaffold( appBar: AppBar( bottom: const TabBar( tabs: tabs, ), ), body: TabBarView( children: tabs.map((Tab tab) { return Center( child: Text( '${tab.text!} Tab', style: Theme.of(context).textTheme.headline5, ), ); }).toList(), ), ); }), ); } }