当前位置: 首页 > 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…...

如何在看板中体现优先级变化

在看板中有效体现优先级变化的关键措施包括&#xff1a;采用颜色或标签标识优先级、设置任务排序规则、使用独立的优先级列或泳道、结合自动化规则同步优先级变化、建立定期的优先级审查流程。其中&#xff0c;设置任务排序规则尤其重要&#xff0c;因为它让看板视觉上直观地体…...

Python爬虫实战:研究feedparser库相关技术

1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

【快手拥抱开源】通过快手团队开源的 KwaiCoder-AutoThink-preview 解锁大语言模型的潜力

引言&#xff1a; 在人工智能快速发展的浪潮中&#xff0c;快手Kwaipilot团队推出的 KwaiCoder-AutoThink-preview 具有里程碑意义——这是首个公开的AutoThink大语言模型&#xff08;LLM&#xff09;。该模型代表着该领域的重大突破&#xff0c;通过独特方式融合思考与非思考…...

React19源码系列之 事件插件系统

事件类别 事件类型 定义 文档 Event Event 接口表示在 EventTarget 上出现的事件。 Event - Web API | MDN UIEvent UIEvent 接口表示简单的用户界面事件。 UIEvent - Web API | MDN KeyboardEvent KeyboardEvent 对象描述了用户与键盘的交互。 KeyboardEvent - Web…...

Python爬虫(二):爬虫完整流程

爬虫完整流程详解&#xff08;7大核心步骤实战技巧&#xff09; 一、爬虫完整工作流程 以下是爬虫开发的完整流程&#xff0c;我将结合具体技术点和实战经验展开说明&#xff1a; 1. 目标分析与前期准备 网站技术分析&#xff1a; 使用浏览器开发者工具&#xff08;F12&…...

Nginx server_name 配置说明

Nginx 是一个高性能的反向代理和负载均衡服务器&#xff0c;其核心配置之一是 server 块中的 server_name 指令。server_name 决定了 Nginx 如何根据客户端请求的 Host 头匹配对应的虚拟主机&#xff08;Virtual Host&#xff09;。 1. 简介 Nginx 使用 server_name 指令来确定…...

数据链路层的主要功能是什么

数据链路层&#xff08;OSI模型第2层&#xff09;的核心功能是在相邻网络节点&#xff08;如交换机、主机&#xff09;间提供可靠的数据帧传输服务&#xff0c;主要职责包括&#xff1a; &#x1f511; 核心功能详解&#xff1a; 帧封装与解封装 封装&#xff1a; 将网络层下发…...

Ascend NPU上适配Step-Audio模型

1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统&#xff0c;支持多语言对话&#xff08;如 中文&#xff0c;英文&#xff0c;日语&#xff09;&#xff0c;语音情感&#xff08;如 开心&#xff0c;悲伤&#xff09;&#x…...

如何在最短时间内提升打ctf(web)的水平?

刚刚刷完2遍 bugku 的 web 题&#xff0c;前来答题。 每个人对刷题理解是不同&#xff0c;有的人是看了writeup就等于刷了&#xff0c;有的人是收藏了writeup就等于刷了&#xff0c;有的人是跟着writeup做了一遍就等于刷了&#xff0c;还有的人是独立思考做了一遍就等于刷了。…...

AI病理诊断七剑下天山,医疗未来触手可及

一、病理诊断困局&#xff1a;刀尖上的医学艺术 1.1 金标准背后的隐痛 病理诊断被誉为"诊断的诊断"&#xff0c;医生需通过显微镜观察组织切片&#xff0c;在细胞迷宫中捕捉癌变信号。某省病理质控报告显示&#xff0c;基层医院误诊率达12%-15%&#xff0c;专家会诊…...