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

Feature: Number Formatter #268

Open
wants to merge 2 commits 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: 39 additions & 14 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:example/gauge_vertical.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:geekyants_flutter_gauges/geekyants_flutter_gauges.dart';
// import 'package:syncfusion_flutter_gauges/gauges.dart';
import 'package:intl/intl.dart';

void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyVerticalGauge(),
home: LinearGaugeExample(),
),
);
}
Expand All @@ -23,21 +26,38 @@ class LinearGaugeExample extends StatefulWidget {
}

class _LinearGaugeExampleState extends State<LinearGaugeExample> {
NumberFormat formatter =
NumberFormat.currency(locale: 'en_US', name: 'Rupees');

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: LinearGauge(
gaugeOrientation: GaugeOrientation.horizontal,
enableGaugeAnimation: true,
rulers: RulerStyle(
rulerPosition: RulerPosition.bottom,
),
pointers: const [
Pointer(
value: 50,
shape: PointerShape.circle,
child: Column(
children: [
LinearGauge(
// numberFormat:
// NumberFormat.currency(decimalDigits: 1, symbol: 'F'),
start: -70.33,
end: 62.4444,
gaugeOrientation: GaugeOrientation.horizontal,
enableGaugeAnimation: true,
rulers: RulerStyle(
rulerPosition: RulerPosition.bottom,
),
pointers: const [
Pointer(
value: 50,
shape: PointerShape.circle,
),
],
),
// SfLinearGauge(
// showAxisTrack: true,
// minimum: -70.333333,
// numberFormat: NumberFormat.currency(decimalDigits: 1, symbol: ''),
// maximum: 62.4,
// )
],
),
),
Expand All @@ -60,12 +80,17 @@ class RadialGaugeExample extends StatefulWidget {
class _RadialGaugeExampleState extends State<RadialGaugeExample> {
@override
Widget build(BuildContext context) {
return const Scaffold(
return Scaffold(
backgroundColor: Colors.white,
body: RadialGauge(
track: RadialTrack(
start: 0,
end: 100,
// trackLabelFormater: (value) {
// return NumberFormat.currency(
// locale: 'en_US', name: 'R', decimalDigits: 3)
// .format(value);
// },
start: -70.33,
end: 62.4444,
),
needlePointer: [
NeedlePointer(
Expand Down
10 changes: 9 additions & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.3"
version: "1.0.4"
intl:
dependency: transitive
description:
name: intl
sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
url: "https://pub.dev"
source: hosted
version: "0.19.0"
leak_tracker:
dependency: transitive
description:
Expand Down
15 changes: 15 additions & 0 deletions lib/src/linear_gauge/gauge_container/linear_gauge_container.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:geekyants_flutter_gauges/geekyants_flutter_gauges.dart';
import 'package:intl/intl.dart' show NumberFormat;
import 'dart:math' as math;
import '../linear_gauge_label.dart';

Expand All @@ -19,6 +20,7 @@ class LinearGaugeContainer extends LeafRenderObjectWidget {
return RenderLinearGaugeContainer(
start: linearGauge.start!,
end: linearGauge.end!,
numberFormat: linearGauge.labelFormat ?? NumberFormat('#.##'),
value: linearGauge.value!,
steps: linearGauge.steps!,
gaugeOrientation: linearGauge.gaugeOrientation!,
Expand Down Expand Up @@ -59,6 +61,7 @@ class LinearGaugeContainer extends LeafRenderObjectWidget {
renderObject
..setStart = linearGauge.start!
..setEnd = linearGauge.end!
..setNumberFormat = linearGauge.labelFormat ?? NumberFormat('#.##')
..setValue = linearGauge.value!
..setSteps = linearGauge.steps!
..setGaugeOrientation = linearGauge.gaugeOrientation!
Expand Down Expand Up @@ -98,6 +101,7 @@ class RenderLinearGaugeContainer extends RenderBox {
RenderLinearGaugeContainer({
required double start,
required double end,
required NumberFormat numberFormat,
required double steps,
required double value,
required GaugeOrientation gaugeOrientation,
Expand Down Expand Up @@ -130,6 +134,7 @@ class RenderLinearGaugeContainer extends RenderBox {
required LinearGradient? linearGradient,
}) : _start = start,
_end = end,
_numberFormat = numberFormat,
_value = value,
_steps = steps,
_gaugeOrientation = gaugeOrientation,
Expand Down Expand Up @@ -181,6 +186,7 @@ class RenderLinearGaugeContainer extends RenderBox {
set setStart(double start) {
if (_start == start) return;
_start = start;
// _start = double.parse(start.toStringAsFixed(2));
markNeedsPaint();
}

Expand All @@ -205,6 +211,14 @@ class RenderLinearGaugeContainer extends RenderBox {
markNeedsPaint();
}

NumberFormat get getNumberFormat => _numberFormat ?? NumberFormat('#.##');
NumberFormat? _numberFormat;
set setNumberFormat(NumberFormat? numberFormat) {
if (_numberFormat == numberFormat) return;
_numberFormat = numberFormat;
markNeedsPaint();
}

///
/// Getter and Setter for the [_steps] parameter.
///
Expand Down Expand Up @@ -700,6 +714,7 @@ class RenderLinearGaugeContainer extends RenderBox {
}

_linearGaugeLabel.addLabels(
labelFormat: getNumberFormat,
distanceValueInRangeOfHundred: getSteps == 0.0 ? interval : getSteps,
start: getStart,
end: getEnd,
Expand Down
17 changes: 17 additions & 0 deletions lib/src/linear_gauge/linear_gauge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:geekyants_flutter_gauges/src/linear_gauge/rulers/label_painter.d
import 'package:geekyants_flutter_gauges/src/linear_gauge/rulers/rulers.dart';
import 'package:geekyants_flutter_gauges/src/linear_gauge/rulers/rulers_painter.dart';
import 'package:geekyants_flutter_gauges/src/linear_gauge/value_bar/valuebar_painter.dart';
import 'package:intl/intl.dart' show NumberFormat;
import 'linear_gauge_painter.dart';

/// Creates a LinearGauge Widget to display the values in a linear scale. The
Expand Down Expand Up @@ -44,6 +45,7 @@ class LinearGauge extends StatefulWidget {
Key? key,
this.start = 0,
this.end = 100,
this.labelFormat,
this.steps = 0,
@Deprecated('Use ValueBar instead') this.value = 0,
this.gaugeOrientation = GaugeOrientation.horizontal,
Expand Down Expand Up @@ -155,6 +157,19 @@ class LinearGauge extends StatefulWidget {
///
final double? steps;

///
/// `labelFormat` Sets the format of the label of the [LinearGauge] using [NumberFormat]
///
/// default is to `NumberFormat('#.##')`
///
/// ```dart
/// const LinearGauge(
/// labelFormat : NumberFormat.currency(locale: "en_US", symbol: "\$"),
/// ),
/// ```
///
final NumberFormat? labelFormat;

///
/// `extendLinearGauge` Sets the rulers & labels away from the ending points of [LinearGauge] Container
///
Expand Down Expand Up @@ -712,6 +727,7 @@ class _RLinearGauge extends MultiChildRenderObjectWidget {
return RenderLinearGauge(
start: lGauge.start!,
end: lGauge.end!,
labelFormat: lGauge.labelFormat ?? NumberFormat('#.##'),
steps: lGauge.steps!,
gaugeOrientation: lGauge.gaugeOrientation!,
primaryRulersWidth: lGauge.rulers!.primaryRulersWidth!,
Expand Down Expand Up @@ -741,6 +757,7 @@ class _RLinearGauge extends MultiChildRenderObjectWidget {
BuildContext context, RenderLinearGauge renderObject) {
renderObject
..setCustomLabels = lGauge.customLabels!
..setNumberFormat = lGauge.labelFormat ?? NumberFormat('#.##')
..setGaugeOrientation = lGauge.gaugeOrientation!
..setPrimaryRulersHeight = lGauge.rulers!.primaryRulersHeight!
..setPrimaryRulersWidth = lGauge.rulers!.primaryRulersWidth!
Expand Down
18 changes: 16 additions & 2 deletions lib/src/linear_gauge/linear_gauge_label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:geekyants_flutter_gauges/geekyants_flutter_gauges.dart';
import 'package:geekyants_flutter_gauges/src/linear_gauge/gauge_container/linear_gauge_container.dart';
import 'package:geekyants_flutter_gauges/src/linear_gauge/rulers/rulers_painter.dart';
import 'package:intl/intl.dart' show NumberFormat;

class LinearGaugeLabel {
String? text;
Expand All @@ -18,20 +19,33 @@ class LinearGaugeLabel {
TextPainter(textDirection: TextDirection.ltr);

void addLabels({
NumberFormat? labelFormat,
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));
String stringValue;
if (labelFormat != null) {
stringValue = labelFormat.format(i);
} else {
stringValue = i.toString();
}
_linearGaugeLabel.add(LinearGaugeLabel(text: stringValue, 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 stringValue;
if (labelFormat != null) {
stringValue = labelFormat.format(end);
} else {
stringValue = end.toString();
}
_linearGaugeLabel.add(LinearGaugeLabel(text: stringValue, value: end));
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/src/linear_gauge/linear_gauge_painter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:geekyants_flutter_gauges/geekyants_flutter_gauges.dart';
Expand All @@ -7,6 +9,7 @@ import 'package:geekyants_flutter_gauges/src/linear_gauge/linear_gauge_label.dar
import 'package:geekyants_flutter_gauges/src/linear_gauge/rulers/label_painter.dart';
import 'package:geekyants_flutter_gauges/src/linear_gauge/rulers/rulers_painter.dart';
import 'package:geekyants_flutter_gauges/src/linear_gauge/value_bar/valuebar_painter.dart';
import 'package:intl/intl.dart' show NumberFormat;

import 'gauge_container/linear_gauge_container.dart';

Expand All @@ -18,6 +21,7 @@ class RenderLinearGauge extends RenderBox
RenderLinearGauge({
required double start,
required double end,
required NumberFormat labelFormat,
required double steps,
required GaugeOrientation gaugeOrientation,
required TextStyle textStyle,
Expand Down Expand Up @@ -159,6 +163,14 @@ class RenderLinearGauge extends RenderBox
markNeedsPaint();
}

get getLabelFormat => _labelFormat;
NumberFormat? _labelFormat;
set setNumberFormat(NumberFormat? labelFormat) {
if (_labelFormat == labelFormat) return;
_labelFormat = labelFormat;
markNeedsPaint();
}

///
/// Getter and Setter for the [_steps] parameter.
///
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ environment:
dependencies:
flutter:
sdk: flutter
intl: ^0.19.0

dev_dependencies:
flutter_test:
Expand Down
Loading