313插件:动态权限permission_handler_flutter
动态权限permission_handler
dependencies:
permission_handler: ^5.0.0+hotfix.6
使用
1.配置权限
Android
在android\app\src\main\AndroidManifest.xml
中配置,uses-permission
就是一个个权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kill_attendance">
<!-- 申请Android权限-->
<!--网络访问-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Permissions options for the `contacts` group -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Permissions options for the `storage` group -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Permissions options for the `camera` group -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Permissions options for the `location` group -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!-- Permissions options for the `microphone` or `speech` group -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- app名称,图标 -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="沙雕考勤"
android:icon="@mipmap/icon">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
2.动态申请权限
目前这个插件改版了好几次,不同版本处理方法也不一样
引入
import 'package:permission_handler/permission_handler.dart';
4.3.0
4.4.x就不一样了,注意自己lock的版本
- 权限列表:
PermissionGroup
中的字段 - 权限状态列表:
PermissionStatus
中字段 - 打开权限设置页面:
await PermissionHandler().openAppSettings();
- 申请权限
await Map<PermissionGroup, PermissionStatus> map= PermissionHandler().requestPermissions([ 权限列表])
//可以使用then,用权限获取状态
- 获取权限状态
PermissionStatus contactsPermStatus = await PermissionHandler().checkPermissionStatus(PermissionGroup.contacts);
案例:
///请求权限
void _requestPermission() async {
debugPrint("进入闪屏页面");
// 申请权限
// PermissionStatus storageStatus ;
PermissionStatus cameraStatus;
await PermissionHandler().requestPermissions(
[ PermissionGroup.camera]).then((value) {
debugPrint("返回:$value");
// storageStatus=value[PermissionGroup.storage];
cameraStatus=value[PermissionGroup.camera];
});
debugPrint("请求权限,并获取权限:$cameraStatus");
//校验权限
if (cameraStatus == PermissionStatus.granted) {
debugPrint("校验权限:用户都同意了");
//用户都同意了(用&&)
///权限都申请成功初始化闪屏
_initSplash();
} else if ( cameraStatus == PermissionStatus.denied) {
debugPrint("校验权限:有任何一组权限被用户拒绝");
//用户拒绝了(用||)
///有任何一组权限被用户拒绝
//拼接提示权限文本
StringBuffer sb = new StringBuffer();
sb.write(cameraStatus == PermissionStatus.denied ? "相机," : "");
String tip = Utils.removePostfix(sb.toString(), ",");
Utils.showCustomDialog(
context,
ConfirmDialog(
"您拒绝了应用的必要权限:\n[$tip],是否重新申请?",
canBackDismiss: false,
confirmCallback: () => _requestPermission(),
cancelCallback: () => SystemNavigator.pop(),
));
} else if ( cameraStatus == PermissionStatus.neverAskAgain) {
debugPrint("校验权限:有权限永久拒绝");
//有权限永久拒绝(用||)
///有任何一组权限选了不再提示
//拼接提示权限文本
StringBuffer sb = new StringBuffer();
sb.write(cameraStatus == PermissionStatus.neverAskAgain ? "相机," : "");
String tip = Utils.removePostfix(sb.toString(), ",");
Utils.showCustomDialog(
context,
ConfirmDialog(
"您禁用了应用的必要权限:\n[$tip],请到设置里允许?",
canBackDismiss: false,
confirmText: "去设置",
confirmCallback: () async {
await PermissionHandler().openAppSettings(); //打开设置页面
SystemNavigator.pop();
},
cancelCallback: () => SystemNavigator.pop(),
));
}
}
5.0.0
- 权限列表:
Permission
中的字段 - 权限状态列表:
PermissionStatus
中字段 - 打开权限设置页面:
openAppSettings();
- 申请权限
await [权限列表].request();
//可以使用then,用权限获取状态
- 获取权限状态
await Permission.camera.status
- 判断权限状态:
await Permission.camera.isDenied|isGranted
等
案例
///请求权限
void _requestPermission() async {
debugPrint("进入闪屏页面");
// 申请权限
// PermissionStatus cameraStatus;
await [Permission.camera].request();
// .then((value){
//设置申请后的结果
// cameraStatus=value[Permission.camera];
// });
//或者直接调用:
debugPrint("请求权限,并获取权限");
if(await Permission.camera.isDenied){
}
//校验权限
if (await Permission.camera.isGranted) {
debugPrint("校验权限:用户都同意了");
//用户都同意了(用&&)
///权限都申请成功初始化闪屏
_initSplash();
} else if ( await Permission.camera.isDenied) {
debugPrint("校验权限:有任何一组权限被用户拒绝");
//用户拒绝了(用||)
///有任何一组权限被用户拒绝
//拼接提示权限文本
StringBuffer sb = new StringBuffer();
sb.write(await Permission.camera.isDenied? "相机," : "");
String tip = Utils.removePostfix(sb.toString(), ",");
Utils.showCustomDialog(
context,
ConfirmDialog(
"您拒绝了应用的必要权限:\n[$tip],是否重新申请?",
canBackDismiss: false,
confirmCallback: () => _requestPermission(),
cancelCallback: () => SystemNavigator.pop(),
));
} else if ( await Permission.camera.isPermanentlyDenied) {
debugPrint("校验权限:有权限永久拒绝");
//有权限永久拒绝(用||)
///有任何一组权限选了不再提示
//拼接提示权限文本
StringBuffer sb = new StringBuffer();
sb.write( await Permission.camera.isPermanentlyDenied ? "相机," : "");
String tip = Utils.removePostfix(sb.toString(), ",");
Utils.showCustomDialog(
context,
ConfirmDialog(
"您禁用了应用的必要权限:\n[$tip],请到设置里允许?",
canBackDismiss: false,
confirmText: "去设置",
confirmCallback: () async {
await openAppSettings(); //打开设置页面
SystemNavigator.pop();
},
cancelCallback: () => SystemNavigator.pop(),
));
}
}