Skip to content

Commit

Permalink
Fix memory leaks in BottomNavigationBar (#147213)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinVignal authored Apr 24, 2024
1 parent 2676c84 commit dba4f77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/flutter/lib/src/material/bottom_navigation_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand All @@ -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) {
Expand Down Expand Up @@ -1254,6 +1257,7 @@ class _Circle {

void dispose() {
controller.dispose();
animation.dispose();
}
}

Expand Down
11 changes: 9 additions & 2 deletions packages/flutter/test/material/bottom_navigation_bar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'));

Expand Down

0 comments on commit dba4f77

Please sign in to comment.