image_picker_util.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  3. import '../resource/colors.gen.dart';
  4. import '../resource/string.gen.dart';
  5. /// 本地选择图片工具类
  6. class ImagePickerUtil {
  7. static final Color _themeColor = ColorName.colorBrand;
  8. /// 选择图片
  9. static Future<void> pickImage(
  10. BuildContext context, {
  11. required int maxAssetsCount,
  12. List<AssetEntity> selectedAssets = const [],
  13. }) async {
  14. await AssetPicker.pickAssets(
  15. context,
  16. pickerConfig: AssetPickerConfig(
  17. // 最大选择数量
  18. maxAssets: maxAssetsCount,
  19. // 已选择的图片列表
  20. selectedAssets: selectedAssets,
  21. // 自定义按钮文字
  22. textDelegate: const CustomChineseDelegate(),
  23. // 主题
  24. pickerTheme: AssetPicker.themeData(
  25. // 主题色
  26. _themeColor,
  27. // 深色默认
  28. light: false,
  29. ),
  30. // 设置为不能预览的模式
  31. specialPickerType: SpecialPickerType.noPreview,
  32. // 只能选取图片类型
  33. requestType: RequestType.image,
  34. // 关闭拽托选择
  35. dragToSelect: false,
  36. // 实现最近相册的名字显示
  37. pathNameBuilder:
  38. (AssetPathEntity path) => switch (path) {
  39. final p when p.isAll => StringName.recently,
  40. _ => path.name,
  41. },
  42. ),
  43. );
  44. }
  45. /// 跳转到图片预览
  46. static Future<List<AssetEntity>> goImagePreview(
  47. BuildContext context,
  48. List<AssetEntity> previewAssets,
  49. int index,
  50. ) async {
  51. final List<AssetEntity> result =
  52. await AssetPickerViewer.pushToViewer(
  53. context,
  54. // 当前预览的索引位置
  55. currentIndex: index,
  56. // 资源列表
  57. previewAssets: previewAssets,
  58. // 主题色
  59. themeData: AssetPicker.themeData(_themeColor),
  60. ) ??
  61. [];
  62. return result;
  63. }
  64. }
  65. /// 自定义按钮文字
  66. class CustomChineseDelegate extends AssetPickerTextDelegate {
  67. const CustomChineseDelegate();
  68. @override
  69. String get confirm => StringName.nextStep;
  70. }