| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import 'package:clean/module/calendar/calendar_state.dart';
- import 'package:clean/module/calendar/preview/calendar_preview_view.dart';
- import 'package:clean/module/people_photo/photo_group.dart';
- import 'package:flutter/Material.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:intl/intl.dart';
- import 'package:wechat_assets_picker/wechat_assets_picker.dart';
- import '../../base/base_controller.dart';
- import 'package:get/get.dart';
- import '../image_picker/image_picker_util.dart';
- import 'calendar_month_view.dart';
- class CalendarController extends BaseController {
- final RxList<PhotoGroup> monthlyAlbums = CalendarState.monthlyAlbums;
- final RxList<AssetEntity> imageList = <AssetEntity>[].obs;
- final Rx<SortType> currentSortType = SortType.latest.obs; // 当前排序类型
- @override
- void onInit() {
- super.onInit();
- loadAssets();
- }
- // 加载并分组图片
- Future<void> loadAssets() async {
- final List<AssetEntity> result = await ImagePickerUtil.loadAssets();
- result.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));
- imageList.value = result;
- updateMonthlyAssets();
- }
- // 更新按月份分组的照片
- void updateMonthlyAssets() {
- Map<String, List<AssetEntity>> groupedAssets = {};
- // 按月份分组照片
- for (var asset in imageList) {
- final monthKey = DateFormat('yyyy-MM').format(asset.createDateTime);
- groupedAssets.putIfAbsent(monthKey, () => []).add(asset);
- }
- monthlyAlbums.clear();
- groupedAssets.forEach((month, assets) {
- assets
- .sort((a, b) => b.createDateTime.compareTo(a.createDateTime)); // 时间排序
- monthlyAlbums
- .add(PhotoGroup(month: month, images: assets, isSelected: false));
- });
- // 打印结果
- for (var album in monthlyAlbums) {
- print('${album.month}: ${album.images.length} 张照片');
- }
- }
- // 点击月份卡片,跳转到对应月份的照片详情页
- void clickMonthCard(PhotoGroup photoGroup) {
- print("clickMonthCard");
- CalendarMonthPage.start(photoGroup: photoGroup);
- }
- // 点击图片,查看该图片详情
- void clickImage(PhotoGroup photoGroup, int imageIndex) {
- print("CalendarController clickImage");
- CalendarPreviewPage.start(
- photoGroup: photoGroup,
- currentImageId: photoGroup.images[imageIndex].id);
- }
- // 排序照片列表
- void sortAssets(SortType sortType) {
- currentSortType.value = sortType; // 更新当前排序类型
- switch (sortType) {
- case SortType.latest:
- imageList.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));
- break;
- case SortType.oldest:
- imageList.sort((a, b) => a.createDateTime.compareTo(b.createDateTime));
- break;
- // case SortType.largeSize:
- // break;
- case SortType.mostQuantity:
- monthlyAlbums
- .sort((a, b) => b.images.length.compareTo(a.images.length));
- imageList.clear();
- for (var album in monthlyAlbums) {
- imageList.addAll(album.images); // 将每个月的照片重新按顺序添加到 imageList
- }
- break;
- }
- updateMonthlyAssets(); // 排序后更新分组
- }
- // 显示排序弹窗
- // 显示排序弹窗
- void clickSort() {
- showCupertinoModalPopup(
- context: Get.context!,
- builder: (context) {
- return Container(
- width: 360.w,
- decoration: ShapeDecoration(
- color: Color(0xFF131C27),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.only(
- topLeft: Radius.circular(44.r),
- topRight: Radius.circular(44.r),
- ),
- ),
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- SizedBox(height: 10.h),
- Container(
- width: 57.w,
- height: 4.h,
- decoration: ShapeDecoration(
- color: Color(0xFF242D38),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(2)),
- ),
- ),
- SizedBox(height: 19.h),
- Text(
- 'Sort By',
- textAlign: TextAlign.center,
- style: TextStyle(
- color: Colors.white,
- fontSize: 16.sp,
- fontWeight: FontWeight.w700,
- ),
- ),
- SizedBox(height: 14.h),
- // Latest
- sortOption(SortType.latest, 'Latest'),
- SizedBox(height: 14.h),
- // Oldest
- sortOption(SortType.oldest, 'Oldest'),
- SizedBox(height: 14.h),
- // Large Size
- // sortOption(SortType.largeSize, 'Large Size'),
- // SizedBox(height: 14.h),
- // Most Quantity
- sortOption(SortType.mostQuantity, 'Most Quantity'),
- SizedBox(height: 55.h),
- ],
- ),
- );
- },
- );
- }
- Widget sortOption(SortType type, String title) {
- bool isSelected = currentSortType.value == type; // 判断是否是选中的排序类型
- return GestureDetector(
- onTap: () {
- Navigator.pop(Get.context!);
- sortAssets(type);
- },
- child: Container(
- width: 300.w,
- height: 50.h,
- alignment: Alignment.center,
- decoration: ShapeDecoration(
- color: isSelected ? Color(0x1C0279FB) : Color(0xFF242D38),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(14.r),
- ),
- ),
- child: Text(
- title,
- style: TextStyle(
- color: isSelected ? Colors.white : Colors.white.withValues(alpha: 0.8),
- fontSize: isSelected ? 16.sp : 14.sp,
- fontWeight: isSelected ? FontWeight.w700 : FontWeight.w500,
- ),
- ),
- ),
- );
- }
- }
- // 排序类型
- enum SortType {
- latest, // 最新
- oldest, // 最旧
- // largeSize, // 按文件组大小排序
- mostQuantity // 按每个月照片数量排序
- }
|