From 4938403a7a1755ab6d34414176c1a7880b7caa3a Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:40:33 +0800 Subject: [PATCH] Fix memory leaks in `PopupMenu` (#147174) --- .../flutter/lib/src/material/popup_menu.dart | 98 ++++++++++++++----- .../test/material/inherited_theme_test.dart | 6 +- 2 files changed, 77 insertions(+), 27 deletions(-) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 0dad9a9d00..87409aa1a1 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -572,7 +572,7 @@ class _CheckedPopupMenuItemState extends PopupMenuItemState extends StatelessWidget { +class _PopupMenu extends StatefulWidget { const _PopupMenu({ super.key, required this.itemKeys, @@ -588,23 +588,69 @@ class _PopupMenu extends StatelessWidget { final BoxConstraints? constraints; final Clip clipBehavior; + @override + State<_PopupMenu> createState() => _PopupMenuState(); +} + +class _PopupMenuState extends State<_PopupMenu> { + List _opacities = const []; + + @override + void initState() { + super.initState(); + _setOpacities(); + } + + @override + void didUpdateWidget(covariant _PopupMenu oldWidget) { + super.didUpdateWidget(oldWidget); + if ( + oldWidget.route.items.length != widget.route.items.length || + oldWidget.route.animation != widget.route.animation + ) { + _setOpacities(); + } + + } + + void _setOpacities() { + for (final CurvedAnimation opacity in _opacities) { + opacity.dispose(); + } + final List newOpacities = []; + final double unit = 1.0 / (widget.route.items.length + 1.5); // 1.0 for the width and 0.5 for the last item's fade. + for (int i = 0; i < widget.route.items.length; i += 1) { + final double start = (i + 1) * unit; + final double end = clampDouble(start + 1.5 * unit, 0.0, 1.0); + final CurvedAnimation opacity = CurvedAnimation( + parent: widget.route.animation!, + curve: Interval(start, end), + ); + newOpacities.add(opacity); + } + _opacities = newOpacities; + } + + @override + void dispose() { + for (final CurvedAnimation opacity in _opacities) { + opacity.dispose(); + } + super.dispose(); + } + @override Widget build(BuildContext context) { - final double unit = 1.0 / (route.items.length + 1.5); // 1.0 for the width and 0.5 for the last item's fade. + final double unit = 1.0 / (widget.route.items.length + 1.5); // 1.0 for the width and 0.5 for the last item's fade. final List children = []; final ThemeData theme = Theme.of(context); final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context); final PopupMenuThemeData defaults = theme.useMaterial3 ? _PopupMenuDefaultsM3(context) : _PopupMenuDefaultsM2(context); - for (int i = 0; i < route.items.length; i += 1) { - final double start = (i + 1) * unit; - final double end = clampDouble(start + 1.5 * unit, 0.0, 1.0); - final CurvedAnimation opacity = CurvedAnimation( - parent: route.animation!, - curve: Interval(start, end), - ); - Widget item = route.items[i]; - if (route.initialValue != null && route.items[i].represents(route.initialValue)) { + for (int i = 0; i < widget.route.items.length; i += 1) { + final CurvedAnimation opacity = _opacities[i]; + Widget item = widget.route.items[i]; + if (widget.route.initialValue != null && widget.route.items[i].represents(widget.route.initialValue)) { item = ColoredBox( color: Theme.of(context).highlightColor, child: item, @@ -613,10 +659,10 @@ class _PopupMenu extends StatelessWidget { children.add( _MenuItem( onLayout: (Size size) { - route.itemSizes[i] = size; + widget.route.itemSizes[i] = size; }, child: FadeTransition( - key: itemKeys[i], + key: widget.itemKeys[i], opacity: opacity, child: item, ), @@ -626,10 +672,10 @@ class _PopupMenu extends StatelessWidget { final CurveTween opacity = CurveTween(curve: const Interval(0.0, 1.0 / 3.0)); final CurveTween width = CurveTween(curve: Interval(0.0, unit)); - final CurveTween height = CurveTween(curve: Interval(0.0, unit * route.items.length)); + final CurveTween height = CurveTween(curve: Interval(0.0, unit * widget.route.items.length)); final Widget child = ConstrainedBox( - constraints: constraints ?? const BoxConstraints( + constraints: widget.constraints ?? const BoxConstraints( minWidth: _kMenuMinWidth, maxWidth: _kMenuMaxWidth, ), @@ -639,7 +685,7 @@ class _PopupMenu extends StatelessWidget { scopesRoute: true, namesRoute: true, explicitChildNodes: true, - label: semanticLabel, + label: widget.semanticLabel, child: SingleChildScrollView( padding: const EdgeInsets.symmetric( vertical: _kMenuVerticalPadding, @@ -651,22 +697,22 @@ class _PopupMenu extends StatelessWidget { ); return AnimatedBuilder( - animation: route.animation!, + animation: widget.route.animation!, builder: (BuildContext context, Widget? child) { return FadeTransition( - opacity: opacity.animate(route.animation!), + opacity: opacity.animate(widget.route.animation!), child: Material( - shape: route.shape ?? popupMenuTheme.shape ?? defaults.shape, - color: route.color ?? popupMenuTheme.color ?? defaults.color, - clipBehavior: clipBehavior, + shape: widget.route.shape ?? popupMenuTheme.shape ?? defaults.shape, + color: widget.route.color ?? popupMenuTheme.color ?? defaults.color, + clipBehavior: widget.clipBehavior, type: MaterialType.card, - elevation: route.elevation ?? popupMenuTheme.elevation ?? defaults.elevation!, - shadowColor: route.shadowColor ?? popupMenuTheme.shadowColor ?? defaults.shadowColor, - surfaceTintColor: route.surfaceTintColor ?? popupMenuTheme.surfaceTintColor ?? defaults.surfaceTintColor, + elevation: widget.route.elevation ?? popupMenuTheme.elevation ?? defaults.elevation!, + shadowColor: widget.route.shadowColor ?? popupMenuTheme.shadowColor ?? defaults.shadowColor, + surfaceTintColor: widget.route.surfaceTintColor ?? popupMenuTheme.surfaceTintColor ?? defaults.surfaceTintColor, child: Align( alignment: AlignmentDirectional.topEnd, - widthFactor: width.evaluate(route.animation!), - heightFactor: height.evaluate(route.animation!), + widthFactor: width.evaluate(widget.route.animation!), + heightFactor: height.evaluate(widget.route.animation!), child: child, ), ), diff --git a/packages/flutter/test/material/inherited_theme_test.dart b/packages/flutter/test/material/inherited_theme_test.dart index a36a6fec1c..31ebd1e646 100644 --- a/packages/flutter/test/material/inherited_theme_test.dart +++ b/packages/flutter/test/material/inherited_theme_test.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; void main() { testWidgets('Theme.wrap()', (WidgetTester tester) async { @@ -145,7 +146,10 @@ void main() { await tester.pumpAndSettle(); // menu route animation }); - testWidgets('Material3 - PopupMenuTheme.wrap()', (WidgetTester tester) async { + testWidgets('Material3 - PopupMenuTheme.wrap()', + // TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in] + experimentalLeakTesting: LeakTesting.settings.withTracked(classes: ['CurvedAnimation']), + (WidgetTester tester) async { const TextStyle textStyle = TextStyle(fontSize: 24.0, color: Color(0xFF0000FF)); Widget buildFrame() {