import 'dart:io'; import 'package:get/get.dart'; import 'package:photo_manager/photo_manager.dart'; class ImagePickerUtil { ImagePickerUtil._(); static const RequestType permissionType = RequestType.image; // 全局存储不同类型的照片 // 截图图片 static final RxList screenshotPhotos = [].obs; // 相似图片 static final RxList> similarPhotos = >[].obs; // 地点图片 static final RxMap> locationPhotos = >{}.obs; // 人物图片 static final RxList peoplePhotos = [].obs; // 清除所有照片数据 static void clearAllPhotos() { screenshotPhotos.clear(); similarPhotos.clear(); locationPhotos.clear(); peoplePhotos.clear(); } // 更新照片数据 static Future updatePhotos(List> photoGroups) async { clearAllPhotos(); for (var group in photoGroups) { String type = group['type'] as String; List photos = group['group'] as List; switch (type) { case 'screenshots': screenshotPhotos.value = await _convertToAssetEntities(photos); break; case 'similar': similarPhotos.add(await _convertToAssetEntities(photos)); break; case 'location': String location = group['name'] as String; locationPhotos[location] = await _convertToAssetEntities(photos); break; case 'people': peoplePhotos.value = await _convertToAssetEntities(photos); break; } } } // 将原始照片数据转换为 AssetEntity 列表 static Future> _convertToAssetEntities(List photos) async { List entities = []; for (var photo in photos) { final entity = await AssetEntity.fromId(photo['id'] as String); if (entity != null) { entities.add(entity); } } return entities; } //申请权限 static Future requestPermissionExtend() async { final PermissionState ps = await PhotoManager.requestPermissionExtend( requestOption: const PermissionRequestOption( androidPermission: AndroidPermission( type: permissionType, mediaLocation: false, ))); return ps.hasAccess; } //判断是否有权限 static Future hasPermission() async { final PermissionState ps = await PhotoManager.getPermissionState( requestOption: const PermissionRequestOption( androidPermission: AndroidPermission( type: permissionType, mediaLocation: false, ))); return ps.hasAccess; } }