Skip to content

Commit 02d5c75

Browse files
authored
Add support for secondary tab bar (#122756)
Add support for secondary tab bar
1 parent ce68d97 commit 02d5c75

10 files changed

Lines changed: 801 additions & 130 deletions

File tree

‎dev/tools/gen_defaults/lib/tabs_template.dart‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class TabsTemplate extends TokenTemplate {
1212

1313
@override
1414
String generate() => '''
15-
class _${blockName}DefaultsM3 extends TabBarTheme {
16-
_${blockName}DefaultsM3(this.context)
15+
class _${blockName}PrimaryDefaultsM3 extends TabBarTheme {
16+
_${blockName}PrimaryDefaultsM3(this.context)
1717
: super(indicatorSize: TabBarIndicatorSize.label);
1818
1919
final BuildContext context;
@@ -69,5 +69,64 @@ class _${blockName}DefaultsM3 extends TabBarTheme {
6969
@override
7070
InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory;
7171
}
72+
73+
class _${blockName}SecondaryDefaultsM3 extends TabBarTheme {
74+
_${blockName}SecondaryDefaultsM3(this.context)
75+
: super(indicatorSize: TabBarIndicatorSize.tab);
76+
77+
final BuildContext context;
78+
late final ColorScheme _colors = Theme.of(context).colorScheme;
79+
late final TextTheme _textTheme = Theme.of(context).textTheme;
80+
81+
@override
82+
Color? get dividerColor => ${componentColor("md.comp.secondary-navigation-tab.divider")};
83+
84+
@override
85+
Color? get indicatorColor => ${componentColor("md.comp.primary-navigation-tab.active-indicator")};
86+
87+
@override
88+
Color? get labelColor => ${componentColor("md.comp.secondary-navigation-tab.active.label-text")};
89+
90+
@override
91+
TextStyle? get labelStyle => ${textStyle("md.comp.secondary-navigation-tab.label-text")};
92+
93+
@override
94+
Color? get unselectedLabelColor => ${componentColor("md.comp.secondary-navigation-tab.inactive.label-text")};
95+
96+
@override
97+
TextStyle? get unselectedLabelStyle => ${textStyle("md.comp.secondary-navigation-tab.label-text")};
98+
99+
@override
100+
MaterialStateProperty<Color?> get overlayColor {
101+
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
102+
if (states.contains(MaterialState.selected)) {
103+
if (states.contains(MaterialState.hovered)) {
104+
return ${componentColor('md.comp.secondary-navigation-tab.hover.state-layer')};
105+
}
106+
if (states.contains(MaterialState.focused)) {
107+
return ${componentColor('md.comp.secondary-navigation-tab.focus.state-layer')};
108+
}
109+
if (states.contains(MaterialState.pressed)) {
110+
return ${componentColor('md.comp.secondary-navigation-tab.pressed.state-layer')};
111+
}
112+
return null;
113+
}
114+
if (states.contains(MaterialState.hovered)) {
115+
return ${componentColor('md.comp.secondary-navigation-tab.hover.state-layer')};
116+
}
117+
if (states.contains(MaterialState.focused)) {
118+
return ${componentColor('md.comp.secondary-navigation-tab.focus.state-layer')};
119+
}
120+
if (states.contains(MaterialState.pressed)) {
121+
return ${componentColor('md.comp.secondary-navigation-tab.pressed.state-layer')};
122+
}
123+
return null;
124+
});
125+
}
126+
127+
@override
128+
InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory;
129+
}
72130
''';
131+
73132
}

‎examples/api/lib/material/tabs/tab_bar.0.dart‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66

77
import 'package:flutter/material.dart';
88

9-
void main() => runApp(const MyApp());
9+
void main() => runApp(const TabBarApp());
1010

11-
class MyApp extends StatelessWidget {
12-
const MyApp({super.key});
13-
14-
static const String _title = 'Flutter Code Sample';
11+
class TabBarApp extends StatelessWidget {
12+
const TabBarApp({super.key});
1513

1614
@override
1715
Widget build(BuildContext context) {
18-
return const MaterialApp(
19-
title: _title,
20-
home: MyStatelessWidget(),
16+
return MaterialApp(
17+
theme: ThemeData(useMaterial3: true),
18+
home: const TabBarExample(),
2119
);
2220
}
2321
}
2422

25-
class MyStatelessWidget extends StatelessWidget {
26-
const MyStatelessWidget({super.key});
23+
class TabBarExample extends StatelessWidget {
24+
const TabBarExample({super.key});
2725

2826
@override
2927
Widget build(BuildContext context) {
@@ -32,7 +30,7 @@ class MyStatelessWidget extends StatelessWidget {
3230
length: 3,
3331
child: Scaffold(
3432
appBar: AppBar(
35-
title: const Text('TabBar Widget'),
33+
title: const Text('TabBar Sample'),
3634
bottom: const TabBar(
3735
tabs: <Widget>[
3836
Tab(

‎examples/api/lib/material/tabs/tab_bar.1.dart‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,49 @@
66

77
import 'package:flutter/material.dart';
88

9-
void main() => runApp(const MyApp());
9+
void main() => runApp(const TabBarApp());
1010

11-
class MyApp extends StatelessWidget {
12-
const MyApp({super.key});
13-
14-
static const String _title = 'Flutter Code Sample';
11+
class TabBarApp extends StatelessWidget {
12+
const TabBarApp({super.key});
1513

1614
@override
1715
Widget build(BuildContext context) {
18-
return const MaterialApp(
19-
title: _title,
20-
home: MyStatefulWidget(),
16+
return MaterialApp(
17+
theme: ThemeData(useMaterial3: true),
18+
home: const TabBarExample(),
2119
);
2220
}
2321
}
2422

25-
class MyStatefulWidget extends StatefulWidget {
26-
const MyStatefulWidget({super.key});
23+
class TabBarExample extends StatefulWidget {
24+
const TabBarExample({super.key});
2725

2826
@override
29-
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
27+
State<TabBarExample> createState() => _TabBarExampleState();
3028
}
3129

3230
/// [AnimationController]s can be created with `vsync: this` because of
3331
/// [TickerProviderStateMixin].
34-
class _MyStatefulWidgetState extends State<MyStatefulWidget>
35-
with TickerProviderStateMixin {
36-
late TabController _tabController;
32+
class _TabBarExampleState extends State<TabBarExample> with TickerProviderStateMixin {
33+
late final TabController _tabController;
3734

3835
@override
3936
void initState() {
4037
super.initState();
4138
_tabController = TabController(length: 3, vsync: this);
4239
}
4340

41+
@override
42+
void dispose() {
43+
_tabController.dispose();
44+
super.dispose();
45+
}
46+
4447
@override
4548
Widget build(BuildContext context) {
4649
return Scaffold(
4750
appBar: AppBar(
48-
title: const Text('TabBar Widget'),
51+
title: const Text('TabBar Sample'),
4952
bottom: TabBar(
5053
controller: _tabController,
5154
tabs: const <Widget>[
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/tabs/tab_bar.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Switch tabs in the TabBar', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.TabBarApp(),
13+
);
14+
15+
final TabBar tabBar = tester.widget<TabBar>(find.byType(TabBar));
16+
expect(tabBar.tabs.length, 3);
17+
18+
final Finder tab1 = find.widgetWithIcon(Tab, Icons.cloud_outlined);
19+
final Finder tab2 = find.widgetWithIcon(Tab, Icons.beach_access_sharp);
20+
final Finder tab3 = find.widgetWithIcon(Tab, Icons.brightness_5_sharp);
21+
22+
const String tabBarViewText1 = "It's cloudy here";
23+
const String tabBarViewText2 = "It's rainy here";
24+
const String tabBarViewText3 = "It's sunny here";
25+
26+
expect(find.text(tabBarViewText1), findsNothing);
27+
expect(find.text(tabBarViewText2), findsOneWidget);
28+
expect(find.text(tabBarViewText3), findsNothing);
29+
30+
await tester.tap(tab1);
31+
await tester.pumpAndSettle();
32+
33+
expect(find.text(tabBarViewText1), findsOneWidget);
34+
expect(find.text(tabBarViewText2), findsNothing);
35+
expect(find.text(tabBarViewText3), findsNothing);
36+
37+
await tester.tap(tab2);
38+
await tester.pumpAndSettle();
39+
40+
expect(find.text(tabBarViewText1), findsNothing);
41+
expect(find.text(tabBarViewText2), findsOneWidget);
42+
expect(find.text(tabBarViewText3), findsNothing);
43+
44+
await tester.tap(tab3);
45+
await tester.pumpAndSettle();
46+
47+
expect(find.text(tabBarViewText1), findsNothing);
48+
expect(find.text(tabBarViewText2), findsNothing);
49+
expect(find.text(tabBarViewText3), findsOneWidget);
50+
});
51+
}

0 commit comments

Comments
 (0)