diff --git a/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer.dart b/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer.dart index bca7cddf..6d3d532f 100644 --- a/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer.dart +++ b/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer.dart @@ -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); /// @@ -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` /// @@ -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 @@ -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); } diff --git a/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer_painter.dart b/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer_painter.dart index c7e23886..157b7e90 100644 --- a/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer_painter.dart +++ b/lib/src/linear_gauge/pointers/linear_gauge_shape_pointer_painter.dart @@ -27,6 +27,7 @@ class RenderLinearGaugeShapePointer extends RenderOpacity { required bool isInteractive, required Animation pointerAnimation, required LinearGauge linearGauge, + required String Function(double? value)? labelFormatter, }) : _value = value, _height = height, _onChanged = onChanged, @@ -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; @@ -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; @@ -341,7 +357,7 @@ class RenderLinearGaugeShapePointer extends RenderOpacity { return; } - if (showLabel) { + if (showLabel || labelFormatter != null) { _drawLabel(canvas, offset, quarterTurns, rulerPosition, linearGauge); } } @@ -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) {