Skip to content

Commit

Permalink
Merge pull request #269 from Afroz-Shaikh/feat/track-label-format
Browse files Browse the repository at this point in the history
Fix: Precision Issues and Render Issues
  • Loading branch information
geekruchika authored May 20, 2024
2 parents c116467 + b0ccdfe commit cb271e7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
32 changes: 17 additions & 15 deletions lib/src/linear_gauge/gauge_container/linear_gauge_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class LinearGaugeContainer extends LeafRenderObjectWidget {
return RenderLinearGaugeContainer(
start: linearGauge.start!,
end: linearGauge.end!,
trackLabelFormat: linearGauge.trackLabelFormat ??
(double value) => ((value * 10).round() / 10).toString(),
value: linearGauge.value!,
steps: linearGauge.steps!,
gaugeOrientation: linearGauge.gaugeOrientation!,
Expand Down Expand Up @@ -59,6 +61,8 @@ class LinearGaugeContainer extends LeafRenderObjectWidget {
renderObject
..setStart = linearGauge.start!
..setEnd = linearGauge.end!
..setTrackLabelFormat = linearGauge.trackLabelFormat ??
((double value) => ((value * 10).round() / 10).toString())
..setValue = linearGauge.value!
..setSteps = linearGauge.steps!
..setGaugeOrientation = linearGauge.gaugeOrientation!
Expand Down Expand Up @@ -98,6 +102,7 @@ class RenderLinearGaugeContainer extends RenderBox {
RenderLinearGaugeContainer({
required double start,
required double end,
required final String Function(double)? trackLabelFormat,
required double steps,
required double value,
required GaugeOrientation gaugeOrientation,
Expand Down Expand Up @@ -130,6 +135,7 @@ class RenderLinearGaugeContainer extends RenderBox {
required LinearGradient? linearGradient,
}) : _start = start,
_end = end,
_trackLabelFormat = trackLabelFormat,
_value = value,
_steps = steps,
_gaugeOrientation = gaugeOrientation,
Expand Down Expand Up @@ -182,6 +188,7 @@ class RenderLinearGaugeContainer extends RenderBox {
if (_start == start) return;
_start = start;
markNeedsPaint();
markNeedsLayout();
}

///
Expand All @@ -193,6 +200,15 @@ class RenderLinearGaugeContainer extends RenderBox {
if (_end == end) return;
_end = end;
markNeedsPaint();
markNeedsLayout();
}

get trackLabelFormat => _trackLabelFormat;
String Function(double)? _trackLabelFormat;
set setTrackLabelFormat(String Function(double)? trackLabelFormat) {
if (_trackLabelFormat == trackLabelFormat) return;
_trackLabelFormat = trackLabelFormat;
markNeedsPaint();
}

double get getValue => _value;
Expand Down Expand Up @@ -656,21 +672,6 @@ class RenderLinearGaugeContainer extends RenderBox {
return largestPointer;
}

// double getLargestWidgetPointerSize() {
// if (RenderLinearGauge.getWidgetPointers!.isNotEmpty) {
// RenderLinearGaugeWidgetPointer? largestPointer =
// getLargestWidgetPointer(RenderLinearGauge.getWidgetPointers);

// if (getGaugeOrientation == GaugeOrientation.vertical) {
// return largestPointer?.size.width ?? 0;
// } else {
// return largestPointer?.size.width ?? 0;
// }
// } else {
// return 0;
// }
// }

void _setLinearGaugeContainerPaint() {
_linearGaugeContainerPaint.color =
setAnimatedColor(getLinearGaugeContainerBgColor);
Expand Down Expand Up @@ -700,6 +701,7 @@ class RenderLinearGaugeContainer extends RenderBox {
}

_linearGaugeLabel.addLabels(
trackLabelFormat: trackLabelFormat,
distanceValueInRangeOfHundred: getSteps == 0.0 ? interval : getSteps,
start: getStart,
end: getEnd,
Expand Down
18 changes: 18 additions & 0 deletions lib/src/linear_gauge/linear_gauge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class LinearGauge extends StatefulWidget {
this.start = 0,
this.end = 100,
this.steps = 0,
this.trackLabelFormat,
@Deprecated('Use ValueBar instead') this.value = 0,
this.gaugeOrientation = GaugeOrientation.horizontal,
this.showLinearGaugeContainer = true,
Expand Down Expand Up @@ -144,6 +145,19 @@ class LinearGauge extends StatefulWidget {
///
final double? end;

///
/// `trackLabelFormat` Sets the format of the label of the [LinearGauge] labels
///
/// ```dart
/// const LinearGauge(
/// trackLabelFormat : value) {
/// return '\$ ${value.toStringAsFixed(1)}';
/// },
/// ),
/// ```
///
final String Function(double)? trackLabelFormat;

///
/// `steps` Sets the interval between label of the [LinearGauge] Container
///
Expand Down Expand Up @@ -712,6 +726,7 @@ class _RLinearGauge extends MultiChildRenderObjectWidget {
return RenderLinearGauge(
start: lGauge.start!,
end: lGauge.end!,
trackLabelFormat: lGauge.trackLabelFormat,
steps: lGauge.steps!,
gaugeOrientation: lGauge.gaugeOrientation!,
primaryRulersWidth: lGauge.rulers!.primaryRulersWidth!,
Expand Down Expand Up @@ -741,6 +756,7 @@ class _RLinearGauge extends MultiChildRenderObjectWidget {
BuildContext context, RenderLinearGauge renderObject) {
renderObject
..setCustomLabels = lGauge.customLabels!
..setTrackLabelFormat = lGauge.trackLabelFormat
..setGaugeOrientation = lGauge.gaugeOrientation!
..setPrimaryRulersHeight = lGauge.rulers!.primaryRulersHeight!
..setPrimaryRulersWidth = lGauge.rulers!.primaryRulersWidth!
Expand Down Expand Up @@ -810,6 +826,8 @@ class RenderLinearGaugeElement extends MultiChildRenderObjectElement {
renderObject.removeValueBar(child);
} else if (child is RenderCurve) {
renderObject.removeCurve(child);
} else if (child is RenderRulerLabel) {
renderObject.removeRulerLabel(child);
}
}
}
8 changes: 6 additions & 2 deletions lib/src/linear_gauge/linear_gauge_label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ class LinearGaugeLabel {
TextPainter(textDirection: TextDirection.ltr);

void addLabels({
required trackLabelFormat,
required double distanceValueInRangeOfHundred,
required double start,
required double end,
}) {
_linearGaugeLabel.clear();

for (double i = start; i <= end; i += distanceValueInRangeOfHundred) {
_linearGaugeLabel.add(LinearGaugeLabel(text: i.toString(), value: i));
text = trackLabelFormat(i);

_linearGaugeLabel.add(LinearGaugeLabel(text: text, value: i));
}

final LinearGaugeLabel localLabel =
_linearGaugeLabel[_linearGaugeLabel.length - 1];
if (localLabel.value != end && localLabel.value! < end) {
_linearGaugeLabel.add(LinearGaugeLabel(text: end.toString(), value: end));
String text = trackLabelFormat(end);
_linearGaugeLabel.add(LinearGaugeLabel(text: text, value: end));
}
}

Expand Down
29 changes: 26 additions & 3 deletions lib/src/linear_gauge/linear_gauge_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RenderLinearGauge extends RenderBox
RenderLinearGauge({
required double start,
required double end,
required String Function(double)? trackLabelFormat,
required double steps,
required GaugeOrientation gaugeOrientation,
required TextStyle textStyle,
Expand All @@ -42,6 +43,7 @@ class RenderLinearGauge extends RenderBox
}) : assert(start < end, "Start should be grater then end"),
_start = start,
_end = end,
_trackLabelFormat = trackLabelFormat,
_steps = steps,
_gaugeOrientation = gaugeOrientation,
_textStyle = textStyle,
Expand Down Expand Up @@ -267,6 +269,14 @@ class RenderLinearGauge extends RenderBox
markNeedsLayout();
}

String Function(double)? get getTrackLabelFormat => _trackLabelFormat;
String Function(double)? _trackLabelFormat;
set setTrackLabelFormat(String Function(double)? val) {
if (_trackLabelFormat == val) return;
_trackLabelFormat = val;
markNeedsPaint();
}

double get getLabelOffset => _labelOffset;
double _labelOffset;
set setLabelOffset(double val) {
Expand Down Expand Up @@ -327,9 +337,19 @@ class RenderLinearGauge extends RenderBox
}

LinearGaugeLabel get getLinearGaugeLabel {
markNeedsPaint();
markNeedsLayout();
return _linearGaugeLabel;
}

LinearGaugeLabel _linearGaugeLabel = LinearGaugeLabel();
set setLinearGaugeLabel(LinearGaugeLabel val) {
if (_linearGaugeLabel == val) return;
_linearGaugeLabel = val;
markNeedsPaint();
markNeedsLayout();
}

///
/// Getter and Setter for the [valueBar] parameter.
///
Expand Down Expand Up @@ -398,7 +418,6 @@ class RenderLinearGauge extends RenderBox
///
double get getPointerSpace => _pointerSpace;
final double _pointerSpace = 0;
LinearGaugeLabel _linearGaugeLabel = LinearGaugeLabel();

late Size _axisActualSize;
late final List<RenderLinearGaugeWidgetPointer> _widgetPointers;
Expand Down Expand Up @@ -449,6 +468,12 @@ class RenderLinearGauge extends RenderBox
markNeedsLayout();
}

/// Remove the ruler label render object from widget .
void removeRulerLabel(RenderRulerLabel label) {
_renderRulerLabel = null;
markNeedsLayout();
}

/// Adds the valuebar render object to widget .
void addValueBar(RenderValueBar ruler) {
_valueBarRenderObject.add(ruler);
Expand Down Expand Up @@ -2231,5 +2256,3 @@ class LinearGaugeParentData extends MultiChildLayoutParentData {
this.gaugeEnd,
this.linearGaugeLabel);
}

// class Temp implements MouseTrackerAnnotation {}

0 comments on commit cb271e7

Please sign in to comment.