diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index a255ee6a30..6842e398ae 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -2690,6 +2690,7 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto ShapeBorder? shape, Clip? clipBehavior, BoxConstraints? constraints, + bool shouldDisposeAnimationController = true, }) { assert(() { if (widget.bottomSheet != null && isPersistent && _currentBottomSheet != null) { @@ -2757,6 +2758,11 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto }); } }, + onDispose: () { + if (shouldDisposeAnimationController) { + animationController.dispose(); + } + }, builder: builder, isPersistent: isPersistent, backgroundColor: backgroundColor, @@ -2894,6 +2900,7 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto shape: shape, clipBehavior: clipBehavior, constraints: constraints, + shouldDisposeAnimationController: transitionAnimationController == null, ); }); return _currentBottomSheet! as PersistentBottomSheetController; @@ -3069,12 +3076,6 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto _snackBarTimer = null; _geometryNotifier.dispose(); - for (final _StandardBottomSheet bottomSheet in _dismissedBottomSheets) { - bottomSheet.animationController.dispose(); - } - if (_currentBottomSheet != null) { - _currentBottomSheet!._widget.animationController.dispose(); - } _floatingActionButtonMoveController.dispose(); _floatingActionButtonVisibilityController.dispose(); _scaffoldMessenger?._unregister(this); @@ -3587,12 +3588,14 @@ class _StandardBottomSheet extends StatefulWidget { this.shape, this.clipBehavior, this.constraints, + this.onDispose, }) : super(key: key); final AnimationController animationController; // we control it, but it must be disposed by whoever created it. final bool enableDrag; final VoidCallback? onClosing; final VoidCallback? onDismissed; + final VoidCallback? onDispose; final WidgetBuilder builder; final bool isPersistent; final Color? backgroundColor; @@ -3619,6 +3622,12 @@ class _StandardBottomSheetState extends State<_StandardBottomSheet> { widget.animationController.addStatusListener(_handleStatusChange); } + @override + void dispose() { + widget.onDispose?.call(); + super.dispose(); + } + @override void didUpdateWidget(_StandardBottomSheet oldWidget) { super.didUpdateWidget(oldWidget); diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index 5ec9233d51..966ebc47d9 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -895,6 +895,106 @@ void main() { expect(find.text('BottomSheet'), findsNothing); }); + // Regression test for https://github.com/flutter/flutter/issues/87708 + testWidgets('Each of the internal animation controllers should be disposed by the framework.', (WidgetTester tester) async { + final GlobalKey scaffoldKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + home: Scaffold( + key: scaffoldKey, + body: const Center(child: Text('body')), + ), + )); + + scaffoldKey.currentState!.showBottomSheet((_) { + return Builder( + builder: (BuildContext context) { + return Container(height: 200.0); + }, + ); + }); + + await tester.pump(); + expect(find.byType(BottomSheet), findsOneWidget); + + // The first sheet's animation is still running. + + // Trigger the second sheet will remove the first sheet from tree. + scaffoldKey.currentState!.showBottomSheet((_) { + return Builder( + builder: (BuildContext context) { + return Container(height: 200.0); + }, + ); + }); + await tester.pump(); + expect(find.byType(BottomSheet), findsOneWidget); + + // Remove the Scaffold from the tree. + await tester.pumpWidget(const SizedBox.shrink()); + + // If the internal animation controller do not dispose will throw + // FlutterError:( + context: context, + transitionAnimationController: controller, + builder: (BuildContext context) { + return MaterialButton( + onPressed: () => Navigator.pop(context), + key: tapTargetToClose, + child: 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)); // Open the sheet. + await tester.pumpAndSettle(); // Finish the animation. + expect(find.text('BottomSheet'), findsOneWidget); + + // Tapping button on the bottom sheet to dismiss it. + await tester.tap(find.byKey(tapTargetToClose)); // Closing the sheet. + await tester.pumpAndSettle(); // Finish the animation. + expect(find.text('BottomSheet'), findsNothing); + + await tester.pumpWidget(const SizedBox.shrink()); + controller.dispose(); + + // Double dispose will throw. + expect(tester.takeException(), isNull); + }); + group('constraints', () { testWidgets('No constraints by default for bottomSheet property', (WidgetTester tester) async {