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.
 
 
 
 
 
 

204 lines
5.5 KiB

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:flutter_screenutil/flutter_screenutil.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.png",
"assets/image/icon_permission_notices.png",
"assets/image/icon_permission_camera.png",
"assets/image/icon_permission_storage.png",
];
List<String> contents = [
S.current.weizhitishixinxi,
S.current.tongzhitishixinxi,
S.current.xiangjitishixinxi,
S.current.cunchutishixinxi,
];
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(() {});
}
@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]);
},
child: buildPermissionItem(title[position], icons[position],
contents[position], permissions[position]),
);
}),
),
);
}
openPermission(Permission permission) async {
if ((permissionSwitch.containsKey(permission) &&
permissionSwitch[permission])) {
openAppSettings();
return;
}
if(await permission.isPermanentlyDenied) {
requestDialog();
} else if (await permission.isUndetermined) {
await permission.request();
queryPermission();
} else {
if (Platform.isIOS) {
//去设置中心
requestDialog();
} else {
if (permission == Permission.notification) {
requestDialog();
} else {
await permission.request();
queryPermission();
}
}
}
}
requestDialog() {
showCupertinoDialog(
context: context,
builder: (context) {
return RequestPermission(
"assets/image/icon_permission_location_bg.png",
S.of(context).nindingweiquanxianweiyunxu,
S.of(context).weilexiangnintuijianfujindemendianxinxi,
S.of(context).kaiqiquanxian,
(result) async {
if (result) {
await openAppSettings();
queryPermission();
}
},
heightRatioWithWidth: 0.82,
);
});
}
Widget buildPermissionItem(
title, icon, content, Permission permission) {
return Container(
margin: EdgeInsets.fromLTRB(16.w, 8.h, 16.w, 8.h),
padding: EdgeInsets.fromLTRB(20.w, 16.h, 20.w, 16.h),
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.sp,
fontWeight: FontWeight.bold),
),
SizedBox(
width: 8,
),
Image.asset(
icon,
width: 22.w,
height: 22.h,
)
],
),
SizedBox(
height: 8.h,
),
Text(
content,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
color: Color(0xFF353535),
fontSize: 12.sp,
),
),
],
),
),
SizedBox(
width: 32.w,
),
CupertinoSwitch(
value: (permissionSwitch.containsKey(permission) &&
permissionSwitch[permission]),
onChanged: (boo){},
),
],
),
);
}
}