Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:sensors/sensors.dart';
  5.  
  6. void main() => runApp(MyApp());
  7.  
  8. class MyApp extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'Accelerometer',
  13. theme: ThemeData(
  14. primarySwatch: Colors.blue,
  15. ),
  16. home: MyHomePage(title: 'Accelerometer'),
  17. );
  18. }
  19. }
  20.  
  21. class MyHomePage extends StatefulWidget {
  22. MyHomePage({Key key, this.title}) : super(key: key);
  23.  
  24. final String title;
  25.  
  26. @override
  27. _MyHomePageState createState() => _MyHomePageState();
  28. }
  29.  
  30. class _MyHomePageState extends State<MyHomePage> {
  31. // color of the circle
  32. Color color = Colors.greenAccent;
  33.  
  34. // event returned from accelerometer stream
  35. AccelerometerEvent event;
  36.  
  37. // hold a refernce to these, so that they can be disposed
  38. Timer timer;
  39. StreamSubscription accel;
  40.  
  41. // positions and count
  42. double top = 125;
  43. double left;
  44. int count = 0;
  45.  
  46. // variables for screen size
  47. double width;
  48. double height;
  49.  
  50. setColor(AccelerometerEvent event) {
  51. // Calculate Left
  52. double x = ((event.x * 12) + ((width - 100) / 2));
  53. // Calculate Top
  54. double y = event.y * 12 + 125;
  55.  
  56. // find the difference from the target position
  57. var xDiff = x.abs() - ((width - 100) / 2);
  58. var yDiff = y.abs() - 125;
  59.  
  60. // check if the circle is centered, currently allowing a buffer of 3 to make centering easier
  61. if (xDiff.abs() < 3 && yDiff.abs() < 3) {
  62. // set the color and increment count
  63. setState(() {
  64. color = Colors.greenAccent;
  65. count += 1;
  66. });
  67. } else {
  68. // set the color and restart count
  69. setState(() {
  70. color = Colors.red;
  71. count = 0;
  72. });
  73. }
  74. }
  75.  
  76. setPosition(AccelerometerEvent event) {
  77. if (event == null) {
  78. return;
  79. }
  80.  
  81. // When x = 0 it should be centered horizontally
  82. // The left positin should equal (width - 100) / 2
  83. // The greatest absolute value of x is 10, multipling it by 12 allows the left position to move a total of 120 in either direction.
  84. setState(() {
  85. left = ((event.x * 12) + ((width - 100) / 2));
  86. });
  87.  
  88. // When y = 0 it should have a top position matching the target, which we set at 125
  89. setState(() {
  90. top = event.y * 12 + 125;
  91. });
  92. }
  93.  
  94.  
  95. startTimer() {
  96. // if the accelerometer subscription hasn't been created, go ahead and create it
  97. if (accel == null) {
  98. accel = accelerometerEvents.listen((AccelerometerEvent eve) {
  99. setState(() {
  100. event = eve;
  101. });
  102. });
  103. } else {
  104. // it has already ben created so just resume it
  105. accel.resume();
  106. }
  107.  
  108. // Accelerometer events come faster than we need them so a timer is used to only proccess them every 200 milliseconds
  109. if (timer == null || !timer.isActive) {
  110. timer = Timer.periodic(Duration(milliseconds: 200), (_) {
  111. // if count has increased greater than 3 call pause timer to handle success
  112. if (count > 3) {
  113. pauseTimer();
  114. } else {
  115. // proccess the current event
  116. setColor(event);
  117. setPosition(event);
  118. }
  119. });
  120. }
  121. }
  122.  
  123. pauseTimer() {
  124. // stop the timer and pause the accelerometer stream
  125. timer.cancel();
  126. accel.pause();
  127.  
  128. // set the success color and reset the count
  129. setState(() {
  130. count = 0;
  131. color = Colors.green;
  132. });
  133. }
  134.  
  135. @override
  136. void dispose() {
  137. timer?.cancel();
  138. accel?.cancel();
  139. super.dispose();
  140. }
  141.  
  142. @override
  143. Widget build(BuildContext context) {
  144. // get the width and height of the screen
  145. width = MediaQuery.of(context).size.width;
  146. height = MediaQuery.of(context).size.height;
  147.  
  148. return Scaffold(
  149. appBar: AppBar(
  150. title: Text(widget.title),
  151. ),
  152. body: Column(
  153. children: [
  154. Padding(
  155. padding: const EdgeInsets.all(8.0),
  156. child: Text('Keep the circle in the center for 1 second'),
  157. ),
  158. Stack(
  159. children: [
  160. // This empty container is given a width and height to set the size of the stack
  161. Container(
  162. height: height / 2,
  163. width: width,
  164. ),
  165.  
  166. // Create the outer target circle wrapped in a Position
  167. Positioned(
  168. // positioned 50 from the top of the stack
  169. // and centered horizontally, left = (ScreenWidth - Container width) / 2
  170. top: 50,
  171. left: (width - 250) / 2,
  172. child: Container(
  173. height: 250,
  174. width: 250,
  175. decoration: BoxDecoration(
  176. border: Border.all(color: Colors.red, width: 2.0),
  177. borderRadius: BorderRadius.circular(125),
  178. ),
  179. ),
  180. ),
  181. // This is the colored circle that will be moved by the accelerometer
  182. // the top and left are variables that will be set
  183. Positioned(
  184. top: top,
  185. left: left ?? (width - 100) / 2,
  186. // the container has a color and is wrappeed in a ClipOval to make it round
  187. child: ClipOval(
  188. child: Container(
  189. width: 100,
  190. height: 100,
  191. color: color,
  192. ),
  193. ),
  194. ),
  195. // inner target circle wrapped in a Position
  196. Positioned(
  197. top: 125,
  198. left: (width - 100) / 2,
  199. child: Container(
  200. height: 100,
  201. width: 100,
  202. decoration: BoxDecoration(
  203. border: Border.all(color: Colors.green, width: 2.0),
  204. borderRadius: BorderRadius.circular(50),
  205. ),
  206. ),
  207. ),
  208. ],
  209. ),
  210. Text('x: ${(event?.x ?? 0).toStringAsFixed(3)}'),
  211. Text('y: ${(event?.y ?? 0).toStringAsFixed(3)}'),
  212. Padding(
  213. padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
  214. child: RaisedButton(
  215. onPressed: startTimer,
  216. child: Text('Begin'),
  217. color: Theme.of(context).primaryColor,
  218. textColor: Colors.white,
  219. ),
  220. )
  221. ],
  222. ),
  223. );
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement