Guest User

Untitled

a guest
Aug 20th, 2019
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class NewStopWatch extends StatefulWidget {
  2.  
  3. @override
  4. _NewStopWatchState createState() => new _NewStopWatchState();
  5. }
  6.  
  7. class _NewStopWatchState extends State<NewStopWatch> {
  8.  
  9. static _NewStopWatchState stopwatch;
  10. Stopwatch watch = new Stopwatch();
  11. Timer timer;
  12. bool startStop = true;
  13. static String elapsedTime = '';
  14. String duration;
  15.  
  16. updateTime(Timer timer) {
  17. if (watch.isRunning) {
  18. setState(() {
  19. elapsedTime = transformMilliSeconds(watch.elapsedMilliseconds);
  20. User.getCurrentUser().getCurrentActivity().setElapsedTime(elapsedTime);
  21. });
  22. }
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26.  
  27. return new Container(
  28. padding: EdgeInsets.all(20.0),
  29. child: new Column(
  30. children: <Widget>[
  31. new Text(elapsedTime, style: new TextStyle(fontSize: 25.0)),
  32. SizedBox(height: 20.0),
  33. new Row(
  34. mainAxisAlignment: MainAxisAlignment.center,
  35. children: <Widget>[
  36. FloatingActionButton(
  37. heroTag: "btn1",
  38. backgroundColor: Colors.red,
  39. onPressed: () => startOrStop(),
  40. child: Icon(Icons.pause)),
  41. SizedBox(width: 20.0),
  42. FloatingActionButton(
  43. heroTag: "btn2",
  44. backgroundColor: Colors.green,
  45. onPressed: () => completeActivity(),
  46. child: Icon(Icons.check)),
  47. ],
  48. )
  49. ],
  50. ));
  51. }
  52.  
  53. void initState() {
  54. super.initState();
  55. WidgetsBinding.instance
  56. .addPostFrameCallback((_) => startWatch());
  57. }
  58.  
  59. startOrStop() {
  60. if(startStop) {
  61. startWatch();
  62. } else {
  63. stopWatch();
  64. }
  65. }
  66.  
  67. startWatch() {
  68. setState(() {
  69. startStop = false;
  70. watch.start();
  71. timer = Timer.periodic(Duration(milliseconds: 100), updateTime);
  72. });
  73. }
  74.  
  75. stopWatch() {
  76. setState(() {
  77. startStop = true;
  78. watch.stop();
  79. setTime();
  80. });
  81. }
  82.  
  83. resetWatch() {
  84. watch.reset();
  85. setTime();
  86. }
  87.  
  88. setTime() {
  89. var timeSoFar = watch.elapsedMilliseconds;
  90. setState(() {
  91. elapsedTime = transformMilliSeconds(timeSoFar);
  92. });
  93. }
  94.  
  95. completeActivity() { //do I call activity.stopActivity()?
  96. return showDialog(
  97. context: context,
  98. builder: (context) => new AlertDialog(
  99. title: new Text('Complete Activity?',
  100. style: new TextStyle(color: Colors.black, fontSize: 20.0)),
  101. actions: <Widget>[
  102. new FlatButton(
  103. onPressed: () {
  104. duration = elapsedTime;
  105. print("Current activitiy: ${User.getCurrentUser().getCurrentActivity()}");
  106. // User.getCurrentUser().completeActivity();
  107. User.getCurrentUser().addPastActivity(User.getCurrentUser().getCurrentActivity());
  108. User.getCurrentUser().getCurrentActivity().setStatus(ActivityStatus.completed);
  109. User.getCurrentUser().setCurrentActivity(null);
  110. Navigator.push(context, MaterialPageRoute(builder: (context) => FrontPage()));
  111. // Navigator.popUntil(context, ModalRoute.withName("/"),);
  112. },
  113. child:
  114. new Text('Yes', style: new TextStyle(fontSize: 18.0)),
  115. ),
  116. new FlatButton(
  117. onPressed: () => Navigator.pop(context), // this line dismisses the dialog
  118. child: new Text('No', style: new TextStyle(fontSize: 18.0)),
  119. )
  120. ],
  121. ),
  122. ) ??
  123. false;
  124. }
  125.  
  126. transformMilliSeconds(int milliseconds) {
  127. int hundreds = (milliseconds / 10).truncate();
  128. int seconds = (hundreds / 100).truncate();
  129. int minutes = (seconds / 60).truncate();
  130. int hours = (minutes / 60).truncate();
  131.  
  132. String hoursStr = (hours % 60).toString().padLeft(2, '0');
  133. String minutesStr = (minutes % 60).toString().padLeft(2, '0');
  134. String secondsStr = (seconds % 60).toString().padLeft(2, '0');
  135.  
  136. return "$hoursStr:$minutesStr:$secondsStr";
  137. }
  138.  
  139. static String getElapsedTime() {
  140. return elapsedTime;
  141. }
  142. }
  143.  
  144. class FrontPage extends StatelessWidget {
  145.  
  146. Activity currentActivity;
  147. var recentActivities = <Widget>[];//<Widget>[Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary()];
  148. var upcomingActivities = <Widget>[];//<Widget>[Activity("Jog", "assets/activity_logos/Squat.png", status: ActivityStatus.completed,).getSummary()];
  149.  
  150. @override
  151. Widget build(BuildContext context) {
  152. return Scaffold(
  153. appBar: AppBar(
  154. centerTitle: true,
  155. title: Text('Home'),
  156. actions: <Widget>[
  157. GestureDetector(
  158. child: Align(child:Text("+", style: TextStyle(fontSize: 40)), alignment: Alignment.centerLeft, widthFactor: 2,),
  159. onTap: () => Navigator.pushNamed(context, '/activity/new'),
  160. )
  161. ],
  162. ),
  163. drawer: NavigationDrawer(),
  164. body: Center(
  165. child: Padding(
  166. padding: const EdgeInsets.only(left: 30.0, right: 30.0, top: 30),
  167. child:ListView(
  168. children: <Widget>[
  169. Text('Current Activity:', style: TextStyle(fontSize: 15 ,height: 2, decoration: TextDecoration.underline)),
  170. _returnCurrentActivity(context) ,
  171. Text('Upcoming Activities:', style: TextStyle(fontSize: 15 ,height: 2, decoration: TextDecoration.underline)),
  172. _returnUpcomingActivities(),
  173. Text('Recent Activities:', style: TextStyle(fontSize: 15 ,height: 2, decoration: TextDecoration.underline)),
  174. _returnRecentActivities()
  175. ],
  176. ),
  177. ),
  178. ),
  179. );}
  180.  
  181. class FrontPage extends StatelessWidget {
  182. Stopwatch watch = Stopwatch();
  183. Timer timer = Timer.periodic(Duration(milliseconds: 100), updateTime);
  184. //... code
  185.  
  186. //.. somewhere a NewStopWatch is created
  187. return NewStopWatch(watch, timer);
  188. }
  189. //... code
  190.  
  191.  
  192. class NewStopWatch extends StatefulWidget {
  193.  
  194. Stopwatch watch;
  195. Timer timer;
  196.  
  197. NewStopWatch(this.watch, this.timer);
  198.  
  199. @override
  200. _NewStopWatchState createState() => new _NewStopWatchState();
  201. }
  202.  
  203.  
  204. class _NewStopWatchState extends State<NewStopWatch> {
  205.  
  206. static _NewStopWatchState stopwatch;
  207. bool startStop = true;
  208. static String elapsedTime = '';
  209. String duration;
  210.  
  211. Stopwatch get watch => widget.watch;
  212. Timer get timer => widget.timer;
  213.  
  214. void initState() {
  215. super.initState();
  216.  
  217. WidgetsBinding.instance
  218. .addPostFrameCallback((_) => startWatch());
  219. }
RAW Paste Data