From e43850d197e09a7582f06f9023576ede7c46dfc9 Mon Sep 17 00:00:00 2001 From: Tom Gilder Date: Fri, 9 Sep 2022 08:35:07 +0300 Subject: [PATCH] Fix TabBarView with no animation not navigating (#110985) --- packages/flutter/lib/src/material/tabs.dart | 11 +- packages/flutter/test/material/tabs_test.dart | 102 ++++++++++++++++++ 2 files changed, 108 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index ee9db1dcea..053671e77a 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -1532,13 +1532,14 @@ class _TabBarViewState extends State { if (duration == Duration.zero) { _pageController.jumpToPage(_currentIndex!); - return Future.value(); + } else { + await _pageController.animateToPage(_currentIndex!, duration: duration, curve: Curves.ease); + + if (!mounted) { + return Future.value(); + } } - await _pageController.animateToPage(_currentIndex!, duration: duration, curve: Curves.ease); - if (!mounted) { - return Future.value(); - } setState(() { _warpUnderwayCount -= 1; if (widget.children != _children) { diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 473ccdf467..8c959e5562 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -1354,6 +1354,108 @@ void main() { expect(position.pixels, 800); }); + testWidgets('TabBarView skips animation when disabled in controller - skip tabs twice', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/110970 + final List tabs = ['A', 'B', 'C']; + final TabController tabController = TabController( + vsync: const TestVSync(), + length: tabs.length, + animationDuration: Duration.zero, + ); + await tester.pumpWidget(boilerplate( + child: Column( + children: [ + TabBar( + tabs: tabs.map((String tab) => Tab(text: tab)).toList(), + controller: tabController, + ), + SizedBox( + width: 400.0, + height: 400.0, + child: TabBarView( + controller: tabController, + children: const [ + Center(child: Text('0')), + Center(child: Text('1')), + Center(child: Text('2')), + ], + ), + ), + ], + ), + )); + + expect(tabController.index, 0); + + final PageView pageView = tester.widget(find.byType(PageView)); + final PageController pageController = pageView.controller; + final ScrollPosition position = pageController.position; + + // The TabBarView's page width is 400, so page 0 is at scroll offset 0.0, + // page 1 is at 400.0, page 2 is at 800.0. + expect(position.pixels, 0); + await tester.tap(find.text('C')); + await tester.pump(); + expect(position.pixels, 800); + + await tester.tap(find.text('A')); + await tester.pump(); + expect(position.pixels, 0); + }); + + testWidgets('TabBarView skips animation when disabled in controller - skip tabs followed by single tab navigation', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/110970 + final List tabs = ['A', 'B', 'C']; + final TabController tabController = TabController( + vsync: const TestVSync(), + length: tabs.length, + animationDuration: Duration.zero, + ); + await tester.pumpWidget(boilerplate( + child: Column( + children: [ + TabBar( + tabs: tabs.map((String tab) => Tab(text: tab)).toList(), + controller: tabController, + ), + SizedBox( + width: 400.0, + height: 400.0, + child: TabBarView( + controller: tabController, + children: const [ + Center(child: Text('0')), + Center(child: Text('1')), + Center(child: Text('2')), + ], + ), + ), + ], + ), + )); + + expect(tabController.index, 0); + + final PageView pageView = tester.widget(find.byType(PageView)); + final PageController pageController = pageView.controller; + final ScrollPosition position = pageController.position; + + // The TabBarView's page width is 400, so page 0 is at scroll offset 0.0, + // page 1 is at 400.0, page 2 is at 800.0. + expect(position.pixels, 0); + await tester.tap(find.text('C')); + await tester.pump(); + expect(position.pixels, 800); + + await tester.tap(find.text('B')); + await tester.pump(); + expect(position.pixels, 400); + + await tester.tap(find.text('A')); + await tester.pump(); + expect(position.pixels, 0); + }); + testWidgets('TabBarView skips animation when disabled in controller - two tabs', (WidgetTester tester) async { final List tabs = ['A', 'B']; final TabController tabController = TabController(