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 createState() { return _PermissionSettingPage(); } } class _PermissionSettingPage extends State { @override void initState() { super.initState(); queryPermission(); } List title = [ S.current.dingwei, S.current.tongzhi, S.current.xiangji, S.current.cunchu, ]; List 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 contents = [ S.current.weizhitishixinxi, S.current.tongzhitishixinxi, S.current.xiangjitishixinxi, S.current.cunchutishixinxi, ]; List 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 titleTips = [ S.current.nindingweiquanxianweiyunxu, S.current.ninxiaoxiquanxianweikaiqi, S.current.ninxiangjiquanxianweikaiqi, S.current.nincunchuquanxianweikaiqi, ]; List contentTips = [ S.current.weilexiangnintuijianfujindemendianxinxi, S.current.weilejishishoudaohuodongxiaoxi, S.current.weilekaipaizhaoxuanzhetouxiang, S.current.weilexuanzhezhaopianhuancun, ]; Map permissionSwitch = {}; List 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], position); }, child: buildPermissionItem(title[position], icons[position], contents[position], permissions[position], position), ); }, ), ), ); } openPermission(Permission permission, int position) async { if ((permissionSwitch.containsKey(permission) && permissionSwitch[permission])) { 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.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.w), 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.w, ), 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) { if (boo) { requestDialog(position); } else { openPermission(permission, position); } }, ), ], ), ); } }