From b0198426b5ec57572b2ffb94b958bb4528c144e2 Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Wed, 24 Apr 2024 02:14:19 +0800 Subject: [PATCH] Fix memory leak in switch painter (#147228) --- packages/flutter/lib/src/material/switch.dart | 8 +++++++- packages/flutter/test/material/reorderable_list_test.dart | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/switch.dart b/packages/flutter/lib/src/material/switch.dart index 35a3dae550..945bdb23d9 100644 --- a/packages/flutter/lib/src/material/switch.dart +++ b/packages/flutter/lib/src/material/switch.dart @@ -1066,9 +1066,14 @@ class _SwitchPainter extends ToggleablePainter { return; } _positionController = value; + _colorAnimation?.dispose(); + _colorAnimation = CurvedAnimation(parent: positionController, curve: Curves.easeOut, reverseCurve: Curves.easeIn); notifyListeners(); } + CurvedAnimation? _colorAnimation; + + Icon? get activeIcon => _activeIcon; Icon? _activeIcon; set activeIcon(Icon? value) { @@ -1516,7 +1521,7 @@ class _SwitchPainter extends ToggleablePainter { final double inset = thumbOffset == null ? 0 : 1.0 - (currentValue - thumbOffset!).abs() * 2.0; thumbSize = Size(thumbSize!.width - inset, thumbSize.height - inset); - final double colorValue = CurvedAnimation(parent: positionController, curve: Curves.easeOut, reverseCurve: Curves.easeIn).value; + final double colorValue = _colorAnimation!.value; final Color trackColor = Color.lerp(inactiveTrackColor, activeTrackColor, colorValue)!; final Color? trackOutlineColor = inactiveTrackOutlineColor == null || activeTrackOutlineColor == null ? null : Color.lerp(inactiveTrackOutlineColor, activeTrackOutlineColor, colorValue); @@ -1737,6 +1742,7 @@ class _SwitchPainter extends ToggleablePainter { _cachedThumbColor = null; _cachedThumbImage = null; _cachedThumbErrorListener = null; + _colorAnimation?.dispose(); super.dispose(); } } diff --git a/packages/flutter/test/material/reorderable_list_test.dart b/packages/flutter/test/material/reorderable_list_test.dart index 5595fe6448..1cf37df7f3 100644 --- a/packages/flutter/test/material/reorderable_list_test.dart +++ b/packages/flutter/test/material/reorderable_list_test.dart @@ -11,6 +11,7 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; void main() { group('$ReorderableListView', () { @@ -689,7 +690,10 @@ void main() { handle.dispose(); }); - testWidgets("Doesn't hide accessibility when a child declares its own semantics", (WidgetTester tester) async { + testWidgets("Doesn't hide accessibility when a child declares its own semantics", + // TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in] + experimentalLeakTesting: LeakTesting.settings.withTracked(classes: const ['CurvedAnimation']), + (WidgetTester tester) async { final SemanticsHandle handle = tester.ensureSemantics(); final Widget reorderableListView = ReorderableListView( onReorder: (int oldIndex, int newIndex) { },