Skip to content

Commit d39b10c

Browse files
committed
Fix memory leak in CurvedAnimation [Flutter core]
1 parent 24d892b commit d39b10c

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

packages/dropdown_button2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Remove 'must be non-null' and 'must not be null' comments [Flutter core].
66
- Form fields onChange callback should be called on reset [Flutter core].
77
- Implement switch expressions.
8+
- Fix memory leak in CurvedAnimation [Flutter core].
89

910
## 3.0.0-beta.21
1011

packages/dropdown_button2/lib/src/dropdown_menu.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class _DropdownMenu<T> extends StatefulWidget {
3838
}
3939

4040
class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
41-
late CurvedAnimation _fadeOpacity;
42-
late CurvedAnimation _resize;
41+
late final CurvedAnimation _fadeOpacity;
42+
late final CurvedAnimation _resize;
4343
late List<Widget> _children;
4444
late SearchMatchFn<T> _searchMatchFn;
4545

packages/dropdown_button2/lib/src/dropdown_menu_item.dart

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,46 @@ class _DropdownItemButton<T> extends StatefulWidget {
147147
}
148148

149149
class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
150+
late CurvedAnimation _opacityAnimation;
151+
152+
@override
153+
void initState() {
154+
super.initState();
155+
_setOpacityAnimation();
156+
}
157+
158+
@override
159+
void didUpdateWidget(_DropdownItemButton<T> oldWidget) {
160+
super.didUpdateWidget(oldWidget);
161+
if (oldWidget.itemIndex != widget.itemIndex ||
162+
oldWidget.route.animation != widget.route.animation ||
163+
oldWidget.route.selectedIndex != widget.route.selectedIndex ||
164+
widget.route.items.length != oldWidget.route.items.length ||
165+
widget.route.dropdownStyle.openInterval.end !=
166+
oldWidget.route.dropdownStyle.openInterval.end) {
167+
_opacityAnimation.dispose();
168+
_setOpacityAnimation();
169+
}
170+
}
171+
172+
@override
173+
void dispose() {
174+
_opacityAnimation.dispose();
175+
super.dispose();
176+
}
177+
178+
void _setOpacityAnimation() {
179+
final double menuCurveEnd = widget.route.dropdownStyle.openInterval.end;
180+
final double unit = 0.5 / (widget.route.items.length + 1.5);
181+
final double start =
182+
clampDouble(menuCurveEnd + (widget.itemIndex + 1) * unit, 0.0, 1.0);
183+
final double end = clampDouble(start + 1.5 * unit, 0.0, 1.0);
184+
_opacityAnimation = CurvedAnimation(
185+
parent: widget.route.animation!,
186+
curve: Interval(start, end),
187+
);
188+
}
189+
150190
void _handleFocusChange(bool focused) {
151191
final bool inTraditionalMode =
152192
switch (FocusManager.instance.highlightMode) {
@@ -197,15 +237,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
197237

198238
@override
199239
Widget build(BuildContext context) {
200-
final double menuCurveEnd = widget.route.dropdownStyle.openInterval.end;
201-
202240
final DropdownItem<T> dropdownItem = widget.route.items[widget.itemIndex];
203-
final double unit = 0.5 / (widget.route.items.length + 1.5);
204-
final double start =
205-
clampDouble(menuCurveEnd + (widget.itemIndex + 1) * unit, 0.0, 1.0);
206-
final double end = clampDouble(start + 1.5 * unit, 0.0, 1.0);
207-
final CurvedAnimation opacity = CurvedAnimation(
208-
parent: widget.route.animation!, curve: Interval(start, end));
209241

210242
Widget child = Container(
211243
padding: (menuItemStyle.padding ?? _kMenuItemPadding)
@@ -230,7 +262,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
230262
: child,
231263
);
232264
}
233-
child = FadeTransition(opacity: opacity, child: child);
265+
child = FadeTransition(opacity: _opacityAnimation, child: child);
234266
if (kIsWeb && dropdownItem.enabled) {
235267
child = Shortcuts(
236268
shortcuts: _webShortcuts,

0 commit comments

Comments
 (0)