| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import 'dart:io';
- import 'package:clean/module/people_photo/photo_group.dart';
- import 'package:get/get.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:path/path.dart' as p;
- class LocationsPhotoController extends GetxController {
- final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;
- @override
- onInit() {
- super.onInit();
- loadPhotosFromDirectory();
- }
- Future<void> loadPhotosFromDirectory() async {
- try {
- final Directory appDir = await getApplicationDocumentsDirectory();
- final String photosPath = '${appDir.path}/photos';
- final Directory photosDir = Directory(photosPath);
- if (!await photosDir.exists()) {
- return;
- }
- final List<Directory> subDirs = await photosDir
- .list()
- .where((entity) => entity is Directory)
- .map((e) => e as Directory)
- .toList();
- for (final dir in subDirs) {
- final List<FileSystemEntity> files = await dir
- .list()
- .where((entity) =>
- entity is File &&
- ['.jpg', '.jpeg', '.png']
- .contains(p.extension(entity.path).toLowerCase()))
- .toList();
- if (files.isNotEmpty) {
- photoGroups.add(PhotoGroup(
- title: 'photo: ${files.length}',
- imageCount: files.length,
- isSelected: false,
- images: files.map((file) => file.path).toList(),
- ));
- }
- }
- } catch (e) {
- print('Error loading photos: $e');
- }
- }
- }
|