Created
July 10, 2020 18:12
-
-
Save GursheeshSingh/608f1af5883c57547bb53d168424440a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GetImagePermission { | |
bool granted = false; | |
Permission _permission; | |
final String subHeading; | |
GetImagePermission.gallery( | |
{this.subHeading = "Photos permission is needed to select photos"}) { | |
if (Platform.isIOS) { | |
_permission = Permission.photos; | |
} else { | |
_permission = Permission.storage; | |
} | |
} | |
GetImagePermission.camera( | |
{this.subHeading = "Cameara permission is needed to click photos"}) { | |
_permission = Permission.camera; | |
} | |
Future<void> getPermission(context) async { | |
PermissionStatus permissionStatus = await _permission.status; | |
if (permissionStatus == PermissionStatus.restricted) { | |
_showOpenAppSettingsDialog(context, subHeading); | |
permissionStatus = await _permission.status; | |
if (permissionStatus != PermissionStatus.granted) { | |
//Only continue if permission granted | |
return; | |
} | |
} | |
if (permissionStatus == PermissionStatus.permanentlyDenied) { | |
_showOpenAppSettingsDialog(context, subHeading); | |
permissionStatus = await _permission.status; | |
if (permissionStatus != PermissionStatus.granted) { | |
//Only continue if permission granted | |
return; | |
} | |
} | |
if (permissionStatus == PermissionStatus.undetermined) { | |
permissionStatus = await _permission.request(); | |
if (permissionStatus != PermissionStatus.granted) { | |
//Only continue if permission granted | |
return; | |
} | |
} | |
if (permissionStatus == PermissionStatus.denied) { | |
if (Platform.isIOS) { | |
_showOpenAppSettingsDialog(context, subHeading); | |
} else { | |
permissionStatus = await _permission.request(); | |
} | |
if (permissionStatus != PermissionStatus.granted) { | |
//Only continue if permission granted | |
return; | |
} | |
} | |
if (permissionStatus == PermissionStatus.granted) { | |
granted = true; | |
return; | |
} | |
} | |
_showOpenAppSettingsDialog(context, String subHeading) { | |
return CustomDialog.show( | |
context, | |
'Permission needed', | |
subHeading, | |
'Open settings', | |
openAppSettings, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment