|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Flutter code sample for [TabBar]. |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | + |
| 9 | +void main() => runApp(const TabBarApp()); |
| 10 | + |
| 11 | +class TabBarApp extends StatelessWidget { |
| 12 | + const TabBarApp({super.key}); |
| 13 | + |
| 14 | + @override |
| 15 | + Widget build(BuildContext context) { |
| 16 | + return MaterialApp( |
| 17 | + theme: ThemeData(useMaterial3: true), |
| 18 | + home: const TabBarExample(), |
| 19 | + ); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class TabBarExample extends StatelessWidget { |
| 24 | + const TabBarExample({super.key}); |
| 25 | + |
| 26 | + @override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + return DefaultTabController( |
| 29 | + initialIndex: 1, |
| 30 | + length: 3, |
| 31 | + child: Scaffold( |
| 32 | + appBar: AppBar( |
| 33 | + title: const Text('Primary and secondary TabBar'), |
| 34 | + bottom: const TabBar( |
| 35 | + dividerColor: Colors.transparent, |
| 36 | + tabs: <Widget>[ |
| 37 | + Tab( |
| 38 | + text: 'Flights', |
| 39 | + icon: Icon(Icons.flight), |
| 40 | + ), |
| 41 | + Tab( |
| 42 | + text: 'Trips', |
| 43 | + icon: Icon(Icons.luggage), |
| 44 | + ), |
| 45 | + Tab( |
| 46 | + text: 'Explore', |
| 47 | + icon: Icon(Icons.explore), |
| 48 | + ), |
| 49 | + ], |
| 50 | + ), |
| 51 | + ), |
| 52 | + body: const TabBarView( |
| 53 | + children: <Widget>[ |
| 54 | + NestedTabBar('Flights'), |
| 55 | + NestedTabBar('Trips'), |
| 56 | + NestedTabBar('Explore'), |
| 57 | + ], |
| 58 | + ), |
| 59 | + ), |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class NestedTabBar extends StatefulWidget { |
| 65 | + const NestedTabBar(this.outerTab, {super.key}); |
| 66 | + |
| 67 | + final String outerTab; |
| 68 | + |
| 69 | + @override |
| 70 | + State<NestedTabBar> createState() => _NestedTabBarState(); |
| 71 | +} |
| 72 | + |
| 73 | +class _NestedTabBarState extends State<NestedTabBar> with TickerProviderStateMixin { |
| 74 | + late final TabController _tabController; |
| 75 | + |
| 76 | + @override |
| 77 | + void initState() { |
| 78 | + super.initState(); |
| 79 | + _tabController = TabController(length: 2, vsync: this); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + void dispose() { |
| 84 | + _tabController.dispose(); |
| 85 | + super.dispose(); |
| 86 | + } |
| 87 | + |
| 88 | + @override |
| 89 | + Widget build(BuildContext context) { |
| 90 | + return Column( |
| 91 | + children: <Widget>[ |
| 92 | + TabBar.secondary( |
| 93 | + controller: _tabController, |
| 94 | + tabs: const <Widget>[ |
| 95 | + Tab(text: 'Overview'), |
| 96 | + Tab(text: 'Specifications'), |
| 97 | + ], |
| 98 | + ), |
| 99 | + Expanded( |
| 100 | + child: TabBarView( |
| 101 | + controller: _tabController, |
| 102 | + children: <Widget>[ |
| 103 | + Card( |
| 104 | + margin: const EdgeInsets.all(16.0), |
| 105 | + child: Center(child: Text('${widget.outerTab}: Overview tab')), |
| 106 | + ), |
| 107 | + Card( |
| 108 | + margin: const EdgeInsets.all(16.0), |
| 109 | + child: Center(child: Text('${widget.outerTab}: Specifications tab')), |
| 110 | + ), |
| 111 | + ], |
| 112 | + ), |
| 113 | + ), |
| 114 | + ], |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments