Skip to content

Commit 62099ac

Browse files
[material_ui] Dispose drag-end CurvedAnimation in modal bottom sheet (flutter#13167)
Fixes flutter/flutter#193944 `_ModalBottomSheetState.handleDragEnd` rebinds the sheet animation to a new `CurvedAnimation` on every drag end but never disposed the previous one, so its listeners accumulated on the route animation. This tracks the drag-end `CurvedAnimation` and disposes it in `handleDragStart`, when it is replaced, and in `dispose`. Adds a regression test that fails before the fix. ## Pre-Review Checklist
1 parent 3254d60 commit 62099ac

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎packages/material_ui/lib/src/bottom_sheet.dart‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ class _ModalBottomSheet<T> extends StatefulWidget {
685685
class _ModalBottomSheetState<T> extends State<_ModalBottomSheet<T>> {
686686
late final ProxyAnimation _sheetAnimation;
687687
late final CurvedAnimation _curvedSheetAnimation;
688+
// The CurvedAnimation created for the most recent drag end, tracked so it can
689+
// be disposed when it is replaced or the state is disposed.
690+
CurvedAnimation? _dragEndCurvedAnimation;
688691

689692
@override
690693
void initState() {
@@ -716,6 +719,7 @@ class _ModalBottomSheetState<T> extends State<_ModalBottomSheet<T>> {
716719
// Detach to avoid leaking listeners on the route animation.
717720
_sheetAnimation.parent = kAlwaysDismissedAnimation;
718721
_curvedSheetAnimation.dispose();
722+
_dragEndCurvedAnimation?.dispose();
719723
super.dispose();
720724
}
721725

@@ -734,6 +738,11 @@ class _ModalBottomSheetState<T> extends State<_ModalBottomSheet<T>> {
734738
void handleDragStart(DragStartDetails details) {
735739
// Allow the bottom sheet to track the user's finger accurately.
736740
_sheetAnimation.parent = widget.route.animation;
741+
// The CurvedAnimation from the previous drag end is no longer the parent of
742+
// [_sheetAnimation], so dispose it to avoid leaking its listeners on the
743+
// route animation.
744+
_dragEndCurvedAnimation?.dispose();
745+
_dragEndCurvedAnimation = null;
737746
}
738747

739748
void handleDragEnd(DragEndDetails details, {bool? isClosing}) {
@@ -742,7 +751,8 @@ class _ModalBottomSheetState<T> extends State<_ModalBottomSheet<T>> {
742751
// Rebind the animation using CurvedAnimation and Split so the
743752
// remaining transition continues smoothly from the exact point
744753
// where the drag gesture ended.
745-
_sheetAnimation.parent = CurvedAnimation(
754+
final CurvedAnimation? oldDragEndCurvedAnimation = _dragEndCurvedAnimation;
755+
_dragEndCurvedAnimation = CurvedAnimation(
746756
parent: widget.route.animation!,
747757
curve: Split(
748758
currentProgress,
@@ -753,6 +763,10 @@ class _ModalBottomSheetState<T> extends State<_ModalBottomSheet<T>> {
753763
endCurve: widget.animationStyle?.reverseCurve ?? _kModalBottomSheetCurve,
754764
),
755765
);
766+
_sheetAnimation.parent = _dragEndCurvedAnimation;
767+
// Dispose the previous drag-end CurvedAnimation only after detaching it
768+
// from [_sheetAnimation], so it is never disposed while still in use.
769+
oldDragEndCurvedAnimation?.dispose();
756770
}
757771

758772
@override
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes a listener leak in modal bottom sheets that accumulated animation listeners each time the sheet was dragged.
3+
version: patch

‎packages/material_ui/test/bottom_sheet_test.dart‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,68 @@ void main() {
19231923
expect(tester.takeException(), isNull);
19241924
});
19251925

1926+
// Regression test for https://github.com/flutter/flutter/issues/193944
1927+
testWidgets('Modal bottom sheet disposes the CurvedAnimations it creates while dragging', (
1928+
WidgetTester tester,
1929+
) async {
1930+
final created = <CurvedAnimation>[];
1931+
final disposed = <CurvedAnimation>{};
1932+
void listener(ObjectEvent event) {
1933+
final Object object = event.object;
1934+
if (object is CurvedAnimation) {
1935+
if (event is ObjectCreated) {
1936+
created.add(object);
1937+
} else if (event is ObjectDisposed) {
1938+
disposed.add(object);
1939+
}
1940+
}
1941+
}
1942+
1943+
FlutterMemoryAllocations.instance.addListener(listener);
1944+
addTearDown(() => FlutterMemoryAllocations.instance.removeListener(listener));
1945+
1946+
late BuildContext savedContext;
1947+
await tester.pumpWidget(
1948+
MaterialApp(
1949+
home: Builder(
1950+
builder: (BuildContext context) {
1951+
savedContext = context;
1952+
return const Center(child: Text('body'));
1953+
},
1954+
),
1955+
),
1956+
);
1957+
1958+
unawaited(
1959+
showModalBottomSheet<void>(
1960+
context: savedContext,
1961+
builder: (BuildContext context) => const SizedBox(height: 300, child: Text('BottomSheet')),
1962+
),
1963+
);
1964+
await tester.pumpAndSettle();
1965+
1966+
// Each non-closing drag rebinds the sheet animation to a new CurvedAnimation
1967+
// in handleDragEnd; these must not accumulate without being disposed.
1968+
for (var i = 0; i < 3; i++) {
1969+
await tester.drag(find.text('BottomSheet'), const Offset(0.0, 40.0));
1970+
await tester.pumpAndSettle();
1971+
}
1972+
1973+
// Dismiss the sheet and tear down the tree so all state is disposed.
1974+
await tester.pumpWidget(const SizedBox.shrink());
1975+
await tester.pumpAndSettle();
1976+
1977+
// Every CurvedAnimation created by the sheet must have been disposed; before
1978+
// the fix the drag-end animations leaked their listeners on the route
1979+
// animation.
1980+
expect(created, isNotEmpty);
1981+
expect(
1982+
created.where((CurvedAnimation animation) => !disposed.contains(animation)),
1983+
isEmpty,
1984+
reason: 'A CurvedAnimation created by the modal bottom sheet was not disposed.',
1985+
);
1986+
});
1987+
19261988
// Regression test for https://github.com/flutter/flutter/issues/99627
19271989
testWidgets('The old route entry should be removed when a new sheet popup', (
19281990
WidgetTester tester,

0 commit comments

Comments
 (0)