flutter项目中 advance_image_picker 组件使用

2023-10-08 20:30

本文主要是介绍flutter项目中 advance_image_picker 组件使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、说明

Advance_image_picker 是flutter 插件,用于从Android 和 ios 图像库中选择多张图片,使用相机拍摄新照片,并对其进行编辑。

下图为最终效果:
在这里插入图片描述
这里有个bug ,在连续多测按 旋转摄像头时,程序会出现卡顿的情况。

二、使用方式

IOS :
将这些设置添加到 ios/Runner/Info.plist

<key>NSCameraUsageDescription</key>
<string>Can I use the camera please?</string>
<key>NSMicrophoneUsageDescription</key>
<string>Can I use the mic please?</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>App need your agree, can visit your album</string>

Android :
在您的android/app/build.gradle文件中将最低 Android sdk 版本更改为 21(或更高)。
将活动和使用权限添加到您的AndroidManifest.xml,就像下一步一样。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="vn.weta.freemarimagepickerexample"><applicationandroid:name="io.flutter.app.FlutterApplication"android:label="freemarimagepicker_example"android:requestLegacyExternalStorage="true"android:icon="@mipmap/ic_launcher"><activityandroid:name="com.yalantis.ucrop.UCropActivity"android:screenOrientation="portrait"android:theme="@style/Theme.AppCompat.Light.NoActionBar"/></application><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.CAMERA" />
</manifest>

添加到 pubspec
flutter项目的 pubspec.yaml 中添加依赖:

dependencies:advance_image_picker: $latest_version

三、项目代码

最近自己动手做的项目是通过Getx 开发。插件测试代码分为 view 端代码和 controller 端代码,这里一并贴出

view 端代码:


class ImagePickerScreen extends GetView<ImagePickerController> {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Obx(() => Text(controller.pageName.value,style: TextStyle(color: Theme.of(context).appBarTheme.titleTextStyle!.color,),),),centerTitle: true,),body: _buildBody(context),);}Widget _buildBody(BuildContext context) {return SingleChildScrollView(child: SafeArea(child: Column(mainAxisAlignment: MainAxisAlignment.start,children: [Container(child: ElevatedButton(onPressed: () {controller.getImages();},child: Text('上传图标'),),),_buildImages(context),],),),);}Widget _buildImages(BuildContext context) {return Obx(() => GridView.builder(itemCount: controller.imgObjs.length,shrinkWrap: true,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4, mainAxisSpacing: 2),itemBuilder: (BuildContext context, int index) {return Padding(padding: EdgeInsets.all(2),child: Image.file(File(controller.imgObjs[index]),height: 80,fit: BoxFit.cover,),);},),);}
}

contoller 部分代码:
ApiRepository 用于网络请求 ,这里通过构造器依赖注入。由于测试过程中,没有用到网络请求,这部分省略。

class ImagePickerController extends GetxControllerwith GetSingleTickerProviderStateMixin {final pageName = "imagePicker".obs;final ApiRepository apiRepository;late ImagePickerConfigs imageConfig;RxList<String> imgObjs = <String>[].obs;CommodityAddController({required this.apiRepository});@overridevoid onInit() {imageConfig = ImagePickerConfigs();imageConfig.appBarTextColor = Colors.white;imageConfig.appBarBackgroundColor = Colors.black54;imageConfig.stickerFeatureEnabled = false; // ON/OFF featuresimageConfig.translateFunc = (name, value) => name.tr;imageConfig.iconCamera = FontAwesomeIcons.camera;imageConfig.doneButtonStyle = DoneButtonStyle.outlinedButton;imageConfig.adjustFeatureEnabled = false;imageConfig.externalImageEditors['external_image_editor_1'] = EditorParams(title: 'external_image_editor_1',icon: Icons.edit_rounded,onEditorEvent: ({required BuildContext context,required File file,required String title,int maxWidth = 1080,int maxHeight = 1920,int compressQuality = 90,ImagePickerConfigs? configs}) async =>navigator!.push(MaterialPageRoute(fullscreenDialog: true,builder: (context) => ImageEdit(file: file,title: title,maxWidth: maxWidth,maxHeight: maxHeight,configs: configs),),),);imageConfig.imagePreProcessingBeforeEditingEnabled = true;imageConfig.filterFeatureEnabled = false;imageConfig.imagePreProcessingEnabled = true;imageConfig.cropFeatureEnabled = false;super.onInit();}void getImages() async {List<ImageObject> objects = await navigator!.push(MaterialPageRoute(builder: (_) {return ImagePicker(maxCount: 5);},),);if (objects.length > 0) {imgObjs.addAll(objects.map((e) => e.modifiedPath).toList());}}
}

四、最后注意事项

ImagePickerConfigs 为imagePicker 配置信息。该插件在弹出图片选择界面后,显示语言默认 英文显示。

imageConfig.translateFunc = (name, value) => name.tr; 用于配置 imagePicker语言国际化。

getx 多语言配置:

  1. 文件结构:
    在这里插入图片描述
  2. 文件内容:

translation_service.dart:

import 'dart:ui';import 'package:get/get.dart';import 'en_US.dart';
import 'zh_CN.dart';class TranslationService extends Translations {static Locale? get locale => Get.deviceLocale;static final fallbackLocale = Locale('zh', 'CN');@overrideMap<String, Map<String, String>> get keys => {'zh_CN': zh_CN,'en_US': en_US,};
}

en_US.dart

const Map<String, String> en_US = {'name': 'name','image_picker_camera_title': 'camera','image_picker_album_title': 'album','image_picker_select_button_title': 'select','image_picker_select_images_title': 'selected images count:','image_picker_select_images_guide': 'drag images for sorting list...:','image_picker_exposure_title':'exposure','image_picker_exposure_locked_title':'locked','image_picker_exposure_auto_title':'auto',
};

zh_CN.dart

const Map<String, String> zh_CN = {'name': '用户名','image_picker_camera_title': '相机','image_picker_album_title': '相册','image_picker_select_button_title': '确定','image_picker_select_images_title': '已选择:','image_picker_select_images_guide': '拖动图片重新排序:','image_picker_exposure_title': '滤镜','image_picker_exposure_locked_title': '锁定','image_picker_exposure_auto_title': '自动','image_picker_confirm': '确定','image_picker_confirm_delete': '是否要删除这张照片','no': '否','yes': '是','image_picker_exit_without_selecting': '是否要退出而不选择图像?','image_picker_preview_title': '预览','image_picker_image_edit_title': '编辑','image_picker_image_edit_contrast': '对比度','image_picker_image_edit_brightness': '亮度','image_picker_image_edit_saturation': '饱和度','image_picker_image_filter_title': '滤镜','image_picker_confirm_reset_changes': '是否重置更改?',
};

main.dart 中 需要 传递参数给GetMaterialApp来定义语言和翻译。

return GetMaterialApp(translations: Messages(), // 你的翻译locale: Locale('zh', 'CN'), // 将会按照此处指定的语言翻译fallbackLocale: Locale('en', 'US'), // 添加一个回调语言选项,以备上面指定的语言翻译不存在
);

项目中的配置:
在这里插入图片描述

这篇关于flutter项目中 advance_image_picker 组件使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/167806

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、