| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_activity_recognition/flutter_activity_recognition.dart';
- import 'package:flutter_activity_recognition/models/permission_request_result.dart';
- import 'package:geolocator/geolocator.dart';
- import 'package:physigo/walking/walking_counter.dart';
- class WalkingPermission extends StatelessWidget {
- WalkingPermission({Key? key}) : super(key: key);
- final FlutterActivityRecognition activityRecognition =
- FlutterActivityRecognition.instance;
- @override
- Widget build(BuildContext context) {
- return(
- FutureBuilder(
- future: Future.wait([isPermissionGrants(), _getPermissions()]),
- builder: (context, snapshot) {
- if (snapshot.connectionState == ConnectionState.waiting) {
- return CircularProgressIndicator();
- } else if (snapshot.hasError) {
- return Text("Error");
- } else {
- return WalkingCounter();
- }
- },
- )
- );
- }
- Future<void> isPermissionGrants() async {
- // Check if the user has granted permission. If not, request permission.
- PermissionRequestResult reqResult;
- reqResult = await activityRecognition.checkPermission();
- if (reqResult == PermissionRequestResult.PERMANENTLY_DENIED) {
- print('Permission is permanently denied.');
- return Future.error('Activity permissions are denied');
- } else if (reqResult == PermissionRequestResult.DENIED) {
- reqResult = await activityRecognition.requestPermission();
- if (reqResult != PermissionRequestResult.GRANTED) {
- print('Permission is denied.');
- return Future.error('Activity permissions are denied');
- }
- }
- }
- Future<void> _getPermissions() async {
- bool serviceEnabled;
- LocationPermission permission;
- // Test if location services are enabled.
- serviceEnabled = await Geolocator.isLocationServiceEnabled();
- if (!serviceEnabled) {
- // Location services are not enabled don't continue
- // accessing the position and request users of the
- // App to enable the location services.
- return Future.error('Location services are disabled.');
- }
- permission = await Geolocator.checkPermission();
- if (permission == LocationPermission.denied) {
- permission = await Geolocator.requestPermission();
- if (permission == LocationPermission.denied) {
- // Permissions are denied, next time you could try
- // requesting permissions again (this is also where
- // Android's shouldShowRequestPermissionRationale
- // returned true. According to Android guidelines
- // your App should show an explanatory UI now.
- return Future.error('Location permissions are denied');
- }
- }
- if (permission == LocationPermission.deniedForever) {
- // Permissions are denied forever, handle appropriately.
- return Future.error(
- 'Location permissions are permanently denied, we cannot request permissions.');
- }
- }
- }
|