|
|
@@ -1,6 +1,8 @@
|
|
|
import 'dart:io';
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
+import 'package:keyboard/utils/app_setting_util.dart';
|
|
|
+import 'package:keyboard/utils/toast_util.dart';
|
|
|
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
|
|
|
|
|
import '../resource/colors.gen.dart';
|
|
|
@@ -11,42 +13,54 @@ class ImagePickerUtil {
|
|
|
static final Color _themeColor = ColorName.colorBrand;
|
|
|
|
|
|
/// 选择图片
|
|
|
- static Future<List<AssetEntity>> pickImage(
|
|
|
+ static Future<List<AssetEntity>?> pickImage(
|
|
|
BuildContext context, {
|
|
|
required int maxAssetsCount,
|
|
|
List<AssetEntity> selectedAssets = const [],
|
|
|
}) async {
|
|
|
- return await AssetPicker.pickAssets(
|
|
|
- context,
|
|
|
- pickerConfig: AssetPickerConfig(
|
|
|
- // 最大选择数量
|
|
|
- maxAssets: maxAssetsCount,
|
|
|
- // 已选择的图片列表
|
|
|
- selectedAssets: selectedAssets,
|
|
|
- // 自定义按钮文字
|
|
|
- textDelegate: const CustomChineseDelegate(),
|
|
|
- // 主题
|
|
|
- pickerTheme: AssetPicker.themeData(
|
|
|
- // 主题色
|
|
|
- _themeColor,
|
|
|
- // 深色默认
|
|
|
- light: false,
|
|
|
+ try {
|
|
|
+ return await AssetPicker.pickAssets(
|
|
|
+ context,
|
|
|
+ pickerConfig: AssetPickerConfig(
|
|
|
+ // 最大选择数量
|
|
|
+ maxAssets: maxAssetsCount,
|
|
|
+ // 已选择的图片列表
|
|
|
+ selectedAssets: selectedAssets,
|
|
|
+ // 自定义按钮文字
|
|
|
+ textDelegate: const CustomChineseDelegate(),
|
|
|
+ // 主题
|
|
|
+ pickerTheme: AssetPicker.themeData(
|
|
|
+ // 主题色
|
|
|
+ _themeColor,
|
|
|
+ // 深色默认
|
|
|
+ light: false,
|
|
|
+ ),
|
|
|
+ // 设置为不能预览的模式
|
|
|
+ specialPickerType: SpecialPickerType.noPreview,
|
|
|
+ // 只能选取图片类型
|
|
|
+ requestType: RequestType.image,
|
|
|
+ // 关闭拽托选择
|
|
|
+ dragToSelect: false,
|
|
|
+ // 实现最近相册的名字显示
|
|
|
+ pathNameBuilder:
|
|
|
+ (AssetPathEntity path) => switch (path) {
|
|
|
+ final p when p.isAll => StringName.recently,
|
|
|
+ _ => path.name,
|
|
|
+ },
|
|
|
),
|
|
|
- // 设置为不能预览的模式
|
|
|
- specialPickerType: SpecialPickerType.noPreview,
|
|
|
- // 只能选取图片类型
|
|
|
- requestType: RequestType.image,
|
|
|
- // 关闭拽托选择
|
|
|
- dragToSelect: false,
|
|
|
- // 实现最近相册的名字显示
|
|
|
- pathNameBuilder:
|
|
|
- (AssetPathEntity path) => switch (path) {
|
|
|
- final p when p.isAll => StringName.recently,
|
|
|
- _ => path.name,
|
|
|
- },
|
|
|
- ),
|
|
|
- ) ??
|
|
|
- [];
|
|
|
+ ) ??
|
|
|
+ [];
|
|
|
+ } catch (e) {
|
|
|
+ if (e is StateError) {
|
|
|
+ ToastUtil.show(StringName.pickerImagePermissionDeniedTip);
|
|
|
+ Future.delayed(Duration(milliseconds: 400), () {
|
|
|
+ AppSettingUtil.jumpSystemAppSetting();
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ rethrow;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/// 跳转到图片预览
|