aboutsummaryrefslogtreecommitdiffstats
path: root/lib/presentation/screens/hvac/widgets/semi_circle_painter.dart
blob: 9756e7ffc306829a0630cb6c5bd78fba49cc1c4f (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
import 'package:flutter_ics_homescreen/export.dart';
import 'dart:math' as math;

class AnimatedColorPainter extends CustomPainter {
  final AnimationController animationController;
  final double progress;
  final Color progressColor; // New parameter for progress color
  final Color backgroundColor;
  final double strokeWidth;

  AnimatedColorPainter(this.animationController, this.progress,
      this.progressColor, this.backgroundColor, this.strokeWidth);

  @override
  void paint(Canvas canvas, Size size) {
    //  const strokeWidth = 25.0;

    // Divide the arc into equal parts based on the number of colors
    const arcAngle = math.pi;
    const arcPart = arcAngle / 3;
    const gapAngle = arcAngle / 150;

    // Define drop shadow properties
    const shadowOffset = Offset(0, 0);
    const shadowBlur = 12.0;
    const shadowColor = AGLDemoColors.jordyBlueColor;

    // Calculate the current color index based on animation progress and progress value
    final double normalizedProgress = progress * 3;
    int currentColorIndex =
        (animationController.value * normalizedProgress).floor();
    if (progress == 0.0) {
      currentColorIndex = -1; // Force background color when progress is 0
    }
    // Draw each part with a border and inner color
    double startAngle = -math.pi; // Start from left
    for (int i = 0; i < 3; i++) {
      Color? currentColor = backgroundColor;
      if (i <= currentColorIndex) {
        // Use progress color if within progress range
        currentColor = progressColor;
      } else {
        // Use background color if outside progress range
        currentColor = backgroundColor;
      }

      // Create paths for drop shadow and actual color
      final shadowPath = Path()
        ..addArc(
          Rect.fromCircle(
            center: Offset(size.width / 2, size.height / 2) + shadowOffset,
            radius: size.width / 2,
          ),
          startAngle,
          arcPart - 2 * gapAngle,
          // Draw clockwise
        );
      final colorPath = Path()
        ..addArc(
          Rect.fromCircle(
            center: Offset(size.width / 2, size.height / 2),
            radius: size.width / 2,
          ),
          startAngle,
          arcPart - 2 * gapAngle,
          // Draw clockwise
        );

      // Draw drop shadow using offset and blur
      final shadowPaint = Paint()
        ..color = shadowColor
        ..style = PaintingStyle.fill
        ..maskFilter = const MaskFilter.blur(BlurStyle.normal, shadowBlur);
      if (currentColor == progressColor) {
        canvas.drawPath(shadowPath, shadowPaint);
      }

      // Draw border
      final borderPaint = Paint()
        ..strokeWidth = strokeWidth + 2.0 // Add border width
        ..style = PaintingStyle.stroke
        ..color = Colors.white12;
      canvas.drawPath(colorPath, borderPaint);

      // Draw inner color
      final colorPaint = Paint()
        ..strokeWidth = strokeWidth
        ..style = PaintingStyle.stroke
        ..shader = _createColorShader(currentColor, size, i);
      canvas.drawPath(colorPath, colorPaint);

      startAngle += arcPart + gapAngle;
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;

  Shader _createColorShader(Color color, Size size, int index) {
    Alignment alignment = index == 0
        ? const Alignment(-0.78, -0.38)
        : index == 1
            ? const Alignment(0, -1)
            : const Alignment(0.78, -0.38);
    if (color == progressColor) {
      return RadialGradient(
        center: alignment,
        radius: 0.35,
        focal: alignment,
        focalRadius: 0.02,
        colors: const [
          AGLDemoColors.blueGlowFillColor,
          AGLDemoColors.jordyBlueColor,
          AGLDemoColors.neonBlueColor
        ],
      ).createShader(
        Rect.fromCircle(
          center: Offset(size.width / 2, size.height / 2),
          radius: size.width / 2,
        ),
      );
    }
    return LinearGradient(colors: [color, color]).createShader(
      Rect.fromCircle(
        center: Offset(size.width / 2, size.height / 2),
        radius: size.width / 2,
      ),
    );
  }
}