Skip to content

Commit

Permalink
Add an 'initialPercent' field to control the initial animation. (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erfa authored May 17, 2024
1 parent ff41559 commit 29b2a68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/circular_percent_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -179,11 +183,12 @@ class _CircularPercentIndicatorState extends State<CircularPercentIndicator>
@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(() {
Expand Down
7 changes: 6 additions & 1 deletion lib/linear_percent_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -185,10 +189,11 @@ class _LinearPercentIndicatorState extends State<LinearPercentIndicator>
}
});
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(() {
Expand Down

0 comments on commit 29b2a68

Please sign in to comment.