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.

236 lines
6.3 KiB

import 'dart:io';
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/request_permission.dart';
import 'package:permission_handler/permission_handler.dart';
4 years ago
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,
];
4 years ago
List<String> icons = [
"assets/image/icon_permission_location.png",
"assets/image/icon_permission_notices.png",
"assets/image/icon_permission_camera.png",
"assets/image/icon_permission_storage.png",
];
4 years ago
List<String> contents = [
S.current.weizhitishixinxi,
S.current.tongzhitishixinxi,
S.current.xiangjitishixinxi,
S.current.cunchutishixinxi,
];
4 years ago
List<String> tipIcons = [
"assets/image/icon_location_permission_tips.png",
"assets/image/icon_notification_permission_tips.png",
"assets/image/icon_camera_permission_tips.png",
"assets/image/icon_storage_permission_tips.png",
];
List<String> titleTips = [
3 years ago
S.current.nindingweiquanxianweiyunxu,
S.current.ninxiaoxiquanxianweikaiqi,
S.current.ninxiangjiquanxianweikaiqi,
S.current.nincunchuquanxianweikaiqi,
4 years ago
];
List<String> contentTips = [
3 years ago
S.current.weilexiangnintuijianfujindemendianxinxi,
S.current.weilejishishoudaohuodongxiaoxi,
S.current.weilekaipaizhaoxuanzhetouxiang,
S.current.weilexuanzhezhaopianhuancun,
4 years ago
];
Map<Permission, bool> permissionSwitch = {};
List<Permission> permissions = [
Permission.location,
Permission.notification,
Permission.camera,
Permission.storage,
];
queryPermission() async {
await permissions.forEach((element) async {
if (await element.isGranted) {
permissionSwitch[element] = true;
}
});
setState(() {});
}
4 years ago
@override
Widget build(BuildContext context) {
return Scaffold(
4 years ago
appBar: MyAppBar(
title: S.of(context).quanxianshezhi,
titleColor: Colors.black,
background: Color(0xFFF7F7F7),
leadingColor: Colors.black,
4 years ago
),
body: Container(
child: ListView.builder(
4 years ago
itemCount: permissions.length,
itemBuilder: (context, position) {
return InkWell(
onTap: () {
openPermission(permissions[position], position);
},
child: buildPermissionItem(title[position], icons[position],
contents[position], permissions[position], position),
);
},
),
4 years ago
),
);
}
4 years ago
openPermission(Permission permission, int position) async {
if ((permissionSwitch.containsKey(permission) &&
permissionSwitch[permission])) {
openAppSettings();
return;
}
4 years ago
if (await permission.isPermanentlyDenied) {
requestDialog(position);
} else if (await permission.isUndetermined) {
await permission.request();
queryPermission();
} else {
if (Platform.isIOS) {
//去设置中心
4 years ago
requestDialog(position);
} else {
if (permission == Permission.notification) {
4 years ago
requestDialog(position);
} else {
await permission.request();
queryPermission();
}
}
}
}
4 years ago
4 years ago
requestDialog(int position) {
showCupertinoDialog(
4 years ago
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(
4 years ago
title, icon, content, Permission permission, int position) {
4 years ago
return Container(
margin: EdgeInsets.fromLTRB(16.w, 8.h, 16.w, 8.h),
padding: EdgeInsets.fromLTRB(20.w, 16.h, 20.w, 16.h),
4 years ago
decoration: BoxDecoration(
color: Colors.white,
4 years ago
borderRadius: BorderRadius.circular(8.w),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0,
)
],
),
4 years ago
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Column(
children: [
Row(
children: [
Text(
title,
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
4 years ago
fontWeight: FontWeight.bold),
),
SizedBox(
3 years ago
width: 8.w,
4 years ago
),
Image.asset(
icon,
width: 22.w,
height: 22.h,
4 years ago
),
4 years ago
],
),
SizedBox(
height: 8.h,
4 years ago
),
Text(
content,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
color: Color(0xFF353535),
fontSize: 12.sp,
4 years ago
),
),
],
),
),
SizedBox(
width: 32.w,
4 years ago
),
CupertinoSwitch(
value: (permissionSwitch.containsKey(permission) &&
permissionSwitch[permission]),
4 years ago
onChanged: (boo) {
if (boo) {
requestDialog(position);
} else {
openPermission(permission, position);
}
},
4 years ago
),
],
),
);
}
}