当前位置: 首页 > news >正文

flutter 使用wechat_assets_picker的权限检测

https://pub.dev/packages/wechat_assets_picker

AssetPicker.pickAssets之前进行权限检查

  pickImages() async {try {if (PermissionState.authorized != await AssetPicker.permissionCheck()) {PermissionUtil.showAllPermissions(Permission.storage, 1);return;}final List<AssetEntity>? result = await AssetPicker.pickAssets(context,pickerConfig: AssetPickerConfig(maxAssets: widget.maxAssets + 1 - saveResult.length,requestType: RequestType.image,filterOptions: FilterOptionGroup(imageOption: const FilterOption(needTitle: true,),),pageSize: 30,gridCount: 3,themeColor: Colors.black,specialItemBuilder: (_, entity, index) {return InkWell(onTap: () async {if (!await PermissionUtil.checkAndDoDefault(Permission.camera)) {return;}final AssetEntity? entity = await CameraPicker.pickFromCamera(context,pickerConfig: CameraPickerConfig(enableRecording: false,theme: CameraPicker.themeData(Colors.black,)),locale: const Locale('en'),);if (entity == null) {return;} else {setState(() {saveResult.insert(saveResult.length - 1, entity);});widget.change?.call(saveResult.sublist(0, saveResult.length - 1));AppToast.closeBottomSheet(context);}},child: SizedBox(width: 33.w,height: 28.h,child: Icon(Icons.camera_alt,size: 28.w,color: Colors.white,),));},specialItemPosition: SpecialItemPosition.prepend,textDelegate: const EnglishAssetPickerTextDelegate(),),);if (result != null) {setState(() {saveResult.insertAll(saveResult.length - 1, result);});widget.change?.call(saveResult.sublist(0, saveResult.length - 1));}} catch (e) {PermissionUtil.showAllPermissions(Permission.storage, 1);}}

permission_utils.dart

