photo_info_controller.dart 3.8 KB

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