Fix memory leaks in BottomNavigationBar (#147213)

This commit is contained in:
Valentin Vignal 2024-04-25 01:11:56 +08:00 committed by GitHub
parent 2676c84a90
commit dba4f77474
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View File

@ -802,7 +802,7 @@ class _Label extends StatelessWidget {
class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerProviderStateMixin {
List<AnimationController> _controllers = <AnimationController>[];
late List<CurvedAnimation> _animations;
List<CurvedAnimation> _animations = <CurvedAnimation>[];
// A queue of color splashes currently being animated.
final Queue<_Circle> _circles = Queue<_Circle>();
@ -820,6 +820,9 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
for (final _Circle circle in _circles) {
circle.dispose();
}
for (final CurvedAnimation animation in _animations) {
animation.dispose();
}
_circles.clear();
_controllers = List<AnimationController>.generate(widget.items.length, (int index) {
@ -1254,6 +1257,7 @@ class _Circle {
void dispose() {
controller.dispose();
animation.dispose();
}
}

View File

@ -14,6 +14,7 @@ import 'package:flutter/foundation.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';
import 'package:vector_math/vector_math_64.dart' show Vector3;
import '../widgets/semantics_tester.dart';
@ -2187,7 +2188,10 @@ void main() {
);
});
testWidgets('BottomNavigationBar handles items.length changes', (WidgetTester tester) async {
testWidgets('BottomNavigationBar handles items.length changes',
// 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 {
// Regression test for https://github.com/flutter/flutter/issues/10322
Widget buildFrame(int itemCount) {
@ -2322,7 +2326,10 @@ void main() {
);
}
for (int pump = 1; pump < 9; pump++) {
testWidgets('pump $pump', (WidgetTester tester) async {
testWidgets('pump $pump',
// 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']).withCreationStackTrace(),
(WidgetTester tester) async {
await tester.pumpWidget(runTest());
await tester.tap(find.text('Green'));