summaryrefslogtreecommitdiffstats
path: root/lib/screen/widgets/gauges/speed_gauge_animation_wrapper.dart
blob: dfa42774025e7e6194a331a64b359d9501628cc6 (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
// SPDX-License-Identifier: Apache-2.0

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:flutter_cluster_dashboard/screen/widgets/gauges/gauge_props.dart';
import 'package:flutter_cluster_dashboard/screen/widgets/gauges/gauge_widget.dart';
import 'package:flutter_cluster_dashboard/vehicle-signals/vehicle_status_provider.dart';

class SpeedGauge extends HookConsumerWidget {
  final double screenHeight;
  final GaugeColors? gaugeColor;
  const SpeedGauge({Key? key, required this.screenHeight, this.gaugeColor})
      : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final double speed = ref.watch(vehicleStatusProvider.select((p) => p.speed));
    final String units = ref.watch(vehicleStatusProvider.select((p) => p.vehicleDistanceUnit));

    const double minSpeed = 0;
    const double maxSpeed = 240;
    const Duration sweepDuration = Duration(milliseconds: 200);
    double speedScaling = (units == "mi") ? 0.621504 : 1.0;

    final animationController = useAnimationController(
      lowerBound: minSpeed,
      upperBound: maxSpeed,
    )..animateTo(speedScaling * speed,
        duration: sweepDuration, curve: Curves.linearToEaseOut);

    return AnimatedBuilder(
        animation: animationController,
        builder: (context, child) {
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: CustomGauge(
              size: (248 * screenHeight) / 480,
              low: minSpeed,
              high: maxSpeed,
              mainValue: animationController.value,
              label: (units == "mi") ? "mph" : "Km/h",
              zeroTickLabel: minSpeed.toInt().toString(),
              maxTickLabel: maxSpeed.toInt().toString(),
              inPrimaryColor: gaugeColor?.inPrimary,
              outPrimaryColor: gaugeColor?.outPrimary,
              secondaryColor: gaugeColor?.secondary,
            ),
          );
        });
  }
}

final gaugeColorProvider = Provider.family<GaugeColors, String>((ref, mode) {
  switch (mode) {
    case "normal":
      return GaugeColors(inPrimary: Colors.red);
    case "sports":
      return GaugeColors(inPrimary: Colors.blue);
    case "eco":
      return GaugeColors(inPrimary: Colors.green);
    default:
      return GaugeColors();
  }
});