photo_group.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import 'package:clean/utils/file_size_calculator_util.dart';
  2. import 'package:get/get.dart';
  3. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  4. class PhotoGroup {
  5. // 照片组的选择状态
  6. final RxBool isSelected;
  7. // 照片组中的图片列表
  8. final RxList<AssetEntity> images;
  9. // 已选中图片的唯一标识集合
  10. final RxSet<String> selectedPhotosIds = <String>{}.obs;
  11. // 照片组的位置
  12. final String? location;
  13. // 选中文件的总大小
  14. RxDouble selectedTotalSize = 0.0.obs;
  15. // 照片组的月份
  16. final String? month;
  17. // 整个 images 列表的总大小
  18. RxDouble totalSize = 0.0.obs;
  19. // 获取已选中的图片数量
  20. int get selectedCount => selectedPhotosIds.length;
  21. // 构造函数
  22. PhotoGroup({
  23. required bool isSelected,
  24. required List<AssetEntity> images,
  25. this.location,
  26. this.month,
  27. }) : isSelected = isSelected.obs,
  28. images = images.obs
  29. {
  30. // 初始化已选中图片的唯一标识集合
  31. if (isSelected) {
  32. selectedPhotosIds.addAll(images.map((e) => e.id));
  33. }
  34. }
  35. Future<void> initTotalSize() async {
  36. await FileSizeCalculatorUtil.calculateTotalSize(
  37. assetIds: images.map((e) => e.id).toSet(), updateValue: (double totalSize) {
  38. if (this.totalSize.value != totalSize) {
  39. this.totalSize.value = totalSize; // 监听并更新 UI
  40. }
  41. });
  42. }
  43. // 切换选择所有图片的状态
  44. void toggleSelectAll(bool value) {
  45. isSelected.value = value;
  46. if (value) {
  47. selectedPhotosIds.addAll(images.map((e) => e.id).toList());
  48. } else {
  49. selectedPhotosIds.clear();
  50. }
  51. }
  52. // 切换某张图片的选择状态
  53. void toggleSelectImage(String id) {
  54. if (selectedPhotosIds.contains(id)) {
  55. selectedPhotosIds.remove(id);
  56. } else {
  57. selectedPhotosIds.add(id);
  58. }
  59. }
  60. /// 判断某张图片是否被选中
  61. bool isImageSelected(AssetEntity image) {
  62. return selectedPhotosIds.contains(image.id);
  63. }
  64. }