Skip to content

Commit

Permalink
Fix memory leaks in CupertinoSwitch (#147821)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinVignal authored May 5, 2024
1 parent 02d239a commit 459df3c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/flutter/lib/src/cupertino/switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt
late HorizontalDragGestureRecognizer _drag;

late AnimationController _positionController;
late CurvedAnimation position;
late final CurvedAnimation position;

late AnimationController _reactionController;
late Animation<double> _reaction;
late CurvedAnimation _reaction;

late bool isFocused;

Expand Down Expand Up @@ -424,6 +424,8 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt

_positionController.dispose();
_reactionController.dispose();
position.dispose();
_reaction.dispose();
super.dispose();
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/flutter/test/cupertino/switch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 <String>['CurvedAnimation']),
(WidgetTester tester) async {
final Key switchKey = UniqueKey();
bool value = false;
await tester.pumpWidget(
Expand Down

0 comments on commit 459df3c

Please sign in to comment.