aboutsummaryrefslogtreecommitdiffstats
path: root/lib/page_dashboard.dart
blob: c35441523e0fe76df0157a00a8916f1b81b1bf13 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_homescreen/layout_size_helper.dart';

// The Dashboard page.
class DashboardPage extends StatefulWidget {
  DashboardPage({Key? key}) : super(key: key);

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

class _DashboardPageState extends State<DashboardPage> {
  late Timer _timer;

  double speed = 20;
  // between 0 and 1.0
  double rpm = 0.2;
  // between 0 and 1.0
  double fuel = 0.2;

  @override
  void initState() {
    _timer = new Timer.periodic(
      Duration(milliseconds: 10),
      (Timer timer) {
        setState(() {
          double now = DateTime.now().millisecondsSinceEpoch / 2000;
          speed = 50 + 40 * sin(now);
          rpm = 0.5 + sin(now) / 3.0;
          fuel = 0.6 + cos(now) / 4.0;
        });
      },
    );
    // Animate the values for the demo.
    // Eventually, we will get the state of the car from the API.
    super.initState();
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var sizeHelper = LayoutSizeHelper(context);
    return Container(
      decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topRight,
              end: Alignment.bottomLeft,
              colors: [Colors.teal.shade900, Colors.grey.shade900])),
      constraints: BoxConstraints.expand(),
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Container(
                height: sizeHelper.largeIconSize * 1,
                width: sizeHelper.largeIconSize * 2,
                decoration: BoxDecoration(
                    border: Border.all(
                      color: Theme.of(context).primaryColorLight,
                      width: sizeHelper.defaultBorder,
                    ),
                    borderRadius: BorderRadius.all(
                        Radius.circular(sizeHelper.largeIconSize / 2.0))),
                child: Center(
                  child: Text(
                    '${speed.floor()} kpm',
                    style: Theme.of(context).textTheme.headline2,
                  ),
                ),
              ),
              _RPMWidget(rpm),
              _FuelWidget(fuel),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  _TireWidget('Left front tire', 21, CrossAxisAlignment.end),
                  _TireWidget('Left rear tire', 23, CrossAxisAlignment.end),
                ],
              ),
              Image.asset(
                'images/HMI_Dashboard_Car_720.png',
                width: 2.0 * sizeHelper.largeIconSize,
                fit: BoxFit.contain,
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _TireWidget('Right front tire', 21, CrossAxisAlignment.start),
                  _TireWidget('Right rear tire', 23, CrossAxisAlignment.start),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}

// The RPM indicator.
class _RPMWidget extends StatelessWidget {
  final double rpm;

  _RPMWidget(this.rpm);

  @override
  Widget build(BuildContext context) {
    var sizeHelper = LayoutSizeHelper(context);
    return Stack(
      alignment: Alignment.center,
      children: [
        Text(
          'RPM',
          style: Theme.of(context).textTheme.headline4,
        ),
        Container(
          height: sizeHelper.largeIconSize * 1.5,
          width: sizeHelper.largeIconSize * 1.5,
          child: RotatedBox(
            quarterTurns: 2,
            child: CircularProgressIndicator(
              value: rpm,
              color: HSLColor.fromColor(Colors.redAccent)
                  .withSaturation(rpm)
                  .toColor(),
              strokeWidth: sizeHelper.largeIconSize / 2.0,
              semanticsLabel: 'RPM indicator',
            ),
          ),
        )
      ],
    );
  }
}

// The fuel indicator.
class _FuelWidget extends StatelessWidget {
  final double fuel;

  _FuelWidget(this.fuel);

  @override
  Widget build(BuildContext context) {
    var sizeHelper = LayoutSizeHelper(context);
    return Row(
      children: [
        Container(
          height: sizeHelper.largeIconSize / 4.0,
          width: sizeHelper.largeIconSize / 2.0,
          child: Center(
            child: Text(
              'Fuel',
              style: Theme.of(context).textTheme.headline4,
            ),
          ),
        ),
        Container(
          height: sizeHelper.largeIconSize / 4.0,
          width: sizeHelper.largeIconSize * 1.5,
          margin: EdgeInsets.fromLTRB(
              0, sizeHelper.largePadding, 0, sizeHelper.largePadding),
          child: LinearProgressIndicator(
            value: fuel,
            color: HSLColor.fromColor(Colors.blueAccent)
                .withSaturation(fuel)
                .toColor(),
            semanticsLabel: 'RPM indicator',
          ),
        )
      ],
    );
  }
}

// The small indicator for the state of each tire.
class _TireWidget extends StatelessWidget {
  final String label;
  final int value;
  final CrossAxisAlignment crossAlign;

  _TireWidget(this.label, this.value, this.crossAlign);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: crossAlign,
      children: [
        Text(
          label,
          style: Theme.of(context).textTheme.headline6,
        ),
        Text(
          '$value PSI',
          style: Theme.of(context).textTheme.headline4,
        ),
      ],
    );
  }
}