image_picker_util.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:keyboard/utils/app_setting_util.dart';
  4. import 'package:keyboard/utils/toast_util.dart';
  5. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  6. import '../resource/colors.gen.dart';
  7. import '../resource/string.gen.dart';
  8. /// 本地选择图片工具类
  9. class ImagePickerUtil {
  10. static final Color _themeColor = ColorName.colorBrand;
  11. /// 选择图片
  12. static Future<List<AssetEntity>?> pickImage(
  13. BuildContext context, {
  14. required int maxAssetsCount,
  15. List<AssetEntity> selectedAssets = const [],
  16. }) async {
  17. try {
  18. return await AssetPicker.pickAssets(
  19. context,
  20. pickerConfig: AssetPickerConfig(
  21. // 最大选择数量
  22. maxAssets: maxAssetsCount,
  23. // 已选择的图片列表
  24. selectedAssets: selectedAssets,
  25. // 自定义按钮文字
  26. textDelegate: const CustomChineseDelegate(),
  27. // 主题
  28. pickerTheme: AssetPicker.themeData(
  29. // 主题色
  30. _themeColor,
  31. // 深色默认
  32. light: false,
  33. ),
  34. // 设置为不能预览的模式
  35. specialPickerType: SpecialPickerType.noPreview,
  36. // 只能选取图片类型
  37. requestType: RequestType.image,
  38. // 关闭拽托选择
  39. dragToSelect: false,
  40. // 实现最近相册的名字显示
  41. pathNameBuilder:
  42. (AssetPathEntity path) => switch (path) {
  43. final p when p.isAll => StringName.recently,
  44. _ => path.name,
  45. },
  46. ),
  47. ) ??
  48. [];
  49. } catch (e) {
  50. if (e is StateError) {
  51. ToastUtil.show(StringName.pickerImagePermissionDeniedTip);
  52. Future.delayed(Duration(milliseconds: 400), () {
  53. AppSettingUtil.jumpSystemAppSetting();
  54. });
  55. } else {
  56. rethrow;
  57. }
  58. }
  59. return null;
  60. }
  61. /// 跳转到图片预览
  62. static Future<List<AssetEntity>> goImagePreview(
  63. BuildContext context,
  64. List<AssetEntity> previewAssets,
  65. int index,
  66. ) async {
  67. final List<AssetEntity> result =
  68. await AssetPickerViewer.pushToViewer(
  69. context,
  70. // 当前预览的索引位置
  71. currentIndex: index,
  72. // 资源列表
  73. previewAssets: previewAssets,
  74. // 主题色
  75. themeData: AssetPicker.themeData(_themeColor),
  76. ) ??
  77. [];
  78. return result;
  79. }
  80. /// AssetEntity实体,转换为File列表
  81. /// [assetsList] 图片、视频选择后的资源列表
  82. static Future<List<File>> convertAssetToFile(
  83. List<AssetEntity> assetsList,
  84. ) async {
  85. List<File> files = [];
  86. for (var asset in assetsList) {
  87. final file = await asset.file;
  88. if (file != null) {
  89. files.add(file);
  90. }
  91. }
  92. return files;
  93. }
  94. }
  95. /// 自定义按钮文字
  96. class CustomChineseDelegate extends AssetPickerTextDelegate {
  97. const CustomChineseDelegate();
  98. @override
  99. String get confirm => StringName.nextStep;
  100. }