Skip to content

Commit

Permalink
WIP- needle Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Afroz-Shaikh committed May 29, 2023
1 parent a1fcbea commit 33a2dde
Show file tree
Hide file tree
Showing 25 changed files with 837 additions and 383 deletions.
2 changes: 1 addition & 1 deletion example/lib/gauge_vertical.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class _MyVerticalGaugeState extends State<MyVerticalGauge> {
rulers: RulerStyle(
primaryRulersWidth: 10,
primaryRulersHeight: 30,
primaryRulerColor: Color(0xff310072),
primaryRulerColor: const Color(0xff310072),
inverseRulers: false,
showLabel: false,
showSecondaryRulers: false,
Expand Down
31 changes: 1 addition & 30 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,11 @@ void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyValueBarPosition(),
home: MyGaugeExample(),
),
);
}

class MyValueBarPosition extends StatefulWidget {
const MyValueBarPosition({super.key});

@override
State<MyValueBarPosition> createState() => _MyValueBarPositionState();
}

class _MyValueBarPositionState extends State<MyValueBarPosition> {
double value = 50;
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: RadialGauge(
needlePointer: NeedlePointer(value: 5),
track: RadialTrack(
steps: 1,
startAngle: 0,
endAngle: 360,
hideTrack: true,
start: 0,
end: 12,
),
),
),
);
}
}

