Gallery/lib/data_provider/local_data_provider.dart
2022-09-26 00:12:57 +02:00

42 lines
1.0 KiB
Dart

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:gallery/data_provider/data_provider.dart';
class LocalDataProvider extends DataProvider {
final String initialPath;
LocalDataProvider(this.initialPath);
@override
void connect() {}
@override
Future<Folder> listOfFiles({Uri? uri}) async {
final dir = uri != null ? Directory.fromUri(uri) : Directory(initialPath);
final list = dir.listSync();
final List<Item> res = [];
for (var val in list) {
if (validSuffix.any((suff) => val.path.endsWith(suff))) {
res.add(Item(false, val.uri, val.uri.pathSegments.last));
} else if (await FileSystemEntity.isDirectory(val.path)) {
res.add(
Item(true, val.uri, val.uri.pathSegments.reversed.skip(1).first));
}
}
res.sort(
(a, b) => b.isFolder ? 1 : -1,
);
return Folder(res, dir.uri, dir.parent.uri);
}
@override
ImageProvider getImageProvider(Uri uri) {
return FileImage(File.fromUri(uri));
}
}