You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
car_common_app/lib/utils/app_permission_handler.dart

98 lines
2.8 KiB
Dart

import 'dart:developer';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:mc_common_app/widgets/common_widgets/confirm_dialog.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dialogs_and_bottomsheets.dart';
enum ConfirmAction { CANCEL, ACCEPT }
Future<bool> requestPermissionGranted(context, Permission requestPermissions) async {
var result = await requestPermissions.request();
log("result: $result");
switch (result) {
case PermissionStatus.granted:
// Application has been given permission to use the feature.
return true;
case PermissionStatus.denied:
// Application has been denied permission to use the feature.
return false;
case PermissionStatus.permanentlyDenied:
ConfirmAction? res = await showConfirmDialogs(context, 'This permission was denied permanently, Please go to settings and allow. ', 'Open App Setting', 'Cancel');
if (res == ConfirmAction.ACCEPT) {
return false;
} else if (res == ConfirmAction.CANCEL) {
return false;
}
return false;
case PermissionStatus.restricted:
// iOS has restricted access to a specific feature.
return false;
default:
return false;
}
}
class AppPermissions {
static void location(Function(bool) completion) {
Permission.location.isGranted.then((isGranted) {
if (!isGranted) {
Permission.location.request().then((granted) {
completion(granted == PermissionStatus.granted);
});
}
completion(isGranted);
});
}
static void checkAll(Function(bool) completion) {
[Permission.location].request().then((value) {
bool allGranted = false;
for (var element in value.values) {
allGranted = allGranted && element == PermissionStatus.granted;
}
completion(allGranted);
});
}
static getDialog(BuildContext context) {
return showDialog(
context: context,
builder: (BuildContext cxt) => ConfirmDialog(
message: "You need to give storage permission to select files.",
onTap: () {
Navigator.pop(context);
},
),
);
}
static Future<bool> checkStoragePermissions(BuildContext context) async {
bool permissionStatus;
final deviceInfo = await DeviceInfoPlugin().androidInfo;
if (deviceInfo.version.sdkInt! > 32) {
permissionStatus = await Permission.photos.request().isGranted;
if (permissionStatus) {
return true;
} else {
getDialog(context);
return false;
}
} else {
permissionStatus = await Permission.storage.request().isGranted;
if (permissionStatus) {
return true;
} else {
getDialog(context);
return false;
}
}
}
}