summaryrefslogtreecommitdiffstats
path: root/lib/presentation/screens/dashboard/widgets/custom_circle.dart
blob: 4e26f0b28fef49160811af204f4cdef36f15bc8e (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
import 'dart:math' as math;

import 'package:flutter_ics_homescreen/export.dart';

class CirclePainter extends CustomPainter {
  final double value;
  final double maxValue;
  final bool? isRPM;
  final bool? isFuel;

  CirclePainter({
    required this.value,
    required this.maxValue,
    this.isRPM = false,
    this.isFuel = false,
  });
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = AGLDemoColors.neonBlueColor
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;
    final paintRed = Paint()
      ..color = const Color(0xFFBF360C)
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;

    final smallCirclePaint = Paint()
      ..color = AGLDemoColors.resolutionBlueColor
      ..strokeWidth = 10
      // Use [PaintingStyle.fill] if you want the circle to be filled.
      ..style = PaintingStyle.fill;

    final center = Offset(size.width / 2, size.height / 2);

    final double radius = (size.width / 2) - 10;

    const totalDegree = 360;

    // Total ticks to display
    var totalTicks = isFuel! ? 4 : 8;

    var values = [];
    for (int i = 0; i < totalTicks; i++) {
      values.add(i * (maxValue / totalTicks));
    }

    /// The angle between each tick
    var unitAngle = totalDegree / totalTicks;
    for (int i = 0; i < totalTicks; i++) {
      final angle = -90.0.radians + (i * unitAngle).radians;
      final xOffset = radius * math.cos(angle);
      final yOffset = radius * math.sin(angle);
      final offset = Offset(center.dx + xOffset, center.dy + yOffset);
      if (value > values[i]) {
        canvas.drawCircle(offset, 3, smallCirclePaint);
      } else {
        canvas.drawCircle(offset, 3, smallCirclePaint..color = Colors.white);
      }
    }

    final rect = Rect.fromCenter(
      center: center,
      width: ((size.width / 2.4) * 2) + 2,
      height: (size.width / 2.4) * 2 + 2,
    );
    canvas.drawArc(
      rect,
      _deg2Rads(-90),
      _deg2Rads(360),
      false,
      paint,
    );
    if (isRPM == true) {
      canvas.drawArc(
        rect,
        _deg2Rads(202),
        _deg2Rads(68),
        false,
        paintRed,
      );
    }
    if (isFuel == true) {
      canvas.drawArc(
        rect,
        _deg2Rads(-90),
        _deg2Rads(80),
        false,
        paintRed,
      );
    }

    //canvas.drawArc(rect, pi / 4, pi * 3 / 4, false, paint);
  }

  double _deg2Rads(num deg) {
    return (deg * math.pi) / 180.0;
  }

  @override
  bool shouldRepaint(oldDelegate) => false;
}

extension on num {
  /// This is an extension we created so we can easily convert a value  /// to a radian value
  double get radians => (this * math.pi) / 180.0;
}