Skip to content

Commit b397406

Browse files
authored
Let CupertinoTabScaffold's tab be programmatically selectable (#16040)
* Let CupertinoTabScaffold's tab be programmatically selectable * Re-use the tab bar's index instead * review
1 parent 68728e9 commit b397406

3 files changed

Lines changed: 93 additions & 7 deletions

File tree

‎packages/flutter/lib/src/cupertino/bottom_tab_bar.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ class CupertinoTabBar extends StatelessWidget implements PreferredSizeWidget {
4747
this.iconSize: 30.0,
4848
}) : assert(items != null),
4949
assert(items.length >= 2),
50+
assert(currentIndex != null),
5051
assert(0 <= currentIndex && currentIndex < items.length),
5152
assert(iconSize != null),
5253
super(key: key);
5354

5455
/// The interactive items laid out within the bottom navigation bar.
56+
///
57+
/// Must not be null.
5558
final List<BottomNavigationBarItem> items;
5659

5760
/// The callback that is called when a item is tapped.
@@ -62,6 +65,8 @@ class CupertinoTabBar extends StatelessWidget implements PreferredSizeWidget {
6265
final ValueChanged<int> onTap;
6366

6467
/// The index into [items] of the current active item.
68+
///
69+
/// Must not be null.
6570
final int currentIndex;
6671

6772
/// The background color of the tab bar. If it contains transparency, the
@@ -82,6 +87,8 @@ class CupertinoTabBar extends StatelessWidget implements PreferredSizeWidget {
8287
/// This value is used to to configure the [IconTheme] for the navigation
8388
/// bar. When a [BottomNavigationBarItem.icon] widget is not an [Icon] the widget
8489
/// should configure itself to match the icon theme's size and color.
90+
///
91+
/// Must not be null.
8592
final double iconSize;
8693

8794
/// True if the tab bar's background color has no transparency.

‎packages/flutter/lib/src/cupertino/tab_scaffold.dart‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ import 'bottom_tab_bar.dart';
8383
class CupertinoTabScaffold extends StatefulWidget {
8484
/// Creates a layout for applications with a tab bar at the bottom.
8585
///
86-
/// The [tabBar] and [tabBuilder] arguments must not be null.
86+
/// The [tabBar], [tabBuilder] and [currentTabIndex] arguments must not be null.
87+
///
88+
/// The [currentTabIndex] argument can be used to programmatically change the
89+
/// currently selected tab.
8790
const CupertinoTabScaffold({
8891
Key key,
8992
@required this.tabBar,
@@ -96,16 +99,20 @@ class CupertinoTabScaffold extends StatefulWidget {
9699
/// that lets the user switch between different tabs in the main content area
97100
/// when present.
98101
///
99-
/// When provided, [CupertinoTabBar.currentIndex] will be ignored and will
100-
/// be managed by the [CupertinoTabScaffold] to show the currently selected page
101-
/// as the active item index. If [CupertinoTabBar.onTap] is provided, it will
102-
/// still be called. [CupertinoTabScaffold] automatically also listen to the
102+
/// Setting and changing [CupertinoTabBar.currentIndex] programmatically will
103+
/// change the currently selected tab item in the [tabBar] as well as change
104+
/// the currently focused tab from the [tabBuilder].
105+
106+
/// If [CupertinoTabBar.onTap] is provided, it will still be called.
107+
/// [CupertinoTabScaffold] automatically also listen to the
103108
/// [CupertinoTabBar]'s `onTap` to change the [CupertinoTabBar]'s `currentIndex`
104109
/// and change the actively displayed tab in [CupertinoTabScaffold]'s own
105110
/// main content area.
106111
///
107112
/// If translucent, the main content may slide behind it.
108113
/// Otherwise, the main content's bottom margin will be offset by its height.
114+
///
115+
/// Must not be null.
109116
final CupertinoTabBar tabBar;
110117

111118
/// An [IndexedWidgetBuilder] that's called when tabs become active.
@@ -121,14 +128,30 @@ class CupertinoTabScaffold extends StatefulWidget {
121128
/// In that case, the child's [BuildContext]'s [MediaQuery] will have a
122129
/// bottom padding indicating the area of obstructing overlap from the
123130
/// [tabBar].
131+
///
132+
/// Must not be null.
124133
final IndexedWidgetBuilder tabBuilder;
125134

126135
@override
127136
_CupertinoTabScaffoldState createState() => new _CupertinoTabScaffoldState();
128137
}
129138

130139
class _CupertinoTabScaffoldState extends State<CupertinoTabScaffold> {
131-
int _currentPage = 0;
140+
int _currentPage;
141+
142+
@override
143+
void initState() {
144+
super.initState();
145+
_currentPage = widget.tabBar.currentIndex;
146+
}
147+
148+
@override
149+
void didUpdateWidget(CupertinoTabScaffold oldWidget) {
150+
super.didUpdateWidget(oldWidget);
151+
if (widget.tabBar.currentIndex != oldWidget.tabBar.currentIndex) {
152+
_currentPage = widget.tabBar.currentIndex;
153+
}
154+
}
132155

133156
@override
134157
Widget build(BuildContext context) {

‎packages/flutter/test/cupertino/tab_scaffold_test.dart‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,64 @@ void main() {
237237
1,
238238
);
239239
});
240+
241+
testWidgets('Programmatic tab switching', (WidgetTester tester) async {
242+
final List<int> tabsPainted = <int>[];
243+
244+
await tester.pumpWidget(
245+
new WidgetsApp(
246+
color: const Color(0xFFFFFFFF),
247+
builder: (BuildContext context, Widget child) {
248+
return new CupertinoTabScaffold(
249+
tabBar: _buildTabBar(),
250+
tabBuilder: (BuildContext context, int index) {
251+
return new CustomPaint(
252+
child: new Text('Page ${index + 1}'),
253+
painter: new TestCallbackPainter(
254+
onPaint: () { tabsPainted.add(index); }
255+
)
256+
);
257+
},
258+
);
259+
},
260+
),
261+
);
262+
263+
expect(tabsPainted, <int>[0]);
264+
265+
await tester.pumpWidget(
266+
new WidgetsApp(
267+
color: const Color(0xFFFFFFFF),
268+
builder: (BuildContext context, Widget child) {
269+
return new CupertinoTabScaffold(
270+
tabBar: _buildTabBar(selectedTab: 1), // Programmatically change the tab now.
271+
tabBuilder: (BuildContext context, int index) {
272+
return new CustomPaint(
273+
child: new Text('Page ${index + 1}'),
274+
painter: new TestCallbackPainter(
275+
onPaint: () { tabsPainted.add(index); }
276+
)
277+
);
278+
},
279+
);
280+
},
281+
),
282+
);
283+
284+
expect(tabsPainted, <int>[0, 1]);
285+
// onTap is not called when changing tabs programmatically.
286+
expect(selectedTabs, isEmpty);
287+
288+
// Can still tap out of the programmatically selected tab.
289+
await tester.tap(find.text('Tab 1'));
290+
await tester.pump();
291+
292+
expect(tabsPainted, <int>[0, 1, 0]);
293+
expect(selectedTabs, <int>[0]);
294+
});
240295
}
241296

242-
CupertinoTabBar _buildTabBar() {
297+
CupertinoTabBar _buildTabBar({ int selectedTab: 0 }) {
243298
return new CupertinoTabBar(
244299
items: const <BottomNavigationBarItem>[
245300
const BottomNavigationBarItem(
@@ -252,6 +307,7 @@ CupertinoTabBar _buildTabBar() {
252307
),
253308
],
254309
backgroundColor: CupertinoColors.white,
310+
currentIndex: selectedTab,
255311
onTap: (int newTab) => selectedTabs.add(newTab),
256312
);
257313
}

0 commit comments

Comments
 (0)