| // 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. |
| |
| import 'package:flutter/material.dart'; |
| |
| /// Flutter code sample for [TabController]. |
| |
| void main() => runApp(const TabControllerExampleApp()); |
| |
| class TabControllerExampleApp extends StatelessWidget { |
| const TabControllerExampleApp({super.key}); |
| |
| @override |
| Widget build(BuildContext context) { |
| return const MaterialApp( |
| home: TabControllerExample(), |
| ); |
| } |
| } |
| |
| const List<Tab> tabs = <Tab>[ |
| Tab(text: 'Zeroth'), |
| Tab(text: 'First'), |
| Tab(text: 'Second'), |
| ]; |
| |
| class TabControllerExample extends StatelessWidget { |
| const TabControllerExample({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.headlineSmall, |
| ), |
| ); |
| }).toList(), |
| ), |
| ); |
| }), |
| ); |
| } |
| } |