image_picker_util.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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<List<AssetEntity>> pickImage(
  10. BuildContext context, {
  11. required int maxAssetsCount,
  12. List<AssetEntity> selectedAssets = const [],
  13. }) async {
  14. return 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. /// 跳转到图片预览
  47. static Future<List<AssetEntity>> goImagePreview(
  48. BuildContext context,
  49. List<AssetEntity> previewAssets,
  50. int index,
  51. ) async {
  52. final List<AssetEntity> result =
  53. await AssetPickerViewer.pushToViewer(
  54. context,
  55. // 当前预览的索引位置
  56. currentIndex: index,
  57. // 资源列表
  58. previewAssets: previewAssets,
  59. // 主题色
  60. themeData: AssetPicker.themeData(_themeColor),
  61. ) ??
  62. [];
  63. return result;
  64. }
  65. }
  66. /// 自定义按钮文字
  67. class CustomChineseDelegate extends AssetPickerTextDelegate {
  68. const CustomChineseDelegate();
  69. @override
  70. String get confirm => StringName.nextStep;
  71. }