From a628e4f4223392c1fae55cb9a5c727f2d7f707e2 Mon Sep 17 00:00:00 2001 From: xubaolin Date: Wed, 11 Aug 2021 08:07:07 +0800 Subject: [PATCH] `showModalBottomSheet` should not dispose the controller provided by user (#87707) --- .../lib/src/material/bottom_sheet.dart | 10 ++- .../flutter/lib/src/material/scaffold.dart | 4 ++ packages/flutter/lib/src/widgets/routes.dart | 16 ++++- .../test/material/bottom_sheet_test.dart | 63 +++++++++++++++++++ 4 files changed, 90 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 868c4190a8..f42dfb09ef 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -497,7 +497,12 @@ class _ModalBottomSheetRoute extends PopupRoute { @override AnimationController createAnimationController() { assert(_animationController == null); - _animationController = transitionAnimationController ?? BottomSheet.createAnimationController(navigator!.overlay!); + if (transitionAnimationController != null) { + _animationController = transitionAnimationController; + willDisposeAnimationController = false; + } else { + _animationController = BottomSheet.createAnimationController(navigator!.overlay!); + } return _animationController!; } @@ -626,7 +631,8 @@ class _BottomSheetSuspendedCurve extends ParametricCurve { /// for more details). /// /// The [transitionAnimationController] controls the bottom sheet's entrance and -/// exit animations if provided. +/// exit animations. It's up to the owner of the controller to call +/// [AnimationController.dispose] when the controller is no longer needed. /// /// The optional `routeSettings` parameter sets the [RouteSettings] of the modal bottom sheet /// sheet. This is particularly useful in the case that a user wants to observe diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 4c2f3c68bf..a255ee6a30 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -2794,6 +2794,10 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto /// [ModalRoute] and a back button is added to the app bar of the [Scaffold] /// that closes the bottom sheet. /// + /// The [transitionAnimationController] controls the bottom sheet's entrance and + /// exit animations. It's up to the owner of the controller to call + /// [AnimationController.dispose] when the controller is no longer needed. + /// /// To create a persistent bottom sheet that is not a [LocalHistoryEntry] and /// does not add a back button to the enclosing Scaffold's app bar, use the /// [Scaffold.bottomSheet] constructor parameter. diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 3d305f6184..da09e42c32 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -149,9 +149,21 @@ abstract class TransitionRoute extends OverlayRoute { Animation? get secondaryAnimation => _secondaryAnimation; final ProxyAnimation _secondaryAnimation = ProxyAnimation(kAlwaysDismissedAnimation); + /// Whether to takeover the [controller] created by [createAnimationController]. + /// + /// If true, this route will call [AnimationController.dispose] when the + /// controller is no longer needed. + /// If false, the controller should be disposed by whoever owned it. + /// + /// It defaults to `true`. + bool willDisposeAnimationController = true; + /// Called to create the animation controller that will drive the transitions to /// this route from the previous one, and back to the previous route from this /// one. + /// + /// The returned controller will be disposed by [AnimationController.dispose] + /// if the [willDisposeAnimationController] is `true`. AnimationController createAnimationController() { assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); final Duration duration = transitionDuration; @@ -422,7 +434,9 @@ abstract class TransitionRoute extends OverlayRoute { void dispose() { assert(!_transitionCompleter.isCompleted, 'Cannot dispose a $runtimeType twice.'); _animation?.removeStatusListener(_handleStatusChanged); - _controller?.dispose(); + if (willDisposeAnimationController) { + _controller?.dispose(); + } _transitionCompleter.complete(_result); super.dispose(); } diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index ac465ca564..5ec9233d51 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -771,6 +771,69 @@ void main() { expect(find.text('BottomSheet'), findsNothing); }); + // Regression test for https://github.com/flutter/flutter/issues/87592 + testWidgets('the framework do not dispose the transitionAnimationController provided by user.', (WidgetTester tester) async { + const Key tapTarget = Key('tap-target'); + final AnimationController controller = AnimationController( + vsync: const TestVSync(), + duration: const Duration(seconds: 2), + reverseDuration: const Duration(seconds: 2), + ); + + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + return GestureDetector( + key: tapTarget, + onTap: () { + showModalBottomSheet( + context: context, + // The default duration and reverseDuration is 1 second + transitionAnimationController: controller, + builder: (BuildContext context) { + return const Text('BottomSheet'); + }, + ); + }, + behavior: HitTestBehavior.opaque, + child: const SizedBox( + height: 100.0, + width: 100.0, + ), + ); + }, + ), + ), + )); + + expect(find.text('BottomSheet'), findsNothing); + + await tester.tap(find.byKey(tapTarget)); // Opening animation will start after tapping + await tester.pump(); + + expect(find.text('BottomSheet'), findsOneWidget); + await tester.pump(const Duration(milliseconds: 2000)); + expect(find.text('BottomSheet'), findsOneWidget); + + // Tapping above the bottom sheet to dismiss it. + await tester.tapAt(const Offset(20.0, 20.0)); // Closing animation will start after tapping + await tester.pump(); + + expect(find.text('BottomSheet'), findsOneWidget); + await tester.pump(const Duration(milliseconds: 2000)); + // The bottom sheet should still be present at the very end of the animation. + expect(find.text('BottomSheet'), findsOneWidget); + + await tester.pump(const Duration(milliseconds: 1)); + // The bottom sheet should not be showing any longer. + expect(find.text('BottomSheet'), findsNothing); + + controller.dispose(); + // Double disposal will throw. + expect(tester.takeException(), isNull); + }); + testWidgets('Verify persistence BottomSheet use AnimationController if provided.', (WidgetTester tester) async { const Key tapTarget = Key('tap-target'); const Key tapTargetToClose = Key('tap-target-to-close');