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

Custom Label Format #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
53 changes: 35 additions & 18 deletions lib/src/linear_gauge/pointers/linear_gauge_shape_pointer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Pointer extends LeafRenderObjectWidget implements BasePointer {
this.animationDuration = 1000,
this.animationType = Curves.ease,
this.enableAnimation = true,
this.labelFormatter,
}) : super(key: key);

///
Expand Down Expand Up @@ -141,6 +142,19 @@ class Pointer extends LeafRenderObjectWidget implements BasePointer {
///
final bool showLabel;

///
/// `labelFormatter` formats a custom label for the pointer on the [Pointer]
///
/// E.g., Value with %
/// ```dart
/// const LinearGauge(
/// pointer: Pointer(
/// labelFormatter: (double? value) => '$value%',
/// ),
/// ),
/// ```
final String Function(double? value)? labelFormatter;

///
/// `quarterTurns` Sets the rotation of the label of `pointer`
///
Expand Down Expand Up @@ -250,23 +264,25 @@ class Pointer extends LeafRenderObjectWidget implements BasePointer {
RenderObject createRenderObject(BuildContext context) {
final LinearGaugeState linearGaugeScope = LinearGaugeState.of(context);
return RenderLinearGaugeShapePointer(
value: value,
color: color,
width: width,
isInteractive: isInteractive,
height: height,
pointerPosition: pointerPosition,
shape: shape,
pointerAlignment: pointerAlignment,
animationDuration: animationDuration,
showLabel: showLabel,
animationType: animationType,
quarterTurns: quarterTurns,
enableAnimation: enableAnimation,
labelStyle: labelStyle,
onChanged: onChanged,
pointerAnimation: linearGaugeScope.animation!,
linearGauge: linearGaugeScope.lGauge);
value: value,
color: color,
width: width,
isInteractive: isInteractive,
height: height,
pointerPosition: pointerPosition,
shape: shape,
pointerAlignment: pointerAlignment,
animationDuration: animationDuration,
showLabel: showLabel,
animationType: animationType,
quarterTurns: quarterTurns,
enableAnimation: enableAnimation,
labelStyle: labelStyle,
onChanged: onChanged,
pointerAnimation: linearGaugeScope.animation!,
linearGauge: linearGaugeScope.lGauge,
labelFormatter: labelFormatter,
);
}

@override
Expand All @@ -289,7 +305,8 @@ class Pointer extends LeafRenderObjectWidget implements BasePointer {
..setLinearGAuge = linearGaugeScope.lGauge
..onChanged = onChanged
..setIsInteractive = isInteractive
..setLabelStyle = labelStyle;
..setLabelStyle = labelStyle
..setLabelFormatter = labelFormatter;

super.updateRenderObject(context, renderObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class RenderLinearGaugeShapePointer extends RenderOpacity {
required bool isInteractive,
required Animation<double> pointerAnimation,
required LinearGauge linearGauge,
required String Function(double? value)? labelFormatter,
}) : _value = value,
_height = height,
_onChanged = onChanged,
Expand All @@ -41,7 +42,8 @@ class RenderLinearGaugeShapePointer extends RenderOpacity {
_linearGauge = linearGauge,
_pointerAnimation = pointerAnimation,
_isInteractive = isInteractive,
_enableAnimation = enableAnimation;
_enableAnimation = enableAnimation,
_labelFormatter = labelFormatter;

double yAxisForGaugeContainer = 0, xAxisForGaugeContainer = 0;
late LinearGaugeLabel linearGaugeLabel;
Expand Down Expand Up @@ -177,6 +179,20 @@ class RenderLinearGaugeShapePointer extends RenderOpacity {
markNeedsPaint();
}

/// Gets the labelFormatter assigned to [RenderLinearGaugeShapePointer].
String Function(double? value)? get labelFormatter => _labelFormatter;
String Function(double? value)? _labelFormatter;

/// Sets the labelFormatter for [RenderLinearGaugeShapePointer].
set setLabelFormatter(String Function(double? value)? value) {
if (value == _labelFormatter) {
return;
}

_labelFormatter = value;
markNeedsPaint();
}

/// Gets the quarterTurns assigned to [RenderLinearGaugeShapePointer].
QuarterTurns get quarterTurns => _quarterTurns;
QuarterTurns _quarterTurns;
Expand Down Expand Up @@ -341,7 +357,7 @@ class RenderLinearGaugeShapePointer extends RenderOpacity {
return;
}

if (showLabel) {
if (showLabel || labelFormatter != null) {
_drawLabel(canvas, offset, quarterTurns, rulerPosition, linearGauge);
}
}
Expand All @@ -356,11 +372,14 @@ class RenderLinearGaugeShapePointer extends RenderOpacity {
textAlign: TextAlign.center,
);

textPainter.text = TextSpan(
text: value == null ? linearGauge.value!.toString() : value.toString(),
style: labelStyle);
offset = applyAnimations(linearGauge, offset);
// Attempt to get the custom label value,
// if null fallback on the standard value.
final String pointerText = value != null
? (labelFormatter?.call(value) ?? value.toString())
: linearGauge.value!.toString();

textPainter.text = TextSpan(text: pointerText, style: labelStyle);
offset = applyAnimations(linearGauge, offset);
textPainter.layout();
if (shape == PointerShape.circle) {
if (gaugeOrientation == GaugeOrientation.horizontal) {
Expand Down