main.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:pedometer/pedometer.dart';
  4. String formatDate(DateTime d) {
  5. return d.toString().substring(0, 19);
  6. }
  7. void main() {
  8. runApp(MyApp());
  9. }
  10. class MyApp extends StatefulWidget {
  11. @override
  12. _MyAppState createState() => _MyAppState();
  13. }
  14. class _MyAppState extends State<MyApp> {
  15. Stream<StepCount> _stepCountStream;
  16. Stream<PedestrianStatus> _pedestrianStatusStream;
  17. String _status = '?', _steps = '?';
  18. @override
  19. void initState() {
  20. super.initState();
  21. initPlatformState();
  22. }
  23. void onStepCount(StepCount event) {
  24. print(event);
  25. setState(() {
  26. _steps = event.steps.toString();
  27. });
  28. }
  29. void onPedestrianStatusChanged(PedestrianStatus event) {
  30. print(event);
  31. setState(() {
  32. _status = event.status;
  33. });
  34. }
  35. void onPedestrianStatusError(error) {
  36. print('onPedestrianStatusError: $error');
  37. setState(() {
  38. _status = 'Pedestrian Status not available';
  39. });
  40. print(_status);
  41. }
  42. void onStepCountError(error) {
  43. print('onStepCountError: $error');
  44. setState(() {
  45. _steps = 'Step Count not available';
  46. });
  47. }
  48. void initPlatformState() {
  49. _pedestrianStatusStream = Pedometer.pedestrianStatusStream;
  50. _pedestrianStatusStream
  51. .listen(onPedestrianStatusChanged)
  52. .onError(onPedestrianStatusError);
  53. _stepCountStream = Pedometer.stepCountStream;
  54. _stepCountStream.listen(onStepCount).onError(onStepCountError);
  55. if (!mounted) return;
  56. }
  57. @override
  58. Widget build(BuildContext context) {
  59. return MaterialApp(
  60. home: Scaffold(
  61. appBar: AppBar(
  62. title: const Text('Pedometer example app'),
  63. ),
  64. body: Center(
  65. child: Column(
  66. mainAxisAlignment: MainAxisAlignment.center,
  67. children: <Widget>[
  68. Text(
  69. 'Steps taken:',
  70. style: TextStyle(fontSize: 30),
  71. ),
  72. Text(
  73. _steps,
  74. style: TextStyle(fontSize: 60),
  75. ),
  76. Divider(
  77. height: 100,
  78. thickness: 0,
  79. color: Colors.white,
  80. ),
  81. Text(
  82. 'Pedestrian status:',
  83. style: TextStyle(fontSize: 30),
  84. ),
  85. Icon(
  86. _status == 'walking'
  87. ? Icons.directions_walk
  88. : _status == 'stopped'
  89. ? Icons.accessibility_new
  90. : Icons.error,
  91. size: 100,
  92. ),
  93. Center(
  94. child: Text(
  95. _status,
  96. style: _status == 'walking' || _status == 'stopped'
  97. ? TextStyle(fontSize: 30)
  98. : TextStyle(fontSize: 20, color: Colors.red),
  99. ),
  100. )
  101. ],
  102. ),
  103. ),
  104. ),
  105. );
  106. }
  107. }