class MyGaugeExample extends StatefulWidget {
const MyGaugeExample({Key? key}) : super(key: key);

Expand Down
2 changes: 1 addition & 1 deletion example/lib/multiple_pointer_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class _MultiplePointerExampleState extends State<MultiplePointerExample> {
valueBar: const [
ValueBar(value: 23, color: Colors.red),
],
pointers: [
pointers: const [
Pointer(
value: 28,
width: 20,
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pointer_alignment_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class _PointerAlignmentExampleState extends State<PointerAlignmentExample> {
return Center(
child: LinearGauge(
gaugeOrientation: GaugeOrientation.vertical,
pointers: [
pointers: const [
Pointer(
color: Colors.red,
value: 30,
Expand Down
2 changes: 1 addition & 1 deletion example/lib/shaders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class _MyShadersState extends State<MyShaders> {
rulerPosition: RulerPosition.center,
inverseRulers: true,
// secondaryRulersHeight: 300,
textStyle: TextStyle(
textStyle: const TextStyle(
fontSize: 12,
),
// rulersOffset: 20,
Expand Down
2 changes: 1 addition & 1 deletion example/lib/tickness_extend_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class _ThicknessExtendExampleState extends State<ThicknessExtendExample> {
enableGaugeAnimation: true,
linearGaugeBoxDecoration:
const LinearGaugeBoxDecoration(thickness: 20),
pointers: [
pointers: const [
Pointer(
shape: PointerShape.triangle,
pointerPosition: PointerPosition.left,
Expand Down
2 changes: 1 addition & 1 deletion example/lib/valuebar_position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class _MyValueBarPositionState extends State<MyValueBarPosition> {
height: 100,
width: 100,
color: Colors.black.withOpacity(0.3),
labelStyle: TextStyle(color: Colors.black),
labelStyle: const TextStyle(color: Colors.black),
pointerPosition: PointerPosition.top,
),
// ShapePointer(
Expand Down
2 changes: 2 additions & 0 deletions lib/geekyants_flutter_gauges.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export './src/linear_gauge/custom_label/custom_ruler_label.dart';
export './src/linear_gauge/value_bar/value_bar.dart';
export './src/linear_gauge/utils/enums.dart';
export './src/linear_gauge/curve/custom_curve.dart';

export './src/radial_gauge/radial_gauge.dart';
export './src/radial_gauge/radial_track.dart';
export 'src/radial_gauge/pointer/needle_pointer.dart';
export 'src/radial_gauge/valuebar/radial_value_bar.dart';
export 'src/radial_gauge/shape_pointer/radial_shape_pointer.dart';
8 changes: 4 additions & 4 deletions lib/src/linear_gauge/linear_gauge_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ class RenderLinearGauge extends RenderBox
yAxisForGaugeContainer = spacingForGauge;

if (topCurveMaxHeight! <= pointerMaxOfTopAndCenter!) {
valueBarMaxOfTopAndCenter = 0;
topCurveMaxHeight = 0;
} else {
pointerMaxOfTopAndCenter = topCurveMaxHeight!;
valueBarMaxOfTopAndCenter = 0;
Expand All @@ -1641,7 +1641,7 @@ class RenderLinearGauge extends RenderBox
}

if (bottomCurveMaxHeight! <= pointerMaxOfBottomAndCenter!) {
valueBarMaxOfBottomAndCenter = 0;
bottomCurveMaxHeight = 0;
} else {
pointerMaxOfBottomAndCenter = bottomCurveMaxHeight!;
valueBarMaxOfBottomAndCenter = 0;
Expand All @@ -1665,7 +1665,7 @@ class RenderLinearGauge extends RenderBox
yAxisForGaugeContainer = pointerMaxOfTopAndCenter!;

if (topCurveMaxHeight! <= pointerMaxOfTopAndCenter!) {
valueBarMaxOfTopAndCenter = 0;
topCurveMaxHeight = 0;
} else {
yAxisForGaugeContainer = topCurveMaxHeight!;
pointerMaxOfTopAndCenter = yAxisForGaugeContainer;
Expand All @@ -1687,7 +1687,7 @@ class RenderLinearGauge extends RenderBox
}
if (bottomCurveMaxHeight! <=
pointerMaxOfBottomAndCenter! + spacingForGauge) {
// valueBarMaxOfBottomAndCenter = 0;
bottomCurveMaxHeight = 0;
} else {
valueBarMaxOfBottomAndCenter = bottomCurveMaxHeight! - spacingForGauge;
pointerMaxOfBottomAndCenter = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/linear_gauge/pointers/linear_gauge_pointer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class BasePointer {
/// ),
/// ),
/// ```
double value;
final double value;

///
/// Pointer Position on the [LinearGauge] sets the position of `pointer` on the [LinearGauge]
Expand Down
8 changes: 5 additions & 3 deletions lib/src/linear_gauge/pointers/linear_gauge_shape_pointer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import 'package:geekyants_flutter_gauges/geekyants_flutter_gauges.dart';
/// ```
///
///
///
class Pointer extends LeafRenderObjectWidget implements BasePointer {
Pointer({
const Pointer({
Key? key,
required this.value,
this.height = 10.0,
Expand Down Expand Up @@ -51,7 +53,7 @@ class Pointer extends LeafRenderObjectWidget implements BasePointer {
/// ),
/// ```
@override
double value;
final double value;

///
/// `height` Sets the height of the pointer on the [LinearGauge]
Expand Down Expand Up @@ -91,7 +93,7 @@ class Pointer extends LeafRenderObjectWidget implements BasePointer {
/// ),
/// ```
bool isInteractive;
final bool isInteractive;

///
/// `color` Sets the color of the pointer on the [LinearGauge]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class RenderLinearGaugeShapePointer extends RenderOpacity {
offset.dy + height + yAxisTurn);
break;
case PointerPosition.center:
var yAxisTurn;
var yAxisTurn = 0.0;
if (gaugeOrientation == GaugeOrientation.horizontal) {
yAxisTurn = (quarterTurns == QuarterTurns.zero ||
quarterTurns == QuarterTurns.two)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,4 @@ class WidgetPointer extends SingleChildRenderObjectWidget

super.updateRenderObject(context, renderObject);
}

@override
set value(double _value) {
value = _value;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import '../../../geekyants_flutter_gauges.dart';
import '../linear_gauge_label.dart';
import '../linear_gauge_painter.dart';
Expand Down
8 changes: 4 additions & 4 deletions lib/src/linear_gauge/rulers/label_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class RenderRulerLabel extends RenderBox {
_endLabelSize = parentDataRef.linearGaugeLabel.endLabelSize;
gaugeStart = parentDataRef.gaugeStart;
gaugeEnd = parentDataRef.gaugeEnd;
Size _axisActualSize;
Size axisActualSize;
setCustomLabelStartEnd();

double actualParentWidth;
Expand Down Expand Up @@ -275,12 +275,12 @@ class RenderRulerLabel extends RenderBox {
}

if (_isHorizontalOrientation) {
_axisActualSize = Size(actualParentWidth, effectiveLabelHeight);
axisActualSize = Size(actualParentWidth, effectiveLabelHeight);
} else {
_axisActualSize = Size(effectiveLabelHeight, actualParentWidth);
axisActualSize = Size(effectiveLabelHeight, actualParentWidth);
}

size = _axisActualSize;
size = axisActualSize;
}

void _drawLabels(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/radial_gauge/pointer/needle_pointer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NeedlePointer extends LeafRenderObjectWidget {
this.color = Colors.red,
this.tailColor = Colors.red,
this.needleWidth = 10,
this.needleHeight = 350,
this.needleHeight = 150,
this.tailRadius = 20})
: super(key: key);

Expand Down
60 changes: 41 additions & 19 deletions lib/src/radial_gauge/pointer/needle_pointer_painter.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import '../../../geekyants_flutter_gauges.dart';

class RenderNeedlePointer extends RenderBox {
Expand Down Expand Up @@ -112,7 +109,8 @@ class RenderNeedlePointer extends RenderBox {
final canvas = context.canvas;
final center = offset;

Rect circle = Rect.fromLTWH(offset.dx, offset.dy, size.width, _tailRadius);
Rect circle = Rect.fromLTWH(offset.dx - getTailRadius / 2,
offset.dy - getTailRadius / 2, _tailRadius, _tailRadius);
Path circlePath = Path()..addOval(circle);

double strokeWidth = _needleWidth;
Expand All @@ -123,30 +121,54 @@ class RenderNeedlePointer extends RenderBox {
double gaugeEnd = _radialGauge!.track.end;

double value = calculateValueAngle(_value, gaugeStart, gaugeEnd);
double startAngle = _radialGauge!.track.startAngle * (pi / 180);
double endAngle = _radialGauge!.track.endAngle * (pi / 180);
double startAngle = (_radialGauge!.track.startAngle - 180) * (pi / 180);
double endAngle = (_radialGauge!.track.endAngle - 180) * (pi / 180);

final double needleMultiplier = _needleHeight;
final maxH = size.shortestSide / 2 * getRadialGauge.radiusFactor;

final double needleMultiplier = _needleHeight.clamp(0, maxH - 40);

final double angle = startAngle + (value / 100) * (endAngle - startAngle);

final double needleEndX =
center.dx + getTailRadius / 2 + needleMultiplier * cos(angle);
final double needleEndY =
center.dy + getTailRadius / 2 + needleMultiplier * sin(angle);
double needleEndX = center.dx + needleMultiplier * cos(angle);
double needleEndY = center.dy + needleMultiplier * sin(angle);
double needleStartX = center.dx;
double needleStartY = center.dy;

final needlePaint = Paint()
..color = _color
..strokeWidth = strokeWidth
..shader = gradient.createShader(Rect.fromPoints(
Offset(center.dx + getTailRadius / 2, center.dy + getTailRadius / 2),
Offset(needleEndX, needleEndY),
))
..shader = gradient.createShader(
Rect.fromPoints(
Offset(needleStartX, needleStartY),
Offset(needleEndX, needleEndY),
),
)
..strokeCap = StrokeCap.round;
canvas.drawLine(
Offset(center.dx + getTailRadius / 2, center.dy + getTailRadius / 2),
Offset(needleEndX, needleEndY),
needlePaint);

// Simple Needle
// canvas.drawLine(Offset(needleStartX, needleStartY),
// Offset(needleEndX, needleEndY), needlePaint);

// Needle Path
Path needlePath = Path();
needlePath.moveTo(offset.dx + getTailRadius / 2 * cos(angle + pi / 2),
offset.dy - getTailRadius / 2 * sin(angle - pi / 2));
needlePath.lineTo(needleStartX + getTailRadius / 2 * cos(angle - pi / 2),
needleStartY - (getTailRadius / 2) * sin(angle + pi / 2));

double temp1 = 30;
needlePath.moveTo(offset.dx + temp1 * cos(angle + pi / 2),
offset.dy - temp1 * sin(angle - pi / 2));
needlePath.lineTo(needleStartX + temp1 * cos(angle - pi / 2),
needleStartY - (temp1) * sin(angle + pi / 2));

needlePath.lineTo(needleEndX, needleEndY);
// Offset c = Offset(offset.dx, offset.dy - getTailRadius);
// canvas.drawCircle(c, 100, Paint()..color = Colors.black);
needlePath.close();
//* Needle Pointer paint
canvas.drawPath(needlePath, needlePaint);

canvas.drawPath(circlePath, Paint()..color = _tailColor);
}
Expand Down
Loading

0 comments on commit 33a2dde

Please sign in to comment.