photo_info_controller.dart 4.4 KB

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