@@ -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