walking_permission.dart 3.0 KB

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