From 1f0730e67af4cfbe7ad76bbfd3cc0267ffe074cb Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Wed, 6 Sep 2023 15:07:07 -0700 Subject: [PATCH] DraggableScrollableActuator should dispose notifier. (#133917) --- .../widgets/draggable_scrollable_sheet.dart | 20 +++++++++++++++---- .../floating_action_button_location_test.dart | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index 8b2012f903..9c22977696 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -1015,12 +1015,12 @@ class _DraggableScrollableSheetScrollPosition extends ScrollPositionWithSingleCo /// in library users' code). Generally, it's easier to control the sheet /// directly by creating a controller and passing the controller to the sheet in /// its constructor (see [DraggableScrollableSheet.controller]). -class DraggableScrollableActuator extends StatelessWidget { +class DraggableScrollableActuator extends StatefulWidget { /// Creates a widget that can notify descendent [DraggableScrollableSheet]s /// to reset to their initial position. /// /// The [child] parameter is required. - DraggableScrollableActuator({ + const DraggableScrollableActuator({ super.key, required this.child, }); @@ -1031,7 +1031,6 @@ class DraggableScrollableActuator extends StatelessWidget { /// Must not be null. final Widget child; - final _ResetNotifier _notifier = _ResetNotifier(); /// Notifies any descendant [DraggableScrollableSheet] that it should reset /// to its initial position. @@ -1047,9 +1046,22 @@ class DraggableScrollableActuator extends StatelessWidget { return notifier._sendReset(); } + @override + State createState() => _DraggableScrollableActuatorState(); +} + +class _DraggableScrollableActuatorState extends State { + final _ResetNotifier _notifier = _ResetNotifier(); + @override Widget build(BuildContext context) { - return _InheritedResetNotifier(notifier: _notifier, child: child); + return _InheritedResetNotifier(notifier: _notifier, child: widget.child); + } + + @override + void dispose() { + _notifier.dispose(); + super.dispose(); } } diff --git a/packages/flutter/test/material/floating_action_button_location_test.dart b/packages/flutter/test/material/floating_action_button_location_test.dart index 5b87324bc5..b43449f243 100644 --- a/packages/flutter/test/material/floating_action_button_location_test.dart +++ b/packages/flutter/test/material/floating_action_button_location_test.dart @@ -415,7 +415,7 @@ void main() { expect(tester.getCenter(find.byType(FloatingActionButton)), const Offset(_leftOffsetX, _floatOffsetY)); }); - testWidgets('centerFloat', (WidgetTester tester) async { + testWidgetsWithLeakTracking('centerFloat', (WidgetTester tester) async { await tester.pumpWidget(_singleFabScaffold(FloatingActionButtonLocation.centerFloat)); expect(tester.getCenter(find.byType(FloatingActionButton)), const Offset(_centerOffsetX, _floatOffsetY)); @@ -1037,7 +1037,7 @@ void main() { ); }); - testWidgets('centerFloat', (WidgetTester tester) async { + testWidgetsWithLeakTracking('centerFloat', (WidgetTester tester) async { const Rect defaultRect = Rect.fromLTRB(372.0, 478.0, 428.0, 534.0); // Positioned relative to BottomNavigationBar const Rect bottomNavigationBarRect = Rect.fromLTRB(372.0, 422.0, 428.0, 478.0);