From 6d4d012397d238964df3a306cd0258435a704b94 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Thu, 7 Dec 2023 22:05:18 +0100 Subject: [PATCH] ScrollDragController should dispatch creation and disposal events. (#138800) --- .../lib/src/widgets/scroll_activity.dart | 17 ++++++++++++++++- .../test/widgets/scroll_activity_test.dart | 13 +++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/scroll_activity.dart b/packages/flutter/lib/src/widgets/scroll_activity.dart index 626e3c48b9..d1155727a7 100644 --- a/packages/flutter/lib/src/widgets/scroll_activity.dart +++ b/packages/flutter/lib/src/widgets/scroll_activity.dart @@ -261,7 +261,17 @@ class ScrollDragController implements Drag { _retainMomentum = carriedVelocity != null && carriedVelocity != 0.0, _lastNonStationaryTimestamp = details.sourceTimeStamp, _kind = details.kind, - _offsetSinceLastStop = motionStartDistanceThreshold == null ? null : 0.0; + _offsetSinceLastStop = motionStartDistanceThreshold == null ? null : 0.0 { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectCreated( + library: 'package:flutter/widgets.dart', + className: '$ScrollDragController', + object: this, + ); + } + } /// The object that will actuate the scroll view as the user drags. ScrollActivityDelegate get delegate => _delegate; @@ -437,6 +447,11 @@ class ScrollDragController implements Drag { /// Called by the delegate when it is no longer sending events to this object. @mustCallSuper void dispose() { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectDisposed(object: this); + } _lastDetails = null; onDragCanceled?.call(); } diff --git a/packages/flutter/test/widgets/scroll_activity_test.dart b/packages/flutter/test/widgets/scroll_activity_test.dart index 1d41b48ded..2c5af1fb8d 100644 --- a/packages/flutter/test/widgets/scroll_activity_test.dart +++ b/packages/flutter/test/widgets/scroll_activity_test.dart @@ -222,6 +222,19 @@ void main() { areCreateAndDispose, ); }); + + test('$ScrollDragController dispatches memory events', () async { + await expectLater( + await memoryEvents( + () => ScrollDragController( + delegate: _ScrollActivityDelegate(), + details: DragStartDetails(), + ).dispose(), + ScrollDragController, + ), + areCreateAndDispose, + ); + }); } class PageView62209 extends StatefulWidget {