locations_photo_controller.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import 'dart:io';
  2. import 'package:clean/module/people_photo/photo_group.dart';
  3. import 'package:get/get.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:path/path.dart' as p;
  6. class LocationsPhotoController extends GetxController {
  7. final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;
  8. @override
  9. onInit() {
  10. super.onInit();
  11. loadPhotosFromDirectory();
  12. }
  13. Future<void> loadPhotosFromDirectory() async {
  14. try {
  15. final Directory appDir = await getApplicationDocumentsDirectory();
  16. final String photosPath = '${appDir.path}/photos';
  17. final Directory photosDir = Directory(photosPath);
  18. if (!await photosDir.exists()) {
  19. return;
  20. }
  21. final List<Directory> subDirs = await photosDir
  22. .list()
  23. .where((entity) => entity is Directory)
  24. .map((e) => e as Directory)
  25. .toList();
  26. for (final dir in subDirs) {
  27. final List<FileSystemEntity> files = await dir
  28. .list()
  29. .where((entity) =>
  30. entity is File &&
  31. ['.jpg', '.jpeg', '.png']
  32. .contains(p.extension(entity.path).toLowerCase()))
  33. .toList();
  34. if (files.isNotEmpty) {
  35. photoGroups.add(PhotoGroup(
  36. title: 'photo: ${files.length}',
  37. imageCount: files.length,
  38. isSelected: false,
  39. images: files.map((file) => file.path).toList(),
  40. ));
  41. }
  42. }
  43. } catch (e) {
  44. print('Error loading photos: $e');
  45. }
  46. }
  47. }