From 802ba9ac9004187515c51b68f71b927eb534bff9 Mon Sep 17 00:00:00 2001 From: Ayush Bherwani Date: Fri, 12 Jun 2020 00:10:03 +0530 Subject: [PATCH] adds interactive example (#59251) --- .../lib/src/material/tab_controller.dart | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/packages/flutter/lib/src/material/tab_controller.dart b/packages/flutter/lib/src/material/tab_controller.dart index 4b7ca51be1..552ce2c6be 100644 --- a/packages/flutter/lib/src/material/tab_controller.dart +++ b/packages/flutter/lib/src/material/tab_controller.dart @@ -85,6 +85,59 @@ import 'constants.dart'; /// } /// ``` /// {@end-tool} +/// +/// {@tool dartpad --template=stateless_widget_material} +/// +/// This example shows how to listen to page updates in [TabBar] and [TabBarView] +/// when using [DefaultTabController]. +/// +/// ```dart preamble +/// final List tabs = [ +/// Tab(text: 'Zeroth'), +/// Tab(text: 'First'), +/// Tab(text: 'Second'), +/// ]; +/// ``` +/// +/// ```dart +/// 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: TabBar( +/// tabs: tabs, +/// ), +/// ), +/// body: TabBarView( +/// children: tabs.map((Tab tab){ +/// return Center( +/// child: Text( +/// tab.text + ' Tab', +/// style: Theme.of(context).textTheme.headline5, +/// ), +/// ); +/// }).toList(), +/// ), +/// ); +/// } +/// ), +/// ); +/// } +/// ``` +/// {@end-tool} +/// class TabController extends ChangeNotifier { /// Creates an object that manages the state required by [TabBar] and a /// [TabBarView].