aboutsummaryrefslogtreecommitdiffstats
path: root/lib/presentation/screens/hvac/widgets/fan_speed_controls.dart
blob: e9a97198479f07cf0bab4394efc69ffe60e14f3e (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import 'package:flutter_ics_homescreen/export.dart';
import 'package:gradient_borders/gradient_borders.dart';
import 'package:rive/rive.dart' as rive;

class FanSpeedControls extends ConsumerStatefulWidget {
  const FanSpeedControls({super.key});

  @override
  FanSpeedControlsState createState() => FanSpeedControlsState();
}

class FanSpeedControlsState extends ConsumerState<FanSpeedControls>
    with TickerProviderStateMixin {
  bool isPressed = false;
  LinearGradient gradientEnable1 = const LinearGradient(colors: <Color>[
    Color(0xFF2962FF),
    Color(0x802962FF),
  ]);
  LinearGradient gradientEnable2 = const LinearGradient(colors: <Color>[
    Color(0xFF1A237E),
    Color(0xFF141F64),
  ]);
  bool isMainACSelected = false;
  late AnimationController animationController;
  double controlProgress = 0.0;
  int selectedFanSpeed = 0;
  late rive.RiveAnimationController _controller;
  bool isButtonHighlighted = false;

  bool _isPlaying = false;

  /// Tracks if the animation is playing by whether controller is running
  bool get isPlaying => _controller.isActive;

  @override
  void initState() {
    super.initState();
    _controller = rive.OneShotAnimation(
      'Fan Spin',
      autoplay: false,
      onStop: () => setState(() => _isPlaying = false),
      onStart: () => setState(() => _isPlaying = true),
    );
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    );

    animationController.addListener(() {
      setState(() {
        // _currentColorIndex = (_currentColorIndex + 1) % colorsList.length;
      }); // Trigger a rebuild to repaint the CustomPaint
    });
    animationController.forward();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double size = MediaQuery.sizeOf(context).height * 0.13021;
    double fanSpeedWidth = MediaQuery.sizeOf(context).width * 0.35;
    double fanSpeedHeight = MediaQuery.sizeOf(context).height * 0.15;
    double strokeWidth = MediaQuery.sizeOf(context).height * 0.03;

    double iconSize = 80;

    int selectedFanSpeed = ref.watch(vehicleProvider.select((vehicle) => vehicle.fanSpeed));
    controlProgress = selectedFanSpeed * 0.3;

    return Stack(
      children: [
        Center(
          child: CustomPaint(
            size: Size(
                fanSpeedWidth, fanSpeedHeight), // Set the desired size here
            painter: AnimatedColorPainter(
              animationController,
              controlProgress,
              AGLDemoColors.blueGlowFillColor,
              AGLDemoColors.backgroundInsetColor,
              strokeWidth,
            ),
          ),
        ),
        Center(
            child: Container(
          margin: const EdgeInsets.only(top: 3),
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            gradient: LinearGradient(
                colors: !isButtonHighlighted
                    ? [
                        AGLDemoColors.neonBlueColor,
                        AGLDemoColors.neonBlueColor.withOpacity(0.2)
                      ]
                    : [
                        AGLDemoColors.resolutionBlueColor,
                        const Color(0xff141F64)
                      ]),
            boxShadow: isButtonHighlighted
                ? [
                    BoxShadow(
                        offset: Offset(isButtonHighlighted ? 1 : 1,
                            isButtonHighlighted ? 2 : 2),
                        blurRadius: isButtonHighlighted ? 16 : 16,
                        spreadRadius: 0,
                        color: isButtonHighlighted
                            ? Colors.black.withOpacity(0.5)
                            : Colors.black)
                  ]
                : [],
          ),
          // border: Border.all(color: Colors.white12, width: 1),
          //width: 90,
          //height: 90,
          child: Container(
            margin: const EdgeInsets.all(1),
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              image: const DecorationImage(
                image: AssetImage("assets/PlusVector.png"),
              ),
              border: GradientBoxBorder(
                width: 1,
                gradient: LinearGradient(
                  colors: [
                    isButtonHighlighted
                        ? AGLDemoColors.neonBlueColor
                        : AGLDemoColors.periwinkleColor.withOpacity(0.20),
                    isButtonHighlighted
                        ? AGLDemoColors.neonBlueColor.withOpacity(0.20)
                        : AGLDemoColors.periwinkleColor,
                  ],
                ),
              ),
            ),
            alignment: Alignment.center,
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                  splashColor: Colors.transparent,
                  hoverColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  customBorder: const CircleBorder(),
                  onHighlightChanged: (value) {
                    setState(() {
                      isButtonHighlighted = value;
                    });
                  },
                  onTap: () {
                    setState(() {
                      if (controlProgress >= 0.80) {
                        controlProgress = 0.0;
                        isMainACSelected = false;
                        _isPlaying = false;
                        animationController.reverse();
                      } else {
                        _controller.isActive = true;
                        isMainACSelected = true;
                        _isPlaying = true;
                        controlProgress += 0.30;
                        animationController.forward();
                      }
                      ref
                          .read(vehicleProvider.notifier)
                          .updateFanSpeed(controlProgress ~/ 0.3);
                    });
                  },
                  onTapDown: (details) {
                  },
                  onTapUp: (details) {
                  },
                  child: Container(
                      width: size,
                      height: size,
                      alignment: Alignment.center,
                      child: !_isPlaying && controlProgress == 0.0
                          ? SvgPicture.asset(
                              "assets/ACMainButtonOff.svg",
                              width: iconSize,
                              height: iconSize,
                            )
                          : SizedBox(
                              width: iconSize,
                              height: iconSize,
                              child: rive.RiveAnimation.asset(
                                  'assets/new_file.riv',
                                  controllers: [_controller],
                                  onInit: (_) => setState(() {
                                        _controller.isActive = true;
                                      }))))
                  ),
            ),
          ),
        ))
      ],
    );
  }
}