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/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.png", "assets/image/icon_permission_notices.png", "assets/image/icon_permission_camera.png", "assets/image/icon_permission_storage.png", ]; List contents = [ S.current.weizhitishixinxi, S.current.tongzhitishixinxi, S.current.xiangjitishixinxi, S.current.cunchutishixinxi, ]; 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: AppBar( title: Text( S.of(context).quanxianshezhi, style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, ), ), centerTitle: false, backgroundColor: Color(0xFFF7F7F7), elevation: 0, leading: GestureDetector( onTap: () { Navigator.of(context).pop(); }, child: Container( alignment: Alignment.centerRight, margin: EdgeInsets.only(left: 10.w), padding: EdgeInsets.all(6), child: Icon( Icons.arrow_back_ios, color: Colors.black, size: 24, ), ), ), titleSpacing: 2.w, leadingWidth: 56.w, ), 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){}, ), ], ), ); } }