diff --git a/example/lib/pages/components/progress_example.dart b/example/lib/pages/components/progress_example.dart index ce35c3b5..b3f0bd19 100644 --- a/example/lib/pages/components/progress_example.dart +++ b/example/lib/pages/components/progress_example.dart @@ -13,11 +13,21 @@ class ProgressExample extends StatefulWidget { class ProgressExampleState extends State { GlobalKey key = GlobalKey(); + int stepsCompleted = 0; + late double progress; + + @override + void initState() { + super.initState(); + progress = stepsCompleted / 10; + } ///Function to increase percentage of progress. void increasePercentage() { - final circle = key.currentState as ProgressCircleState; - circle.increasePercentage(circle.progress + 0.1); + setState(() { + stepsCompleted++; + progress = stepsCompleted / 10; + }); } @override @@ -33,7 +43,7 @@ class ProgressExampleState extends State { mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ProgressCircle( - progress: 0, + progress: progress, key: key, size: CircleSizes.xl, border: ZetaWidgetBorder.sharp, diff --git a/lib/src/components/progress/progress_circle.dart b/lib/src/components/progress/progress_circle.dart index bc7adba6..a47c8648 100644 --- a/lib/src/components/progress/progress_circle.dart +++ b/lib/src/components/progress/progress_circle.dart @@ -1,6 +1,7 @@ import 'dart:math' as math; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import '../../../zeta_flutter.dart'; @@ -65,10 +66,12 @@ class ProgressCircleState extends State ///Animation for [ProgressCircleState] late Animation animation; + late final ValueNotifier _counter; @override void initState() { super.initState(); + _counter = ValueNotifier(widget.progress); progress = widget.progress; controller = AnimationController( vsync: this, duration: const Duration(milliseconds: 200)); @@ -91,13 +94,23 @@ class ProgressCircleState extends State ///Increase the progress percentage and animate to new progress percentage. void increasePercentage(double newProgress) { - // Update the Tween with new start and end values - animation = Tween( - begin: progress, - end: newProgress, - ).animate(controller); - controller.forward( - from: progress); // Start the animation from the beginning + // Update the Tween with new start and end value + + setState(() { + animation = Tween( + begin: progress, + end: newProgress, + ).animate(controller); + controller.forward(from: progress); + }); + } + + @override + void didUpdateWidget(ProgressCircle oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.progress != widget.progress) { + increasePercentage(widget.progress); + } } @override