You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
6.2 KiB
234 lines
6.2 KiB
import 'dart:io'; |
|
|
|
import 'package:flutter/cupertino.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:huixiang/generated/l10n.dart'; |
|
import 'package:huixiang/view_widget/my_appbar.dart'; |
|
import 'package:huixiang/view_widget/request_permission.dart'; |
|
import 'package:permission_handler/permission_handler.dart'; |
|
|
|
class PermissionSettingPage extends StatefulWidget { |
|
@override |
|
State<StatefulWidget> createState() { |
|
return _PermissionSettingPage(); |
|
} |
|
} |
|
|
|
class _PermissionSettingPage extends State<PermissionSettingPage> { |
|
@override |
|
void initState() { |
|
super.initState(); |
|
|
|
queryPermission(); |
|
} |
|
|
|
List<String> title = [ |
|
S.current.dingwei, |
|
S.current.tongzhi, |
|
S.current.xiangji, |
|
S.current.cunchu, |
|
]; |
|
|
|
List<String> icons = [ |
|
"assets/image/icon_permission_location.webp", |
|
"assets/image/icon_permission_notices.webp", |
|
"assets/image/icon_permission_camera.webp", |
|
"assets/image/icon_permission_storage.webp", |
|
]; |
|
|
|
List<String> contents = [ |
|
S.current.weizhitishixinxi, |
|
S.current.tongzhitishixinxi, |
|
S.current.xiangjitishixinxi, |
|
S.current.cunchutishixinxi, |
|
]; |
|
|
|
List<String> tipIcons = [ |
|
"assets/image/icon_location_permission_tips.webp", |
|
"assets/image/icon_natification_permission_tips.webp", |
|
"assets/image/icon_camera_permission_tips.webp", |
|
"assets/image/icon_storage_permission_tips.webp", |
|
]; |
|
|
|
List<String> titleTips = [ |
|
S.current.nindingweiquanxianweiyunxu, |
|
S.current.ninxiaoxiquanxianweikaiqi, |
|
S.current.ninxiangjiquanxianweikaiqi, |
|
S.current.nincunchuquanxianweikaiqi, |
|
]; |
|
|
|
List<String> contentTips = [ |
|
S.current.weilexiangnintuijianfujindemendianxinxi, |
|
S.current.weilejishishoudaohuodongxiaoxi, |
|
S.current.weilekaipaizhaoxuanzhetouxiang, |
|
S.current.weilexuanzhezhaopianhuancun, |
|
]; |
|
|
|
Map<Permission, bool> permissionSwitch = {}; |
|
|
|
List<Permission> permissions = [ |
|
Permission.location, |
|
Permission.notification, |
|
Permission.camera, |
|
Permission.storage, |
|
]; |
|
|
|
queryPermission() async { |
|
await Future.forEach(permissions, (Permission element) async { |
|
if (await element.isGranted) { |
|
permissionSwitch[element] = true; |
|
} |
|
}); |
|
setState(() {}); |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Scaffold( |
|
appBar: MyAppBar( |
|
title: S.of(context).quanxianshezhi, |
|
titleColor: Colors.black, |
|
background: Color(0xFFF7F7F7), |
|
leadingColor: Colors.black, |
|
), |
|
body: Container( |
|
child: ListView.builder( |
|
itemCount: permissions.length, |
|
itemBuilder: (context, position) { |
|
return InkWell( |
|
onTap: () { |
|
openPermission(permissions[position], position); |
|
}, |
|
child: buildPermissionItem(title[position], icons[position], |
|
contents[position], permissions[position], position), |
|
); |
|
}, |
|
), |
|
), |
|
); |
|
} |
|
|
|
openPermission(Permission permission, int position) async { |
|
if ((permissionSwitch.containsKey(permission) && |
|
(permissionSwitch[permission] ?? false))) { |
|
openAppSettings(); |
|
return; |
|
} |
|
if (await permission.isPermanentlyDenied) { |
|
requestDialog(position); |
|
} else if (await permission.isDenied) { |
|
await permission.request(); |
|
queryPermission(); |
|
} else { |
|
if (Platform.isIOS) { |
|
//去设置中心 |
|
requestDialog(position); |
|
} else { |
|
if (permission == Permission.notification) { |
|
requestDialog(position); |
|
} else { |
|
await permission.request(); |
|
queryPermission(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
requestDialog(int position) { |
|
showCupertinoDialog( |
|
context: context, |
|
builder: (context) { |
|
return RequestPermission( |
|
tipIcons[position], |
|
titleTips[position], |
|
contentTips[position], |
|
S.of(context).kaiqiquanxian, |
|
(result) async { |
|
if (result) { |
|
await openAppSettings(); |
|
queryPermission(); |
|
} |
|
}, |
|
heightRatioWithWidth: 0.82, |
|
); |
|
}, |
|
); |
|
} |
|
|
|
Widget buildPermissionItem( |
|
title, icon, content, Permission permission, int position) { |
|
return Container( |
|
margin: EdgeInsets.fromLTRB(16, 8, 16, 8), |
|
padding: EdgeInsets.fromLTRB(20, 16, 20, 16), |
|
decoration: BoxDecoration( |
|
color: Colors.white, |
|
borderRadius: BorderRadius.circular(8), |
|
boxShadow: [ |
|
BoxShadow( |
|
color: Colors.black.withAlpha(12), |
|
offset: Offset(0, 3), |
|
blurRadius: 14, |
|
spreadRadius: 0, |
|
) |
|
], |
|
), |
|
child: Row( |
|
mainAxisAlignment: MainAxisAlignment.center, |
|
crossAxisAlignment: CrossAxisAlignment.center, |
|
children: [ |
|
Expanded( |
|
child: Column( |
|
children: [ |
|
Row( |
|
children: [ |
|
Text( |
|
title, |
|
style: TextStyle( |
|
color: Colors.black, |
|
fontSize: 16, |
|
fontWeight: FontWeight.bold), |
|
), |
|
SizedBox( |
|
width: 8, |
|
), |
|
Image.asset( |
|
icon, |
|
width: 22, |
|
height: 22, |
|
), |
|
], |
|
), |
|
SizedBox( |
|
height: 8, |
|
), |
|
Text( |
|
content, |
|
overflow: TextOverflow.ellipsis, |
|
maxLines: 2, |
|
style: TextStyle( |
|
color: Color(0xFF353535), |
|
fontSize: 12, |
|
), |
|
), |
|
], |
|
), |
|
), |
|
SizedBox( |
|
width: 32, |
|
), |
|
CupertinoSwitch( |
|
value: (permissionSwitch.containsKey(permission) && |
|
(permissionSwitch[permission] ?? false)), |
|
onChanged: (boo) { |
|
if (boo) { |
|
requestDialog(position); |
|
} else { |
|
openPermission(permission, position); |
|
} |
|
}, |
|
), |
|
], |
|
), |
|
); |
|
} |
|
}
|
|
|