calendar_controller.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import 'package:clean/module/calendar/calendar_state.dart';
  2. import 'package:clean/module/calendar/preview/calendar_preview_view.dart';
  3. import 'package:clean/module/people_photo/photo_group.dart';
  4. import 'package:flutter/Material.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter_screenutil/flutter_screenutil.dart';
  7. import 'package:intl/intl.dart';
  8. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  9. import '../../base/base_controller.dart';
  10. import 'package:get/get.dart';
  11. import '../image_picker/image_picker_util.dart';
  12. import 'calendar_month_view.dart';
  13. class CalendarController extends BaseController {
  14. final RxList<PhotoGroup> monthlyAlbums = CalendarState.monthlyAlbums;
  15. final RxList<AssetEntity> imageList = <AssetEntity>[].obs;
  16. final Rx<SortType> currentSortType = SortType.latest.obs; // 当前排序类型
  17. @override
  18. void onInit() {
  19. super.onInit();
  20. loadAssets();
  21. }
  22. // 加载并分组图片
  23. Future<void> loadAssets() async {
  24. final List<AssetEntity> result = await ImagePickerUtil.loadAssets();
  25. result.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));
  26. imageList.value = result;
  27. updateMonthlyAssets();
  28. }
  29. // 更新按月份分组的照片
  30. void updateMonthlyAssets() {
  31. Map<String, List<AssetEntity>> groupedAssets = {};
  32. // 按月份分组照片
  33. for (var asset in imageList) {
  34. final monthKey = DateFormat('yyyy-MM').format(asset.createDateTime);
  35. groupedAssets.putIfAbsent(monthKey, () => []).add(asset);
  36. }
  37. monthlyAlbums.clear();
  38. groupedAssets.forEach((month, assets) {
  39. assets
  40. .sort((a, b) => b.createDateTime.compareTo(a.createDateTime)); // 时间排序
  41. monthlyAlbums
  42. .add(PhotoGroup(month: month, images: assets, isSelected: false));
  43. });
  44. // 打印结果
  45. for (var album in monthlyAlbums) {
  46. print('${album.month}: ${album.images.length} 张照片');
  47. }
  48. }
  49. // 点击月份卡片,跳转到对应月份的照片详情页
  50. void clickMonthCard(PhotoGroup photoGroup) {
  51. print("clickMonthCard");
  52. CalendarMonthPage.start(photoGroup: photoGroup);
  53. }
  54. // 点击图片,查看该图片详情
  55. void clickImage(PhotoGroup photoGroup, int imageIndex) {
  56. print("CalendarController clickImage");
  57. CalendarPreviewPage.start(
  58. photoGroup: photoGroup,
  59. currentImageId: photoGroup.images[imageIndex].id);
  60. }
  61. // 排序照片列表
  62. void sortAssets(SortType sortType) {
  63. currentSortType.value = sortType; // 更新当前排序类型
  64. switch (sortType) {
  65. case SortType.latest:
  66. imageList.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));
  67. break;
  68. case SortType.oldest:
  69. imageList.sort((a, b) => a.createDateTime.compareTo(b.createDateTime));
  70. break;
  71. // case SortType.largeSize:
  72. // break;
  73. case SortType.mostQuantity:
  74. monthlyAlbums
  75. .sort((a, b) => b.images.length.compareTo(a.images.length));
  76. imageList.clear();
  77. for (var album in monthlyAlbums) {
  78. imageList.addAll(album.images); // 将每个月的照片重新按顺序添加到 imageList
  79. }
  80. break;
  81. }
  82. updateMonthlyAssets(); // 排序后更新分组
  83. }
  84. // 显示排序弹窗
  85. // 显示排序弹窗
  86. void clickSort() {
  87. showCupertinoModalPopup(
  88. context: Get.context!,
  89. builder: (context) {
  90. return Container(
  91. width: 360.w,
  92. decoration: ShapeDecoration(
  93. color: Color(0xFF131C27),
  94. shape: RoundedRectangleBorder(
  95. borderRadius: BorderRadius.only(
  96. topLeft: Radius.circular(44.r),
  97. topRight: Radius.circular(44.r),
  98. ),
  99. ),
  100. ),
  101. child: Column(
  102. mainAxisSize: MainAxisSize.min,
  103. children: [
  104. SizedBox(height: 10.h),
  105. Container(
  106. width: 57.w,
  107. height: 4.h,
  108. decoration: ShapeDecoration(
  109. color: Color(0xFF242D38),
  110. shape: RoundedRectangleBorder(
  111. borderRadius: BorderRadius.circular(2)),
  112. ),
  113. ),
  114. SizedBox(height: 19.h),
  115. Text(
  116. 'Sort By',
  117. textAlign: TextAlign.center,
  118. style: TextStyle(
  119. color: Colors.white,
  120. fontSize: 16.sp,
  121. fontWeight: FontWeight.w700,
  122. ),
  123. ),
  124. SizedBox(height: 14.h),
  125. // Latest
  126. sortOption(SortType.latest, 'Latest'),
  127. SizedBox(height: 14.h),
  128. // Oldest
  129. sortOption(SortType.oldest, 'Oldest'),
  130. SizedBox(height: 14.h),
  131. // Large Size
  132. // sortOption(SortType.largeSize, 'Large Size'),
  133. // SizedBox(height: 14.h),
  134. // Most Quantity
  135. sortOption(SortType.mostQuantity, 'Most Quantity'),
  136. SizedBox(height: 55.h),
  137. ],
  138. ),
  139. );
  140. },
  141. );
  142. }
  143. Widget sortOption(SortType type, String title) {
  144. bool isSelected = currentSortType.value == type; // 判断是否是选中的排序类型
  145. return GestureDetector(
  146. onTap: () {
  147. Navigator.pop(Get.context!);
  148. sortAssets(type);
  149. },
  150. child: Container(
  151. width: 300.w,
  152. height: 50.h,
  153. alignment: Alignment.center,
  154. decoration: ShapeDecoration(
  155. color: isSelected ? Color(0x1C0279FB) : Color(0xFF242D38),
  156. shape: RoundedRectangleBorder(
  157. borderRadius: BorderRadius.circular(14.r),
  158. ),
  159. ),
  160. child: Text(
  161. title,
  162. style: TextStyle(
  163. color: isSelected ? Colors.white : Colors.white.withValues(alpha: 0.8),
  164. fontSize: isSelected ? 16.sp : 14.sp,
  165. fontWeight: isSelected ? FontWeight.w700 : FontWeight.w500,
  166. ),
  167. ),
  168. ),
  169. );
  170. }
  171. }
  172. // 排序类型
  173. enum SortType {
  174. latest, // 最新
  175. oldest, // 最旧
  176. // largeSize, // 按文件组大小排序
  177. mostQuantity // 按每个月照片数量排序
  178. }