walking_page.dart 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_activity_recognition/flutter_activity_recognition.dart';
  3. import 'package:geolocator/geolocator.dart';
  4. import 'package:physigo/walking/widgets/walking.dart';
  5. class WalkingPage extends StatelessWidget {
  6. WalkingPage({Key? key}) : super(key: key);
  7. final FlutterActivityRecognition activityRecognition =
  8. FlutterActivityRecognition.instance;
  9. @override
  10. Widget build(BuildContext context) {
  11. return(
  12. FutureBuilder(
  13. future: Future.wait([isPermissionGrants(), _getPermissions()]),
  14. builder: (context, snapshot) {
  15. if (snapshot.connectionState == ConnectionState.waiting) {
  16. return CircularProgressIndicator();
  17. } else if (snapshot.hasError) {
  18. return Text("Error");
  19. } else {
  20. return Walking();
  21. }
  22. },
  23. )
  24. );
  25. }
  26. Future<void> isPermissionGrants() async {
  27. // Check if the user has granted permission. If not, request permission.
  28. PermissionRequestResult reqResult;
  29. reqResult = await activityRecognition.checkPermission();
  30. if (reqResult == PermissionRequestResult.PERMANENTLY_DENIED) {
  31. print('Permission is permanently denied.');
  32. return Future.error('Activity permissions are denied');
  33. } else if (reqResult == PermissionRequestResult.DENIED) {
  34. reqResult = await activityRecognition.requestPermission();
  35. if (reqResult != PermissionRequestResult.GRANTED) {
  36. print('Permission is denied.');
  37. return Future.error('Activity permissions are denied');
  38. }
  39. }
  40. }
  41. Future<void> _getPermissions() async {
  42. bool serviceEnabled;
  43. LocationPermission permission;
  44. // Test if location services are enabled.
  45. serviceEnabled = await Geolocator.isLocationServiceEnabled();
  46. if (!serviceEnabled) {
  47. // Location services are not enabled don't continue
  48. // accessing the position and request users of the
  49. // App to enable the location services.
  50. return Future.error('Location services are disabled.');
  51. }
  52. permission = await Geolocator.checkPermission();
  53. if (permission == LocationPermission.denied) {
  54. permission = await Geolocator.requestPermission();
  55. if (permission == LocationPermission.denied) {
  56. // Permissions are denied, next time you could try
  57. // requesting permissions again (this is also where
  58. // Android's shouldShowRequestPermissionRationale
  59. // returned true. According to Android guidelines
  60. // your App should show an explanatory UI now.
  61. return Future.error('Location permissions are denied');
  62. }
  63. }
  64. if (permission == LocationPermission.deniedForever) {
  65. // Permissions are denied forever, handle appropriately.
  66. return Future.error(
  67. 'Location permissions are permanently denied, we cannot request permissions.');
  68. }
  69. }
  70. }