From 29b2a6859854c79dbc32a268a2a4ec4b3e9efc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fahl=C3=A9n?= Date: Fri, 17 May 2024 22:16:59 +0200 Subject: [PATCH] Add an 'initialPercent' field to control the initial animation. (#209) --- lib/circular_percent_indicator.dart | 7 ++++++- lib/linear_percent_indicator.dart | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/circular_percent_indicator.dart b/lib/circular_percent_indicator.dart index 91206fd..65685d6 100644 --- a/lib/circular_percent_indicator.dart +++ b/lib/circular_percent_indicator.dart @@ -73,6 +73,9 @@ class CircularPercentIndicator extends StatefulWidget { /// set true if you want to animate the linear from the last percent value you set final bool animateFromLastPercent; + /// set to false if you do not want the default behavior of initially animating up from 0% + final bool animateToInitialPercent; + /// set false if you don't want to preserve the state of the widget final bool addAutomaticKeepAlive; @@ -129,6 +132,7 @@ class CircularPercentIndicator extends StatefulWidget { this.arcBackgroundColor, this.arcType, this.animateFromLastPercent = false, + this.animateToInitialPercent = true, this.reverse = false, this.curve = Curves.linear, this.maskFilter, @@ -179,11 +183,12 @@ class _CircularPercentIndicatorState extends State @override void initState() { if (widget.animation) { + if (!widget.animateToInitialPercent) _percent = widget.percent; _animationController = AnimationController( vsync: this, duration: Duration(milliseconds: widget.animationDuration), ); - _animation = Tween(begin: 0.0, end: widget.percent).animate( + _animation = Tween(begin: _percent, end: widget.percent).animate( CurvedAnimation(parent: _animationController!, curve: widget.curve), )..addListener(() { setState(() { diff --git a/lib/linear_percent_indicator.dart b/lib/linear_percent_indicator.dart index 399583b..6790aa5 100644 --- a/lib/linear_percent_indicator.dart +++ b/lib/linear_percent_indicator.dart @@ -64,6 +64,9 @@ class LinearPercentIndicator extends StatefulWidget { /// set true if you want to animate the linear from the last percent value you set final bool animateFromLastPercent; + /// set to false if you do not want the default behavior of initially animating up from 0% + final bool animateToInitialPercent; + /// If present, this will make the progress bar colored by this gradient. /// /// This will override [progressColor]. It is an error to provide both. @@ -111,6 +114,7 @@ class LinearPercentIndicator extends StatefulWidget { this.animation = false, this.animationDuration = 500, this.animateFromLastPercent = false, + this.animateToInitialPercent = true, this.isRTL = false, this.leading, this.trailing, @@ -185,10 +189,11 @@ class _LinearPercentIndicatorState extends State } }); if (widget.animation) { + if (!widget.animateToInitialPercent) _percent = widget.percent; _animationController = AnimationController( vsync: this, duration: Duration(milliseconds: widget.animationDuration)); - _animation = Tween(begin: 0.0, end: widget.percent).animate( + _animation = Tween(begin: _percent, end: widget.percent).animate( CurvedAnimation(parent: _animationController!, curve: widget.curve), )..addListener(() { setState(() {