photo_info_controller.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import 'dart:math';
  2. import 'package:classify_photo/classify_photo.dart';
  3. import 'package:clean/module/analysis/analysis_state.dart';
  4. import 'package:clean/module/image_picker/image_picker_util.dart';
  5. import 'package:clean/module/privacy/privacy_state.dart';
  6. import 'package:clean/utils/expand.dart';
  7. import 'package:clean/utils/file_utils.dart';
  8. import 'package:clean/utils/image_util.dart';
  9. import 'package:flutter/cupertino.dart';
  10. import 'package:flutter_screenutil/flutter_screenutil.dart';
  11. import 'package:get/get.dart';
  12. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  13. import '../../base/base_controller.dart';
  14. import '../../model/asset_info.dart';
  15. class PhotoInfoController extends BaseController {
  16. Rx<FileType> type = FileType.analysis.obs;
  17. // 存储所有图片,按月份分组
  18. RxList<AssetInfo> imageList = <AssetInfo>[].obs;
  19. // 当前显示的图片索引
  20. final RxInt currentImageIndex = 0.obs;
  21. final RxMap<String, dynamic> photoDetails = <String, dynamic>{}.obs;
  22. RxString createTime = "".obs;
  23. RxString fileName = "".obs;
  24. RxString model = "".obs;
  25. RxString focalLength = "".obs;
  26. RxString size = "".obs;
  27. RxString iso = "".obs;
  28. RxString address = "".obs;
  29. @override
  30. void onInit() {
  31. // TODO: implement onInit
  32. super.onInit();
  33. if (Get.arguments != null) {
  34. final args = Get.arguments as Map<String, dynamic>;
  35. imageList.value = Get.arguments["list"] as List<AssetInfo>;
  36. var argType = Get.arguments["type"] as String;
  37. if (argType == "privacy") {
  38. type.value = FileType.privacy;
  39. } else {
  40. type.value = FileType.analysis;
  41. }
  42. // 设置初始索引
  43. if (args.containsKey('index')) {
  44. currentImageIndex.value = args['index'] as int;
  45. }
  46. loadPhotoInfo();
  47. }
  48. }
  49. Future<void> loadPhotoInfo() async {
  50. createTime.value = "";
  51. fileName.value = "";
  52. model.value = "";
  53. focalLength.value = "";
  54. size.value = "";
  55. iso.value = "";
  56. var assetInfo = imageList[currentImageIndex.value];
  57. photoDetails.value = await ImageUtil.getPhotoDetails(assetInfo);
  58. if (photoDetails.isNotEmpty) {
  59. createTime.value = photoDetails["createDate"] as String;
  60. fileName.value = photoDetails["fileName"] as String;
  61. model.value = photoDetails["model"] as String;
  62. focalLength.value =
  63. "${photoDetails["focalLength"] as String}mm f/${photoDetails["aperture"] as String}";
  64. var imageSize = ImagePickerUtil.formatFileSize(photoDetails["size"], decimals: 1);
  65. size.value = "${photoDetails["width"] as int} X ${photoDetails["height"] as int} $imageSize";
  66. iso.value = "ISO${photoDetails["iso"]} ${photoDetails["exposureTime"]}s";
  67. }
  68. }
  69. // 切换到下一张图片
  70. void nextImage() {
  71. if (currentImageIndex.value < imageList.length - 1) {
  72. currentImageIndex.value++;
  73. }
  74. loadPhotoInfo();
  75. }
  76. // 切换到上一张图片
  77. void previousImage() {
  78. if (currentImageIndex.value > 0) {
  79. currentImageIndex.value--;
  80. }
  81. loadPhotoInfo();
  82. }
  83. void deleteBtnClick(AssetInfo asset, int index) {
  84. showCupertinoModalPopup(
  85. context: Get.context!,
  86. builder: (context) {
  87. return CupertinoActionSheet(
  88. title: Text(
  89. "The file will be deleted from the privacy space",
  90. style: TextStyle(
  91. color: "#6E6E6E".color,
  92. fontSize: 12.sp,
  93. fontWeight: FontWeight.w300,
  94. ),
  95. ),
  96. actions: <Widget>[
  97. //操作按钮集合
  98. CupertinoActionSheetAction(
  99. onPressed: () {
  100. Navigator.pop(context);
  101. FileUtils.deleteAsset(type.value, asset.id.substring(0, 36));
  102. if (type.value == FileType.privacy) {
  103. PrivacyState.removeAsset(asset.id);
  104. // 从列表中移除
  105. imageList = PrivacyState.imageList;
  106. }
  107. if (type.value == FileType.analysis) {
  108. AnalysisState.removeAsset(asset.id);
  109. // 从列表中移除
  110. imageList = AnalysisState.imageList;
  111. }
  112. // 如果没有图片了,返回上一页
  113. if (imageList.isEmpty) {
  114. Get.back();
  115. } else {
  116. if (currentImageIndex.value != 0) {
  117. currentImageIndex.value = imageList.length - 1;
  118. }
  119. }
  120. },
  121. child: Text(
  122. 'Delete',
  123. style: TextStyle(
  124. color: "#FC4C4F".color,
  125. fontWeight: FontWeight.w500,
  126. fontSize: 16.sp,
  127. ),
  128. ),
  129. ),
  130. ],
  131. cancelButton: CupertinoActionSheetAction(
  132. //取消按钮
  133. onPressed: () {
  134. Navigator.pop(context);
  135. },
  136. child: Text(
  137. 'Cancel',
  138. style: TextStyle(
  139. color: "#007AFF".color,
  140. fontWeight: FontWeight.w500,
  141. fontSize: 16.sp,
  142. ),
  143. ),
  144. ),
  145. );
  146. },
  147. );
  148. }
  149. // 格式化文件大小显示
  150. String formatFileSize(int bytes) {
  151. if (bytes <= 0) return "Delete";
  152. final units = ['B', 'KB', 'MB', 'GB'];
  153. int digitGroups = (log(bytes) / log(1024)).floor();
  154. if (bytes == 0) {
  155. return "Delete";
  156. } else {
  157. return "Delete(${(bytes / pow(1024, digitGroups)).toStringAsFixed(1)} ${units[digitGroups]})";
  158. }
  159. }
  160. }