Skip to content

Commit

Permalink
Add function return current percent value if animation is true (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
muonroi committed Mar 17, 2024
1 parent 522b5e9 commit ff41559
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 48 deletions.
5 changes: 5 additions & 0 deletions lib/circular_percent_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class CircularPercentIndicator extends StatefulWidget {
/// Set to true if you want to rotate linear gradient in accordance to the [startAngle].
final bool rotateLinearGradient;

/// Return current percent value if animation is true.
final Function(double value)? onPercentValue;

CircularPercentIndicator({
Key? key,
this.percent = 0.0,
Expand Down Expand Up @@ -134,6 +137,7 @@ class CircularPercentIndicator extends StatefulWidget {
this.widgetIndicator,
this.rotateLinearGradient = false,
this.progressBorderColor,
this.onPercentValue,
}) : super(key: key) {
if (linearGradient != null && progressColor != null) {
throw ArgumentError(
Expand Down Expand Up @@ -184,6 +188,7 @@ class _CircularPercentIndicatorState extends State<CircularPercentIndicator>
)..addListener(() {
setState(() {
_percent = _animation!.value;
widget.onPercentValue?.call(_percent);
});
if (widget.restartAnimation && _percent == 1.0) {
_animationController!.repeat(min: 0, max: 1.0);
Expand Down
99 changes: 51 additions & 48 deletions lib/linear_percent_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class LinearPercentIndicator extends StatefulWidget {
/// Display a widget indicator at the end of the progress. It only works when `animation` is true
final Widget? widgetIndicator;

/// Return current percent value if animation is true.
final Function(double value)? onPercentValue;

LinearPercentIndicator({
Key? key,
this.fillColor = Colors.transparent,
Expand Down Expand Up @@ -124,6 +127,7 @@ class LinearPercentIndicator extends StatefulWidget {
this.onAnimationEnd,
this.widgetIndicator,
this.progressBorderColor,
this.onPercentValue,
}) : super(key: key) {
if (linearGradient != null && progressColor != null) {
throw ArgumentError(
Expand Down Expand Up @@ -189,6 +193,7 @@ class _LinearPercentIndicatorState extends State<LinearPercentIndicator>
)..addListener(() {
setState(() {
_percent = _animation!.value;
widget.onPercentValue?.call(_percent);
});
if (widget.restartAnimation && _percent == 1.0) {
_animationController!.repeat(min: 0, max: 1.0);
Expand Down Expand Up @@ -253,56 +258,54 @@ class _LinearPercentIndicatorState extends State<LinearPercentIndicator>
final percentPositionedHorizontal =
_containerWidth * _percent - _indicatorWidth / 3;
//LayoutBuilder is used to get the size of the container where the widget is rendered
var containerWidget = LayoutBuilder(
builder: (context, constraints) {
_containerWidth = constraints.maxWidth;
_containerHeight = constraints.maxHeight;
return Container(
width: hasSetWidth ? widget.width : double.infinity,
height: widget.lineHeight,
padding: widget.padding,
child: Stack(
clipBehavior: Clip.none,
children: [
CustomPaint(
key: _containerKey,
painter: _LinearPainter(
isRTL: widget.isRTL,
progress: _percent,
progressColor: widget.progressColor,
linearGradient: widget.linearGradient,
backgroundColor: widget.backgroundColor,
barRadius: widget.barRadius ??
Radius.zero, // If radius is not defined, set it to zero
linearGradientBackgroundColor:
var containerWidget = LayoutBuilder(builder: (context, constraints) {
_containerWidth = constraints.maxWidth;
_containerHeight = constraints.maxHeight;
return Container(
width: hasSetWidth ? widget.width : double.infinity,
height: widget.lineHeight,
padding: widget.padding,
child: Stack(
clipBehavior: Clip.none,
children: [
CustomPaint(
key: _containerKey,
painter: _LinearPainter(
isRTL: widget.isRTL,
progress: _percent,
progressColor: widget.progressColor,
linearGradient: widget.linearGradient,
backgroundColor: widget.backgroundColor,
barRadius: widget.barRadius ??
Radius.zero, // If radius is not defined, set it to zero
linearGradientBackgroundColor:
widget.linearGradientBackgroundColor,
maskFilter: widget.maskFilter,
clipLinearGradient: widget.clipLinearGradient,
),
child: (widget.center != null)
? Center(child: widget.center)
: Container(),
),
if (widget.widgetIndicator != null && _indicatorWidth == 0)
Opacity(
opacity: 0.0,
key: _keyIndicator,
child: widget.widgetIndicator,
),
if (widget.widgetIndicator != null &&
_containerWidth > 0 &&
_indicatorWidth > 0)
Positioned(
right: widget.isRTL ? percentPositionedHorizontal : null,
left: !widget.isRTL ? percentPositionedHorizontal : null,
top: _containerHeight / 2 - _indicatorHeight,
child: widget.widgetIndicator!,
),
],
maskFilter: widget.maskFilter,
clipLinearGradient: widget.clipLinearGradient,
),
child: (widget.center != null)
? Center(child: widget.center)
: Container(),
),
);
}
);
if (widget.widgetIndicator != null && _indicatorWidth == 0)
Opacity(
opacity: 0.0,
key: _keyIndicator,
child: widget.widgetIndicator,
),
if (widget.widgetIndicator != null &&
_containerWidth > 0 &&
_indicatorWidth > 0)
Positioned(
right: widget.isRTL ? percentPositionedHorizontal : null,
left: !widget.isRTL ? percentPositionedHorizontal : null,
top: _containerHeight / 2 - _indicatorHeight,
child: widget.widgetIndicator!,
),
],
),
);
});

if (hasSetWidth) {
items.add(containerWidget);
Expand Down

0 comments on commit ff41559

Please sign in to comment.