diff --git a/packages/flutter/lib/src/widgets/scroll_activity.dart b/packages/flutter/lib/src/widgets/scroll_activity.dart index 64376b7dd5..d42d0374e2 100644 --- a/packages/flutter/lib/src/widgets/scroll_activity.dart +++ b/packages/flutter/lib/src/widgets/scroll_activity.dart @@ -63,6 +63,8 @@ abstract class ScrollActivity { ScrollActivityDelegate get delegate => _delegate; ScrollActivityDelegate _delegate; + bool _isDisposed = false; + /// Updates the activity's link to the [ScrollActivityDelegate]. /// /// This should only be called when an activity is being moved from a defunct @@ -134,7 +136,9 @@ abstract class ScrollActivity { /// Called when the scroll view stops performing this activity. @mustCallSuper - void dispose() { } + void dispose() { + _isDisposed = true; + } @override String toString() => describeIdentity(this); @@ -535,7 +539,7 @@ class BallisticScrollActivity extends ScrollActivity { ) ..addListener(_tick) ..animateWith(simulation) - .whenComplete(_end); // won't trigger if we dispose _controller first + .whenComplete(_end); // won't trigger if we dispose _controller before it completes. } late AnimationController _controller; @@ -569,7 +573,11 @@ class BallisticScrollActivity extends ScrollActivity { } void _end() { - delegate.goBallistic(0.0); + // Check if the activity was disposed before going ballistic because _end might be called + // if _controller is disposed just after completion. + if (!_isDisposed) { + delegate.goBallistic(0.0); + } } @override @@ -628,7 +636,7 @@ class DrivenScrollActivity extends ScrollActivity { ) ..addListener(_tick) ..animateTo(to, duration: duration, curve: curve) - .whenComplete(_end); // won't trigger if we dispose _controller first + .whenComplete(_end); // won't trigger if we dispose _controller before it completes. } late final Completer _completer; @@ -648,7 +656,11 @@ class DrivenScrollActivity extends ScrollActivity { } void _end() { - delegate.goBallistic(velocity); + // Check if the activity was disposed before going ballistic because _end might be called + // if _controller is disposed just after completion. + if (!_isDisposed) { + delegate.goBallistic(velocity); + } } @override diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 144fbaf3b3..098ad754c9 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -2224,6 +2224,57 @@ void main() { expect(tabController.index, 0); }); + testWidgets('On going TabBarView animation can be interrupted by a new animation', (WidgetTester tester) async { + // This is a regression test for https://github.com/flutter/flutter/issues/132293. + + final List tabs = ['A', 'B', 'C']; + final TabController tabController = TabController( + vsync: const TestVSync(), + length: tabs.length, + ); + 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')), + ], + ), + ), + ], + ), + )); + + // First page is visible. + expect(tabController.index, 0); + expect(find.text('0'), findsOneWidget); + expect(find.text('1'), findsNothing); + + // Animate to the second page. + tabController.animateTo(1); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 300)); + + // Animate back to the first page before the previous animation ends. + tabController.animateTo(0); + await tester.pumpAndSettle(); + + // First page should be visible. + expect(tabController.index, 0); + expect(find.text('0'), findsOneWidget); + expect(find.text('1'), findsNothing); + }); + testWidgets('Can switch to non-neighboring tab in nested TabBarView without crashing', (WidgetTester tester) async { // This is a regression test for https://github.com/flutter/flutter/issues/18756 final TabController mainTabController = _tabController(length: 4, vsync: const TestVSync());