Skip to content

Commit dba4f77

Browse files
Fix memory leaks in BottomNavigationBar (#147213)
1 parent 2676c84 commit dba4f77

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/flutter/lib/src/material/bottom_navigation_bar.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ class _Label extends StatelessWidget {
802802

803803
class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerProviderStateMixin {
804804
List<AnimationController> _controllers = <AnimationController>[];
805-
late List<CurvedAnimation> _animations;
805+
List<CurvedAnimation> _animations = <CurvedAnimation>[];
806806

807807
// A queue of color splashes currently being animated.
808808
final Queue<_Circle> _circles = Queue<_Circle>();
@@ -820,6 +820,9 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
820820
for (final _Circle circle in _circles) {
821821
circle.dispose();
822822
}
823+
for (final CurvedAnimation animation in _animations) {
824+
animation.dispose();
825+
}
823826
_circles.clear();
824827

825828
_controllers = List<AnimationController>.generate(widget.items.length, (int index) {
@@ -1254,6 +1257,7 @@ class _Circle {
12541257

12551258
void dispose() {
12561259
controller.dispose();
1260+
animation.dispose();
12571261
}
12581262
}
12591263

packages/flutter/test/material/bottom_navigation_bar_test.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:flutter/foundation.dart';
1414
import 'package:flutter/material.dart';
1515
import 'package:flutter/rendering.dart';
1616
import 'package:flutter_test/flutter_test.dart';
17+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1718
import 'package:vector_math/vector_math_64.dart' show Vector3;
1819

1920
import '../widgets/semantics_tester.dart';
@@ -2187,7 +2188,10 @@ void main() {
21872188
);
21882189
});
21892190

2190-
testWidgets('BottomNavigationBar handles items.length changes', (WidgetTester tester) async {
2191+
testWidgets('BottomNavigationBar handles items.length changes',
2192+
// TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in]
2193+
experimentalLeakTesting: LeakTesting.settings.withTracked(classes: const <String>['CurvedAnimation']),
2194+
(WidgetTester tester) async {
21912195
// Regression test for https://github.com/flutter/flutter/issues/10322
21922196

21932197
Widget buildFrame(int itemCount) {
@@ -2322,7 +2326,10 @@ void main() {
23222326
);
23232327
}
23242328
for (int pump = 1; pump < 9; pump++) {
2325-
testWidgets('pump $pump', (WidgetTester tester) async {
2329+
testWidgets('pump $pump',
2330+
// TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in]
2331+
experimentalLeakTesting: LeakTesting.settings.withTracked(classes: const <String>['CurvedAnimation']).withCreationStackTrace(),
2332+
(WidgetTester tester) async {
23262333
await tester.pumpWidget(runTest());
23272334
await tester.tap(find.text('Green'));
23282335

0 commit comments

Comments
 (0)