From 459df3c62d9a4a3b60fb57a3ccd110391b286d97 Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Sun, 5 May 2024 09:33:42 +0800 Subject: [PATCH] Fix memory leaks in `CupertinoSwitch` (#147821) --- packages/flutter/lib/src/cupertino/switch.dart | 6 ++++-- packages/flutter/test/cupertino/switch_test.dart | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/switch.dart b/packages/flutter/lib/src/cupertino/switch.dart index b864fd5e98c..609e69d37b7 100644 --- a/packages/flutter/lib/src/cupertino/switch.dart +++ b/packages/flutter/lib/src/cupertino/switch.dart @@ -200,10 +200,10 @@ class _CupertinoSwitchState extends State with TickerProviderSt late HorizontalDragGestureRecognizer _drag; late AnimationController _positionController; - late CurvedAnimation position; + late final CurvedAnimation position; late AnimationController _reactionController; - late Animation _reaction; + late CurvedAnimation _reaction; late bool isFocused; @@ -424,6 +424,8 @@ class _CupertinoSwitchState extends State with TickerProviderSt _positionController.dispose(); _reactionController.dispose(); + position.dispose(); + _reaction.dispose(); super.dispose(); } } diff --git a/packages/flutter/test/cupertino/switch_test.dart b/packages/flutter/test/cupertino/switch_test.dart index 4ddfeffc12c..f44c774cccf 100644 --- a/packages/flutter/test/cupertino/switch_test.dart +++ b/packages/flutter/test/cupertino/switch_test.dart @@ -14,9 +14,13 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; void main() { - testWidgets('Switch can toggle on tap', (WidgetTester tester) async { + testWidgets('Switch can toggle on tap', + // 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 Key switchKey = UniqueKey(); bool value = false; await tester.pumpWidget(