From f0205354b4e7878848fa130a82193478b1450a02 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Thu, 22 Feb 2024 20:16:25 +0100 Subject: [PATCH] Add CurvedAnimation disposals in some widgets (#143790) Contributes to https://github.com/flutter/flutter/issues/141198 ### Description - Adds `CurvedAnimation` disposals to `material/chip.dart`, `material/input_decorator.dart`, `material/toggleable.dart`, `widgets/animated_switcher.dart`, `widgets/overscroll_indicator.dart`. --- packages/flutter/lib/src/material/chip.dart | 15 ++++++++++----- .../lib/src/material/input_decorator.dart | 9 ++++++--- .../flutter/lib/src/material/toggleable.dart | 16 ++++++++++------ .../lib/src/widgets/animated_switcher.dart | 9 ++++++--- .../lib/src/widgets/overscroll_indicator.dart | 8 +++++--- .../test/widgets/overscroll_indicator_test.dart | 3 --- .../widgets/scrollable_restoration_test.dart | 3 --- 7 files changed, 37 insertions(+), 26 deletions(-) diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index b417a7db4fd..5fdf1817135 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -902,11 +902,11 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid late AnimationController avatarDrawerController; late AnimationController deleteDrawerController; late AnimationController enableController; - late Animation checkmarkAnimation; - late Animation avatarDrawerAnimation; - late Animation deleteDrawerAnimation; - late Animation enableAnimation; - late Animation selectionFade; + late CurvedAnimation checkmarkAnimation; + late CurvedAnimation avatarDrawerAnimation; + late CurvedAnimation deleteDrawerAnimation; + late CurvedAnimation enableAnimation; + late CurvedAnimation selectionFade; bool get hasDeleteButton => widget.onDeleted != null; bool get hasAvatar => widget.avatar != null; @@ -993,6 +993,11 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid avatarDrawerController.dispose(); deleteDrawerController.dispose(); enableController.dispose(); + checkmarkAnimation.dispose(); + avatarDrawerAnimation.dispose(); + deleteDrawerAnimation.dispose(); + enableAnimation.dispose(); + selectionFade.dispose(); super.dispose(); } diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 4e659c7541a..136826b0e65 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -181,9 +181,9 @@ class _BorderContainerState extends State<_BorderContainer> with TickerProviderS late AnimationController _controller; late AnimationController _hoverColorController; - late Animation _borderAnimation; + late CurvedAnimation _borderAnimation; late _InputBorderTween _border; - late Animation _hoverAnimation; + late CurvedAnimation _hoverAnimation; late ColorTween _hoverColorTween; @override @@ -218,6 +218,8 @@ class _BorderContainerState extends State<_BorderContainer> with TickerProviderS void dispose() { _controller.dispose(); _hoverColorController.dispose(); + _borderAnimation.dispose(); + _hoverAnimation.dispose(); super.dispose(); } @@ -1902,7 +1904,7 @@ class InputDecorator extends StatefulWidget { class _InputDecoratorState extends State with TickerProviderStateMixin { late final AnimationController _floatingLabelController; - late final Animation _floatingLabelAnimation; + late final CurvedAnimation _floatingLabelAnimation; late final AnimationController _shakingLabelController; final _InputBorderGap _borderGap = _InputBorderGap(); static const OrdinalSortKey _kPrefixSemanticsSortOrder = OrdinalSortKey(0); @@ -1946,6 +1948,7 @@ class _InputDecoratorState extends State with TickerProviderStat @override void dispose() { _floatingLabelController.dispose(); + _floatingLabelAnimation.dispose(); _shakingLabelController.dispose(); _borderGap.dispose(); super.dispose(); diff --git a/packages/flutter/lib/src/material/toggleable.dart b/packages/flutter/lib/src/material/toggleable.dart index ecf19d37e45..4d1ebbd8fb2 100644 --- a/packages/flutter/lib/src/material/toggleable.dart +++ b/packages/flutter/lib/src/material/toggleable.dart @@ -68,8 +68,8 @@ mixin ToggleableStateMixin on TickerProviderStateMixin /// /// To paint the actual radial reaction, [ToggleablePainter.paintRadialReaction] /// may be used. - Animation get reaction => _reaction; - late Animation _reaction; + CurvedAnimation get reaction => _reaction; + late CurvedAnimation _reaction; /// Controls the radial reaction's opacity animation for hover changes. /// @@ -79,8 +79,8 @@ mixin ToggleableStateMixin on TickerProviderStateMixin /// /// To paint the actual radial reaction, [ToggleablePainter.paintRadialReaction] /// may be used. - Animation get reactionHoverFade => _reactionHoverFade; - late Animation _reactionHoverFade; + CurvedAnimation get reactionHoverFade => _reactionHoverFade; + late CurvedAnimation _reactionHoverFade; late AnimationController _reactionHoverFadeController; /// Controls the radial reaction's opacity animation for focus changes. @@ -90,8 +90,8 @@ mixin ToggleableStateMixin on TickerProviderStateMixin /// /// To paint the actual radial reaction, [ToggleablePainter.paintRadialReaction] /// may be used. - Animation get reactionFocusFade => _reactionFocusFade; - late Animation _reactionFocusFade; + CurvedAnimation get reactionFocusFade => _reactionFocusFade; + late CurvedAnimation _reactionFocusFade; late AnimationController _reactionFocusFadeController; /// Whether [value] of this control can be changed by user interaction. @@ -203,9 +203,13 @@ mixin ToggleableStateMixin on TickerProviderStateMixin @override void dispose() { _positionController.dispose(); + _position.dispose(); _reactionController.dispose(); + _reaction.dispose(); _reactionHoverFadeController.dispose(); + _reactionHoverFade.dispose(); _reactionFocusFadeController.dispose(); + _reactionFocusFade.dispose(); super.dispose(); } diff --git a/packages/flutter/lib/src/widgets/animated_switcher.dart b/packages/flutter/lib/src/widgets/animated_switcher.dart index 177ede77062..13ea82d7bf7 100644 --- a/packages/flutter/lib/src/widgets/animated_switcher.dart +++ b/packages/flutter/lib/src/widgets/animated_switcher.dart @@ -25,7 +25,7 @@ class _ChildEntry { final AnimationController controller; // The (curved) animation being used to drive the transition. - final Animation animation; + final CurvedAnimation animation; // The currently built transition for this child. Widget transition; @@ -308,7 +308,7 @@ class _AnimatedSwitcherState extends State with TickerProvider reverseDuration: widget.reverseDuration, vsync: this, ); - final Animation animation = CurvedAnimation( + final CurvedAnimation animation = CurvedAnimation( parent: controller, curve: widget.switchInCurve, reverseCurve: widget.switchOutCurve, @@ -331,7 +331,7 @@ class _AnimatedSwitcherState extends State with TickerProvider required Widget child, required AnimatedSwitcherTransitionBuilder builder, required AnimationController controller, - required Animation animation, + required CurvedAnimation animation, }) { final _ChildEntry entry = _ChildEntry( widgetChild: child, @@ -348,6 +348,7 @@ class _AnimatedSwitcherState extends State with TickerProvider _markChildWidgetCacheAsDirty(); }); controller.dispose(); + animation.dispose(); } }); return entry; @@ -376,9 +377,11 @@ class _AnimatedSwitcherState extends State with TickerProvider void dispose() { if (_currentEntry != null) { _currentEntry!.controller.dispose(); + _currentEntry!.animation.dispose(); } for (final _ChildEntry entry in _outgoingEntries) { entry.controller.dispose(); + entry.animation.dispose(); } super.dispose(); } diff --git a/packages/flutter/lib/src/widgets/overscroll_indicator.dart b/packages/flutter/lib/src/widgets/overscroll_indicator.dart index 37f96e3ff72..ab925cd20f4 100644 --- a/packages/flutter/lib/src/widgets/overscroll_indicator.dart +++ b/packages/flutter/lib/src/widgets/overscroll_indicator.dart @@ -313,12 +313,12 @@ class _GlowController extends ChangeNotifier { } _glowController = AnimationController(vsync: vsync) ..addStatusListener(_changePhase); - final Animation decelerator = CurvedAnimation( + _decelerator = CurvedAnimation( parent: _glowController, curve: Curves.decelerate, )..addListener(notifyListeners); - _glowOpacity = decelerator.drive(_glowOpacityTween); - _glowSize = decelerator.drive(_glowSizeTween); + _glowOpacity = _decelerator.drive(_glowOpacityTween); + _glowSize = _decelerator.drive(_glowSizeTween); _displacementTicker = vsync.createTicker(_tickDisplacement); } @@ -330,6 +330,7 @@ class _GlowController extends ChangeNotifier { double _paintOffsetScrollPixels = 0.0; // animation values + late final CurvedAnimation _decelerator; final Tween _glowOpacityTween = Tween(begin: 0.0, end: 0.0); late final Animation _glowOpacity; final Tween _glowSizeTween = Tween(begin: 0.0, end: 0.0); @@ -383,6 +384,7 @@ class _GlowController extends ChangeNotifier { @override void dispose() { _glowController.dispose(); + _decelerator.dispose(); _displacementTicker.dispose(); _pullRecedeTimer?.cancel(); super.dispose(); diff --git a/packages/flutter/test/widgets/overscroll_indicator_test.dart b/packages/flutter/test/widgets/overscroll_indicator_test.dart index b647416216e..c0234f89b5d 100644 --- a/packages/flutter/test/widgets/overscroll_indicator_test.dart +++ b/packages/flutter/test/widgets/overscroll_indicator_test.dart @@ -6,7 +6,6 @@ import 'dart:math' as math; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; final Matcher doesNotOverscroll = isNot(paints..circle()); @@ -293,8 +292,6 @@ void main() { }); testWidgets('Nested overscrolls do not throw exceptions', - // TODO(polina-c): clean up leaks, https://github.com/flutter/flutter/issues/134787 [leaks-to-clean] - experimentalLeakTesting: LeakTesting.settings.withIgnoredAll(), (WidgetTester tester) async { await tester.pumpWidget(Directionality( textDirection: TextDirection.ltr, diff --git a/packages/flutter/test/widgets/scrollable_restoration_test.dart b/packages/flutter/test/widgets/scrollable_restoration_test.dart index d0b72937c49..f22708bc3c4 100644 --- a/packages/flutter/test/widgets/scrollable_restoration_test.dart +++ b/packages/flutter/test/widgets/scrollable_restoration_test.dart @@ -5,7 +5,6 @@ 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'; void main() { testWidgets('CustomScrollView restoration', (WidgetTester tester) async { @@ -264,8 +263,6 @@ void main() { }); testWidgets('PageView restoration', - // TODO(polina-c): clean up leaks, https://github.com/flutter/flutter/issues/134787 [leaks-to-clean] - experimentalLeakTesting: LeakTesting.settings.withIgnoredAll(), (WidgetTester tester) async { await tester.pumpWidget( TestHarness(