Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(UX-1247): extend ZetaProgressCircle #199

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ Widget progressCircleUseCase(BuildContext context) => WidgetbookScaffold(
labelBuilder: enumLabelBuilder,
),
onCancel: context.knobs.boolean(label: "Can Cancel") ? () {} : null,
label: context.knobs.stringOrNull(label: 'Label'),
DE7924 marked this conversation as resolved.
Show resolved Hide resolved
),
);
17 changes: 13 additions & 4 deletions lib/src/components/progress/progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ abstract class ZetaProgress extends ZetaStatefulWidget {
super.key,
super.rounded,
this.progress = 0,
this.maxValue = 1,
});

/// ZetaProgress value, decimal value ranging from 0.0 - 1.0, 0.5 = 50%
final double progress;

/// Maximum value for progress, defaults to 1
final double maxValue;

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DoubleProperty('progress', progress));
properties
..add(DoubleProperty('progress', progress))
..add(DoubleProperty('maxValue', maxValue));
}
}

Expand Down Expand Up @@ -48,7 +55,7 @@ abstract class ZetaProgressState<T extends ZetaProgress> extends State<T> with T
).animate(controller)
..addListener(() {
setState(() {
progress = animation.value;
progress = animation.value / widget.maxValue;
});
});
}
Expand All @@ -65,10 +72,12 @@ abstract class ZetaProgressState<T extends ZetaProgress> extends State<T> with T

setState(() {
animation = Tween<double>(
begin: progress,
begin: progress * widget.maxValue,
end: newProgress,
).animate(controller);
controller.forward(from: progress);
controller
..reset()
..forward(from: 0);
});
}

Expand Down
41 changes: 34 additions & 7 deletions lib/src/components/progress/progress_circle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ class ZetaProgressCircle extends ZetaProgress {
const ZetaProgressCircle({
super.key,
super.progress = 0,
super.maxValue = 1,
this.size = ZetaCircleSizes.xl,
super.rounded,
this.onCancel,
this.label,
});

///Size of [ZetaProgressCircle]
Expand All @@ -45,6 +47,9 @@ class ZetaProgressCircle extends ZetaProgress {
/// Cancel function => cancel upload.
final VoidCallback? onCancel;

/// Label for [ZetaProgressCircle], override default percentage label.
final String? label;

@override
State<ZetaProgressCircle> createState() => _ZetaProgressCircleState();

Expand All @@ -55,7 +60,9 @@ class ZetaProgressCircle extends ZetaProgress {
..add(EnumProperty<ZetaCircleSizes>('size', size))
..add(DoubleProperty('progress', progress))
..add(DiagnosticsProperty<bool>('rounded', rounded))
..add(ObjectFlagProperty<VoidCallback?>.has('onCancel', onCancel));
..add(ObjectFlagProperty<VoidCallback?>.has('onCancel', onCancel))
..add(DoubleProperty('maxValue', maxValue))
..add(StringProperty('label', label));
}
}

Expand Down Expand Up @@ -85,13 +92,11 @@ class _ZetaProgressCircleState extends ZetaProgressState<ZetaProgressCircle> {

@override
Widget build(BuildContext context) {
final textVal = '${(widget.progress * 100).round()}%';
final textVal = widget.label ?? '${(widget.progress * 100).round()}%';
final colors = Zeta.of(context).colors;
final textWidget = Text(
textVal,
style: widget.size != ZetaCircleSizes.s
? ZetaTextStyles.labelSmall
: ZetaTextStyles.labelSmall.copyWith(fontSize: Zeta.of(context).spacing.small),
style: _getTextSize(),
);

return ConstrainedBox(
Expand All @@ -105,6 +110,7 @@ class _ZetaProgressCircleState extends ZetaProgressState<ZetaProgressCircle> {
progress: animation.value,
rounded: context.rounded,
context: context,
maxValue: widget.maxValue,
),
child: Center(
child: widget.size == ZetaCircleSizes.xs
Expand Down Expand Up @@ -168,6 +174,19 @@ class _ZetaProgressCircleState extends ZetaProgressState<ZetaProgressCircle> {
}
}

TextStyle _getTextSize() {
switch (widget.size) {
case ZetaCircleSizes.xs:
case ZetaCircleSizes.s:
return ZetaTextStyles.labelSmall;
case ZetaCircleSizes.m:
return ZetaTextStyles.labelMedium;
case ZetaCircleSizes.l:
case ZetaCircleSizes.xl:
return ZetaTextStyles.labelLarge;
}
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
Expand All @@ -185,7 +204,12 @@ typedef CirclePainter = _CirclePainter;
/// Class definition for [_CirclePainter]
class _CirclePainter extends CustomPainter {
///Constructor for [_CirclePainter]
_CirclePainter({this.progress = 0, this.rounded = true, required this.context});
_CirclePainter({
this.progress = 0,
this.rounded = true,
required this.context,
this.maxValue = 1,
});

///Percentage of progress in decimal value, defaults to 0
final double progress;
Expand All @@ -195,6 +219,9 @@ class _CirclePainter extends CustomPainter {

final BuildContext context;

/// Maximum value for progress, defaults to 1
final double maxValue;

final _paint = Paint();

@override
Expand All @@ -210,7 +237,7 @@ class _CirclePainter extends CustomPainter {
canvas.drawArc(
Rect.fromLTRB(0, 0, size.width, size.height),
3 * math.pi / 2,
progress * fullCircle,
progress / maxValue * fullCircle,
false,
_paint,
);
Expand Down