import 'dart:io';import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:space_whisper_app/config.dart';
import 'package:space_whisper_app/utils/toast.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';class PermissionUtil {static final bool isIos = Platform.isIOS;static final bool isAndroid = Platform.isAndroid;static String appName = Config.appName;// 检查权限 并做相应的处理static Future<bool> checkAndDoDefault(Permission permission) async {final status = await permission.status;if (isIos) {switch (status) {case PermissionStatus.granted:return true;case PermissionStatus.permanentlyDenied:// openAppSettings();showAllPermissions(permission, 1);return false;case PermissionStatus.denied:case PermissionStatus.limited:case PermissionStatus.restricted:default:final newStatus = await permission.request();if (newStatus == PermissionStatus.granted) {return true;}return false;}} else if (isAndroid) {switch (status) {case PermissionStatus.granted:return true;case PermissionStatus.permanentlyDenied:// openAppSettings();showAllPermissions(permission, 1);return false;// 第一次进来的时候case PermissionStatus.denied:showAllPermissions(permission, 2);return false;case PermissionStatus.limited:case PermissionStatus.restricted:default:final newStatus = await permission.request();if (newStatus == PermissionStatus.granted) {return true;}return false;}}return await permission.status == PermissionStatus.granted;}static showAllPermissions(Permission permissions, int type) {String title = "";String desc = "";switch (permissions.value) {// 相机case 1:title = "Do you authorize the '$appName' to access your camera?";desc ="Uploading an avatar requires your camera permission. We will not abuse your permission. Your camera permission is only used when you use our app to upload images.";break;// 通讯录case 2:title = "Do you authorize '$appName' to access your address book?";desc ="Access to address book friends requires your permission to access your address book. We will not abuse your permission. Your address book permissions are only used when you use our app to access your address book friends.";break;// 相册case 6:title = "Do you authorize '$appName' to access your albums?";desc ="Uploading requires your album permission. We will not abuse your permission. Your albums will only be opened if you need to use the picture function (uploading pictures, etc.).";break;// storage 权限case 15:title = "Do you authorize '$appName' to access your albums?";desc ="Uploading requires your album permission. We will not abuse your permission. Your albums will only be opened if you need to use the picture function (uploading pictures, etc.).";break;// 麦克风case 7:title = "Do you authorize '$appName' to use your microphone?";desc ="I just wanted to inform you that sending voice messages requires your microphone permission. And we promise not to abuse your microphone permission, which will be only used when you send voice messages";break;}AppToast.showPermissionDialog(Container(padding: EdgeInsets.all(20.w),decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.all(Radius.circular(10.w)),),child: Column(children: [Text(title,style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 20.sp,),),SizedBox(height: 5.w,),Text(desc,style: const TextStyle(color: Colors.black),),SizedBox(height: 5.w,),Row(mainAxisAlignment: MainAxisAlignment.center,children: [InkWell(child: Container(color: Colors.white,padding: EdgeInsets.all(10.w),child: const Text("Cancel",style: TextStyle(color: Color(0xFF4677FF),fontWeight: FontWeight.bold,),),),onTap: () {SmartDialog.dismiss();},),const Spacer(),type == 1? InkWell(child: Container(color: Colors.white,padding: EdgeInsets.all(10.w),child: const Text("Settings",style: TextStyle(color: Color(0xFF4677FF),fontWeight: FontWeight.bold,),),),onTap: () {SmartDialog.dismiss();openAppSettings();},): InkWell(child: Container(color: Colors.white,padding: EdgeInsets.all(10.w),child: const Text("Confirmed",style: TextStyle(color: Color(0xFF4677FF),fontWeight: FontWeight.bold,),),),onTap: () async {SmartDialog.dismiss();await permissions.request();},)],),],)));}static Future<bool> checkStatusAndDoDefault(PermissionState status, Permission permission) async {if (isIos) {switch (status) {// notDetermined 未设置授权case PermissionState.notDetermined:return true;// 该应用程序未被授权访问照片库,用户也无法授予此类权限。case PermissionState.restricted:openAppSettings();return false;// 用户明确拒绝此应用程序访问照片库。case PermissionState.denied:// 用户明确授予此应用程序访问照片库的权限。case PermissionState.authorized:case PermissionState.limited:default:final newStatus = await permission.request();if (newStatus == PermissionStatus.granted) {return true;}return false;}} else if (isAndroid) {switch (status) {case PermissionStatus.granted:return true;default:final newStatus = await permission.request();if (newStatus == PermissionStatus.granted) {return true;} else if (newStatus == PermissionStatus.denied) {return false;} else if (newStatus == PermissionStatus.permanentlyDenied) {openAppSettings();return false;}return false;}}return await permission.status == PermissionStatus.granted;}
}

总结 核心 

使用 wechat_assets_picker 的权限检测,如果报错的情况也进行弹窗提示

