From 95dd2544849d0484d31be0615c676dae94732048 Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Wed, 28 Aug 2024 15:53:30 +0200 Subject: [PATCH] fixed memory leak --- .../widgets/animate_gradient_shader.dart | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app/lib/presentation/widgets/animate_gradient_shader.dart b/app/lib/presentation/widgets/animate_gradient_shader.dart index 08ea2362..2b4b0726 100644 --- a/app/lib/presentation/widgets/animate_gradient_shader.dart +++ b/app/lib/presentation/widgets/animate_gradient_shader.dart @@ -91,8 +91,8 @@ class AnimateGradientShader extends StatefulWidget { class _AnimateGradientShaderState extends State with TickerProviderStateMixin { - late Animation _animation; - late AnimationController _controller; + AnimationController? _controller; + Animation? _animation; late List _colorTween; @@ -123,17 +123,21 @@ class _AnimateGradientShaderState extends State @override Widget build(BuildContext context) { + if (_animation == null) { + return const SizedBox.shrink(); + } + return AnimatedBuilder( - animation: _animation, + animation: _animation!, builder: (BuildContext context, Widget? child) { final gradient = LinearGradient( begin: widget.animateAlignments - ? begin.evaluate(_animation) + ? begin.evaluate(_animation!) : widget.primaryBegin, end: widget.animateAlignments - ? end.evaluate(_animation) + ? end.evaluate(_animation!) : widget.primaryEnd, - colors: _evaluateColors(_animation), + colors: _evaluateColors(_animation!), ); return ShaderMask( @@ -199,21 +203,23 @@ class _AnimateGradientShaderState extends State } void _setAnimations() { - _controller = widget.controller ?? + _controller?.dispose(); + _controller = (widget.controller ?? AnimationController( vsync: this, duration: widget.duration, - ) + )) ..repeat(reverse: widget.reverse); + _animation = CurvedAnimation( - parent: _controller, + parent: _controller!, curve: Curves.easeInOut, ); } @override void dispose() { - _controller.dispose(); + _controller?.dispose(); super.dispose(); } }