Fix memory leaks in CupertinoSwitch (#147821)

This commit is contained in:
Valentin Vignal 2024-05-05 09:33:42 +08:00 committed by GitHub
parent 02d239a098
commit 459df3c62d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View File

@ -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;
@ -424,6 +424,8 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt
_positionController.dispose();
_reactionController.dispose();
position.dispose();
_reaction.dispose();
super.dispose();
}
}

View File

@ -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(