-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.dart
77 lines (67 loc) · 1.74 KB
/
main.dart
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
import 'package:euler_angles/euler_angles.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math.dart' show radians;
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Home(),
),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
// calculate average values from moving window
var pitch =
window.map((it) => it.pitch).reduce((a, b) => a + b) / window.length;
var yaw =
window.map((it) => it.yaw).reduce((a, b) => a + b) / window.length;
return Stack(
children: <Widget>[
if (window != null)
Positioned.fill(
child: Center(
child: Transform.translate(
offset: Offset(
radians(yaw) * size.width / 2,
-radians(pitch) * size.height / 2,
),
child: Image.asset(
'assets/rocket.png',
width: 50,
),
),
),
),
],
);
}
@override
void initState() {
super.initState();
EulerAngles.addListener(onData);
}
var window = <EulerSensorData>[];
void onData(EulerSensorData value) {
setState(() {
if (window.length > 50) {
window.removeAt(0);
}
window.add(value);
});
}
}