Fix memory leak in switch painter (#147228)

This commit is contained in:
Valentin Vignal 2024-04-24 02:14:19 +08:00 committed by GitHub
parent b0524b354b
commit b0198426b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View file

@ -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();
}
}

View file

@ -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 <String>['CurvedAnimation']),
(WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
final Widget reorderableListView = ReorderableListView(
onReorder: (int oldIndex, int newIndex) { },