From 082ad11422dae822c47808337e18a9d2af5734eb Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Mon, 11 Jul 2022 11:40:07 +0200 Subject: [PATCH] Fix TabController throws build scheduled during frame error (#105442) --- packages/flutter/lib/src/material/tabs.dart | 15 ++++--- packages/flutter/test/material/tabs_test.dart | 45 +++++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 20af1ec3b2..10daa6af48 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -1495,15 +1495,13 @@ class _TabBarViewState extends State { } final Duration duration = _controller!.animationDuration; - - if (duration == Duration.zero) { - _pageController.jumpToPage(_currentIndex!); - return Future.value(); - } - final int previousIndex = _controller!.previousIndex; if ((_currentIndex! - previousIndex).abs() == 1) { + if (duration == Duration.zero) { + _pageController.jumpToPage(_currentIndex!); + return Future.value(); + } _warpUnderwayCount += 1; await _pageController.animateToPage(_currentIndex!, duration: duration, curve: Curves.ease); _warpUnderwayCount -= 1; @@ -1525,6 +1523,11 @@ class _TabBarViewState extends State { }); _pageController.jumpToPage(initialPage); + if (duration == Duration.zero) { + _pageController.jumpToPage(_currentIndex!); + return Future.value(); + } + await _pageController.animateToPage(_currentIndex!, duration: duration, curve: Curves.ease); if (!mounted) { return Future.value(); diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 1432f0c5a9..a8cd7e3d09 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -1191,6 +1191,51 @@ void main() { expect(controller.indexIsChanging, false); }); + testWidgets('TabBar should not throw when animation is disabled in controller', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/102600 + final List tabs = ['A']; + + Widget buildWithTabBarView() { + return boilerplate( + child: DefaultTabController( + animationDuration: Duration.zero, + length: tabs.length, + child: Column( + children: [ + TabBar( + tabs: tabs.map((String tab) => Tab(text: tab)).toList(), + isScrollable: true, + ), + Flexible( + child: TabBarView( + children: List.generate( + tabs.length, + (int index) => Text('Tab $index'), + ), + ), + ), + ], + ), + ), + ); + } + + await tester.pumpWidget(buildWithTabBarView()); + TabController controller = DefaultTabController.of(tester.element(find.text('A')))!; + expect(controller.index, 0); + + tabs.add('B'); + await tester.pumpWidget(buildWithTabBarView()); + tabs.add('C'); + await tester.pumpWidget(buildWithTabBarView()); + await tester.tap(find.text('C')); + await tester.pumpAndSettle(); + controller = DefaultTabController.of(tester.element(find.text('A')))!; + expect(controller.index, 2); + + expect(tester.takeException(), isNull); + }); + testWidgets('TabBarView skips animation when disabled in controller', (WidgetTester tester) async { final List tabs = ['A', 'B', 'C']; final TabController tabController = TabController(