summaryrefslogtreecommitdiffstats
path: root/lib/screen/paints/gauge_paint.dart
blob: bae265ce323519c791045d82f30365e0bea00d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: Apache-2.0

import 'package:flutter/material.dart';
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter_cluster_dashboard/screen/widgets/gauges/gauge_props.dart';

class GaugePainter extends CustomPainter {
  final double low, high;
  double currentSpeed;
  final Color? outPrimaryColor;
  final Color? inPrimaryColor;
  final Color? secondaryColor;
  GaugePainter({
    required this.low,
    required this.high,
    required this.currentSpeed,
    this.outPrimaryColor,
    this.inPrimaryColor,
    this.secondaryColor,
  });
  @override
  void paint(Canvas canvas, Size size) {
    Offset center = Offset(size.width / 2, size.height / 2);
    double radius = min(size.width / 2, size.height / 2);
    double radius1 = radius - ((20 / 200) * (radius));
    double speedAngle =
        GaugeProps.degToRad((GaugeProps.majorAngle / high) * currentSpeed);

    final zeroTickPaint = Paint()
      ..strokeWidth = ((7 / 200) * (radius))
      ..shader = ui.Gradient.radial(center, radius1,
          [Colors.black, const Color.fromARGB(255, 244, 242, 231)], [1, 0.5]);
    final maxTickPaint = Paint()
      ..strokeWidth = ((7 / 200) * (radius))
      ..shader = ui.Gradient.radial(center, radius1,
          [Colors.black, const Color.fromARGB(255, 244, 242, 231)], [1, 0.5]);

    final speedPathStrokePaint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = ((7 / 200) * (radius))
      ..shader = ui.Gradient.radial(
          center, radius1, [Colors.black, Colors.white], [0.6, 1]);

    final speedPathFillPaint = Paint()
      ..style = PaintingStyle.fill
      ..shader = ui.Gradient.radial(center, radius1, [
        const ui.Color.fromARGB(0, 0, 0, 0),
        secondaryColor ?? const Color.fromARGB(156, 226, 226, 200)
      ], [
        0.8,
        1
      ]);

    final outerPathPaint = ui.Paint()
      ..style = PaintingStyle.fill
      ..shader = ui.Gradient.radial(
        center,
        radius,
        [
          Colors.black,
          outPrimaryColor ?? const Color.fromARGB(255, 120, 120, 120)
        ],
        [0.8, 0.9],
      );

    final innerPathPaint = Paint()
      ..style = PaintingStyle.fill
      ..shader = ui.Gradient.radial(center, radius1, [
        Colors.black,
        inPrimaryColor ?? const Color.fromARGB(255, 67, 67, 67)
      ], [
        0.65,
        0.9
      ]);

    final outerPathPaintRed = ui.Paint()
      ..style = PaintingStyle.fill
      ..shader = ui.Gradient.radial(
        center,
        radius,
        [Colors.black, const Color.fromARGB(255, 187, 59, 57)],
        [0.8, 0.9],
      );

    final innerPathPaintRed = Paint()
      ..style = PaintingStyle.fill
      ..shader = ui.Gradient.radial(center, radius1,
          [Colors.black, const Color.fromARGB(255, 142, 35, 39)], [0.65, 0.9]);

    for (double i = 0; i < 13; i++) {
      double startAngle = GaugeProps.degToRad(i * 20);
      double gapAngle = GaugeProps.degToRad(19);

      var outerPath = Path();
      outerPath.addArc(Rect.fromCircle(center: center, radius: radius),
          startAngle, gapAngle);
      outerPath.lineTo(center.dx, center.dy);
      outerPath.close();

      var innerPath = Path();
      innerPath.addArc(Rect.fromCircle(center: center, radius: radius1),
          startAngle, gapAngle);
      innerPath.lineTo(center.dx, center.dy);
      innerPath.close();
      if (i >= 11) {
        canvas.drawPath(outerPath, outerPathPaintRed);
        canvas.drawPath(innerPath, innerPathPaintRed);
      } else {
        canvas.drawPath(outerPath, outerPathPaint);
        canvas.drawPath(innerPath, innerPathPaint);
      }
    }

    var speedStrokePath = Path();
    speedStrokePath.moveTo(center.dx, center.dy);
    speedStrokePath.addArc(
        Rect.fromCircle(center: center, radius: radius), 0, speedAngle);
    speedStrokePath.lineTo(center.dx, center.dy);

    var speedFillPath = Path();
    speedFillPath.addArc(
        Rect.fromCircle(center: center, radius: radius), 0, speedAngle);
    speedFillPath.lineTo(center.dx, center.dy);
    speedFillPath.close();

    canvas.drawPath(speedFillPath, speedPathFillPaint);
    canvas.drawPath(speedStrokePath, speedPathStrokePaint);
    canvas.drawLine(
        center,
        Offset(center.dx + (radius + ((3 / 200) * (radius))), center.dy),
        zeroTickPaint);
    canvas.drawLine(
        center,
        Offset(center.dx + (radius) * cos(GaugeProps.majorAngleRad),
            center.dy + (radius) * sin(GaugeProps.majorAngleRad)),
        maxTickPaint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}