-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.dart
More file actions
255 lines (224 loc) · 6.82 KB
/
main.dart
File metadata and controls
255 lines (224 loc) · 6.82 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'drawer_widget.dart';
import 'gauges/analog.dart';
import 'gauges/digital.dart';
import 'image_picker_utils.dart';
import 'settings.dart';
import 'speedreader.dart';
const APP_NAME = 'SPDO';
class GaugeSettings {
final String display;
final double speed;
final double topSpeed;
final int maxSpeed;
final bool showTopSpeed;
final bool showAnalog;
final Widget? background;
GaugeSettings({
required final this.display,
required final this.speed,
required final this.topSpeed,
required final this.maxSpeed,
required final this.showTopSpeed,
required final this.showAnalog,
final this.background,
});
}
Future<void> main() async {
runApp(SPDO_App());
}
// ignore: camel_case_types
class SPDO_App extends StatelessWidget {
@override
Widget build(final BuildContext context) {
return MaterialApp(
title: APP_NAME,
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: SpeedoScaffold(),
debugShowCheckedModeBanner: false,
);
}
}
@immutable
class SpeedoScaffold extends StatelessWidget {
@override
Widget build(final BuildContext context) {
return Scaffold(
drawer: DrawerWidget(),
body: SpeedListenerWidget(),
);
}
}
@immutable
class SpeedListenerWidget extends StatefulWidget {
late final SpeedReader speedReader;
double msToKPH(final double metersPerSecond) {
const secondsPerHour = 60 * 60;
final double metersPerHour = metersPerSecond * secondsPerHour;
return metersPerHour / 1000;
}
double kphToMPH(final double kmph) {
return (kmph / 1.609);
}
String displaySpeed(final double speed, final bool metric) {
return speed.round().toString() + (metric ? "km/h" : "MPH");
}
@override
_SpeedListenerWidgetState createState() => _SpeedListenerWidgetState();
}
class _SpeedListenerWidgetState extends State<SpeedListenerWidget>
with SingleTickerProviderStateMixin {
Settings? settings;
int get maxSpeed => settings?.maxSpeed ?? Settings.DEFAULT_MAX_SPEED;
bool get metric => settings?.metric ?? false;
bool get showTopSpeed => settings?.showTopSpeed ?? false;
bool get analog => settings?.analog ?? true;
bool get digital => settings?.digital ?? true;
var _display = '';
var _speed = 0.0;
var _topSpeed = 0.0;
Image? _background;
late Animation<double>? _animation;
late AnimationController? _animationController;
@override
void dispose() {
settings?.writePreferences();
this.widget.speedReader.cancel();
_animationController?.dispose();
super.dispose();
}
void initAnimationController() {
_animationController = new AnimationController(
duration: const Duration(seconds: 1), vsync: this);
_animation = Tween<double>(begin: 0, end: maxSpeed * 1.1)
.animate(_animationController!)
..addListener(() {
setState(() {
_speed = _animation?.value ?? 0.0;
});
});
_animationController?.addStatusListener((final status) {
switch (status) {
case AnimationStatus.completed:
_animationController?.reverse();
break;
case AnimationStatus.dismissed:
_animationController?.dispose();
_animationController = null;
_animation = null;
break;
default:
break;
}
});
_animationController?.forward();
}
void initSpeedReader() {
this.widget.speedReader = SpeedReader((final Position position) {
setState(() {
if (position.speedAccuracy < 0 || position.speed.isNaN) {
return;
}
final speedKPH = widget.msToKPH(position.speed);
_speed = (metric ? speedKPH : widget.kphToMPH(speedKPH));
if (_topSpeed < _speed) {
_topSpeed = _speed;
}
if (digital) {
_display = widget.displaySpeed(_speed, metric);
} else {
_display = '';
}
print(_display);
});
});
}
@override
Widget build(final BuildContext context) {
// Loading the background has to happen here, instead of in initState,
// because the context (which is used to query the screen size) is not
// available until build() is called.
ImagePickerUtils.getImage(context).then((final value) => setState(() {
_background = value;
}));
// For some reason, trying to initialize properties async in initState()
// causes an error to be thrown when _touching_ any instance of a nullable
// property. This is a stupid race condition and a gross workaround due to
// SharedPreferences.getInstance() being async.
if (settings == null) {
Settings.getInstance().then((final value) {
setState(() {
settings = value;
initAnimationController();
initSpeedReader();
});
});
return _background ??
Center(
child: Text(
APP_NAME,
style: Theme.of(context).textTheme.headline1,
),
);
}
// if we're not animating, use actual speed values
if (_animation != null) {
_speed = _animation?.value ?? 0.0;
}
return GestureDetector(
child: GaugeWidget(
GaugeSettings(
display: _display,
speed: _speed,
showAnalog: analog,
showTopSpeed: showTopSpeed,
topSpeed: _topSpeed,
maxSpeed: maxSpeed,
background: _background),
),
onTap: () => setState(() {
_topSpeed = 0.0;
}));
}
}
class GaugeWidget extends StatelessWidget {
final String display;
final double speed;
final double topSpeed;
final int maxSpeed;
final bool showTopSpeed;
final bool showAnalog;
final Widget? background;
GaugeWidget(final GaugeSettings settings)
: display = settings.display,
speed = settings.speed,
topSpeed = settings.topSpeed,
maxSpeed = settings.maxSpeed,
showTopSpeed = settings.showTopSpeed,
showAnalog = settings.showAnalog,
background = settings.background,
super();
@override
Widget build(final BuildContext context) {
return Center(
child: Stack(alignment: Alignment.topLeft, children: [
if (background != null) ...[Center(child: background)],
settingsDrawerDisclosureIconButtonWidget(context),
if (topSpeed > 0 && showTopSpeed) ...[
// The green top-speed indicator is only shown if
// the top speed is > 0
AnalogGauge(
speed: topSpeed, maxSpeed: maxSpeed, color: Colors.greenAccent),
],
Center(
child: DigitalGauge(value: display),
),
if (showAnalog) ...[AnalogGauge(speed: speed, maxSpeed: maxSpeed)],
]),
);
}
}