PermissionUtil.showAllPermissions(Permission.storage, 1); 只是弹窗提示

    if (PermissionState.authorized != await AssetPicker.permissionCheck()) {PermissionUtil.showAllPermissions(Permission.storage, 1);return;}
   try {if (PermissionState.authorized != await AssetPicker.permissionCheck()) {PermissionUtil.showAllPermissions(Permission.storage, 1);return;}//  await AssetPicker.pickAssets(} catch (e) {PermissionUtil.showAllPermissions(Permission.storage, 1);}

功能二:将相机加入wechat_assets_picker

     final List<AssetEntity>? result = await AssetPicker.pickAssets(context,pickerConfig: AssetPickerConfig(maxAssets: widget.maxAssets + 1 - saveResult.length,requestType: RequestType.image,filterOptions: FilterOptionGroup(imageOption: const FilterOption(needTitle: true,),),pageSize: 30,gridCount: 3,themeColor: Colors.black,specialItemBuilder: (_, entity, index) {return InkWell(onTap: () async {if (!await PermissionUtil.checkAndDoDefault(Permission.camera)) {return;}final AssetEntity? entity = await CameraPicker.pickFromCamera(context,pickerConfig: CameraPickerConfig(enableRecording: false,theme: CameraPicker.themeData(Colors.black,)),locale: const Locale('en'),);if (entity == null) {return;} else {setState(() {saveResult.insert(saveResult.length - 1, entity);});widget.change?.call(saveResult.sublist(0, saveResult.length - 1));AppToast.closeBottomSheet(context);}},child: SizedBox(width: 33.w,height: 28.h,child: Icon(Icons.camera_alt,size: 28.w,color: Colors.white,),));},specialItemPosition: SpecialItemPosition.prepend,textDelegate: const EnglishAssetPickerTextDelegate(),),);

相关文章:

flutter 使用wechat_assets_picker的权限检测

https://pub.dev/packages/wechat_assets_picker AssetPicker.pickAssets之前进行权限检查 pickImages() async {try {if (PermissionState.authorized ! await AssetPicker.permissionCheck()) {PermissionUtil.showAllPermissions(Permission.storage, 1);return;}final Lis…...

Mojo入门案例教程(上手篇)

以下是 Mojo 编程语言入门案例教程&#xff0c;内容包括 Mojo 的基本概念、变量、控制结构、函数等方面&#xff1a; Mojo 的基本概念 1.什么是 Mojo&#xff1f;&#xff1a;Mojo 是一种函数式编程语言&#xff0c;用于开发小型应用程序、脚本和工具。 2.Mojo 的特点&#x…...

如何在window执行mkfile

1、Windows cmd中出现错误&#xff1a;“‘make‘ 不是内部或外部命令&#xff0c;也不是可运行的程序或批处理文件。”的解决方法_windows_是板栗啊-GitCode 开源社区 2、安装cmder&#xff0c;再通过包管理工具下载make...

Nginx 是一个非常流行的 Web 服务器和反向代理服务器

Nginx 是一个非常流行的 Web 服务器和反向代理服务器&#xff0c;以其高性能、稳定性、丰富的功能集和低资源消耗而闻名。下面是一个简化的 Nginx 使用教程&#xff0c;包括基本的安装、配置和一些常见用途。 安装 Nginx 在 Ubuntu/Debian 上安装&#xff1a; sudo apt upda…...

mysql怎么调整缓冲区大小

MySQL中调整缓冲区大小是数据库性能优化的重要一环。缓冲区大小直接影响了数据库的读写性能和响应速度。以下是一些常见的MySQL缓冲区及其调整方法&#xff1a; 一、InnoDB缓冲池&#xff08;InnoDB Buffer Pool&#xff09; InnoDB缓冲池是InnoDB存储引擎用来缓存表数据和索…...

计算机组成原理学习笔记(一)

计算机组成原理 [类型:: [[计算机基础课程]] ] [来源:: [[B站]] ] [主讲人:: [[咸鱼学长]] ] [评价:: ] [知识点:: [[系统软件]] & [[应用软件]] ] [简单解释:: 管理计算机系统的软件&#xff1b; 按照任务需要编写的程序 ] [问题:: ] [知识点:: [[机器字长]] ] [简单…...

Vue3 对跳转 同一路由传入不同参数的页面分别进行缓存

1&#xff1a;使用场景 从列表页跳转至不同的详情页面&#xff0c;对这些详情页面分别进行缓存 2&#xff1a;核心代码 2.1: 配置路由文件 在路由文件里对需要进行缓存的路由对象添加meta 属性 // 需要缓存的详情页面路由 { name: detail, path: /myRouter/detail…...

LinearLayout的测量流程

在日常开发中我们常常使用LinearLayout作为布局Group&#xff0c;本文从其源码实现出发分析测量流程。大家可以带着问题进入下面的分析流程&#xff0c;看看是否能找到答案。 垂直测量 View的测量入口方法是onmeasure方法。LinearLayout的onMeasure方法根据其方向而做不同的处…...

数据无忧:Ubuntu 系统迁移备份全指南

唠唠闲话 最近电脑出现了一些故障&#xff0c;送修期间&#xff0c;不得不在实验室的台式机上重装系统&#xff0c;配环境的过程花费了不少时间。为避免未来处理类似事情时耗费时间&#xff0c;特此整理一些备份策略。 先做以下准备&#xff1a; U盘启动盘&#xff0c;参考 …...

中国IDC圈探访北京•光子1号金融算力中心

今天&#xff0c;“AI”、“大模型”是最炙手可热的话题&#xff0c;全球有海量人群在工作生活中使用大模型&#xff0c;大模型产品涉及多模态&#xff0c;应用范围已涵盖电商、传媒、金融、短视频、制造等众多行业。 而回看2003年的互联网记忆&#xff0c; “上网”“在线”是…...

[Unity入门01] Unity基本操作

参考的傅老师的教程学了一下Unity的基础操作&#xff1a; [傅老師/Unity教學] Unity3D基礎入門 [華梵大學] 遊戲引擎應用基礎(Unity版本) Class#01 移动&#xff1a;鼠标中键旋转&#xff1a;鼠标右键放大&#xff1a;鼠标滚轮飞行模式&#xff1a;右键WASDQEFocus模式&…...

vivado DELAY_VALUE_XPHY、DIFF_TERM

延迟_值_XPHY PORT对象上的DELAY_VALUE_XPHY属性指定要添加的延迟量 Versal XPHY逻辑接口的输入或输出路径。在的早期阶段 opt_design在重新生成高级I/O向导IP时 DELAY_VALUE_XPHY值将从PORT复制到的XPHY实例上 输入或输出路径。Vivado设计套件中存在DRCs&#xff0c;以确保 DE…...

C++语言相关的常见面试题目(三)

1. List底层实现原理 省流&#xff1a; list底层实现了一个双向循环链表。 每个元素&#xff08;或节点&#xff09;包含三个部分&#xff1a;数据域(_M_Storage)、前驱指针(_M_prev)、后继指针(_M_next)。 数据域&#xff1a;存储实际数据。 前驱指针&#xff1a;指向链表中…...

代码随想录-Day53

739. 每日温度 给定一个整数数组 temperatures &#xff0c;表示每天的温度&#xff0c;返回一个数组 answer &#xff0c;其中 answer[i] 是指对于第 i 天&#xff0c;下一个更高温度出现在几天后。如果气温在这之后都不会升高&#xff0c;请在该位置用 0 来代替。 示例 1: …...

Android 如何通过代码实时设置EditTextView光标

背景&#xff1a;换肤框架下&#xff0c;QA进行深色浅色切换说输入框光标颜色没有改变&#xff0c;转UI结果UI说需要修改&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 本来有方法可以设置&#xff0c;但是 设置后未生效。重新进入该页面才生效&#xff01;&a…...

202488读书笔记|《365日创意文案》——无聊的 到底是这世间, 还是自己?懂得忘却的人才能前进

202488读书笔记|《365日创意文案》——无聊的 到底是这世间&#xff0c; 还是自己&#xff1f;懂得忘却的人才能前进 1月2月3月4月5月6月7月8月9月10月11月12月 《365日创意文案》WRITES PUBLISHING&#xff0c;一些日常&#xff0c;是烟火&#xff0c;也是幸福的印记。 当下也…...

iperf3: error - unable to connect to server: No route to host

1.确认iperf3版本是否统一。 2.确认防火墙是否关闭。 关闭防火墙 : systemctl stop firewalld 查看防火墙状态: systemctl status firewalld 3.重新建起链接...

正则表达式中的贪心匹配

在正则表达式中&#xff0c;&#xff1f;既可以表示数量&#xff0c;0次或1次&#xff0c;等效于 {0&#xff0c;1}&#xff0c;也可以跟在其它数量限定符之后&#xff0c;表示非贪心匹配&#xff0c;即匹配时匹配搜索到的尽可能短的字符串。 下面来看一个例子&#xff1a; T…...

线程相关概念及操作

【1】线程的概念 1.线程-->进程会得到一个内存地址&#xff0c;进程是资源分配的基本单位线程才是真正进程里处理数据与逻辑的东西进程---》被分配一定的资源线程---》利用进程资源处理数据与逻辑 【2】进程和线程关系&#xff1a; 进程与进程之间是竞争关系&#xff0c;竞…...

2024最新版若依-RuoYi-Vue3-PostgreSQL前后端分离项目部署手册教程

项目简介: RuoYi-Vue3-PostgreSQL 是一个基于 RuoYi-Vue3 框架并集成 PostgreSQL 数据库的项目。该项目提供了一套高效的前后端分离的开发解决方案&#xff0c;适用于中小型企业快速构建现代化的企业级应用。此项目结合了 RuoYi-Vue-Postgresql 和 RuoYi-Vue3 的优点&#xff0…...

51c自动驾驶~合集58

我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留&#xff0c;CCA-Attention为LLM长文本建模带来突破性进展 琶洲实验室、华南理工大学联合推出关键上下文感知注意力机制&#xff08;CCA-Attention&#xff09;&#xff0c;…...

盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来

一、破局&#xff1a;PCB行业的时代之问 在数字经济蓬勃发展的浪潮中&#xff0c;PCB&#xff08;印制电路板&#xff09;作为 “电子产品之母”&#xff0c;其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透&#xff0c;PCB行业面临着前所未有的挑战与机遇。产品迭代…...

蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练

前文覆盖了 BLE 扫描的基础概念与经典问题蓝牙 BLE 扫描面试题大全(1)&#xff1a;从基础到实战的深度解析-CSDN博客&#xff0c;但实际面试中&#xff0c;企业更关注候选人对复杂场景的应对能力&#xff08;如多设备并发扫描、低功耗与高发现率的平衡&#xff09;和前沿技术的…...

五年级数学知识边界总结思考-下册

目录 一、背景二、过程1.观察物体小学五年级下册“观察物体”知识点详解&#xff1a;由来、作用与意义**一、知识点核心内容****二、知识点的由来&#xff1a;从生活实践到数学抽象****三、知识的作用&#xff1a;解决实际问题的工具****四、学习的意义&#xff1a;培养核心素养…...

【RockeMQ】第2节|RocketMQ快速实战以及核⼼概念详解(二)

升级Dledger高可用集群 一、主从架构的不足与Dledger的定位 主从架构缺陷 数据备份依赖Slave节点&#xff0c;但无自动故障转移能力&#xff0c;Master宕机后需人工切换&#xff0c;期间消息可能无法读取。Slave仅存储数据&#xff0c;无法主动升级为Master响应请求&#xff…...

多模态大语言模型arxiv论文略读(108)

CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题&#xff1a;CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者&#xff1a;Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

中医有效性探讨

文章目录 西医是如何发展到以生物化学为药理基础的现代医学&#xff1f;传统医学奠基期&#xff08;远古 - 17 世纪&#xff09;近代医学转型期&#xff08;17 世纪 - 19 世纪末&#xff09;​现代医学成熟期&#xff08;20世纪至今&#xff09; 中医的源远流长和一脉相承远古至…...

iview框架主题色的应用

1.下载 less要使用3.0.0以下的版本 npm install less2.7.3 npm install less-loader4.0.52./src/config/theme.js文件 module.exports {yellow: {theme-color: #FDCE04},blue: {theme-color: #547CE7} }在sass中使用theme配置的颜色主题&#xff0c;无需引入&#xff0c;直接可…...

【Android】Android 开发 ADB 常用指令

查看当前连接的设备 adb devices 连接设备 adb connect 设备IP 断开已连接的设备 adb disconnect 设备IP 安装应用 adb install 安装包的路径 卸载应用 adb uninstall 应用包名 查看已安装的应用包名 adb shell pm list packages 查看已安装的第三方应用包名 adb shell pm list…...