photo_gallery/lib/src/image_providers/photo_provider.dart

54 lines
1.4 KiB
Dart
Raw Normal View History

2020-08-14 18:07:27 +08:00
part of photogallery;
/// Fetches the given image from the gallery.
class PhotoProvider extends ImageProvider<PhotoProvider> {
2023-07-30 00:54:28 +08:00
/// ImageProvider of photo
2020-08-14 18:07:27 +08:00
PhotoProvider({
2021-03-18 12:00:31 +00:00
required this.mediumId,
this.mimeType,
2021-03-18 12:00:31 +00:00
});
2020-08-14 18:07:27 +08:00
2023-07-30 00:54:28 +08:00
/// Medium id
2020-08-14 18:07:27 +08:00
final String mediumId;
2023-07-30 00:54:28 +08:00
/// Mime type
final String? mimeType;
2020-08-14 18:07:27 +08:00
@override
ImageStreamCompleter loadImage(key, decode) {
2020-08-14 18:07:27 +08:00
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key, decode),
scale: 1.0,
informationCollector: () sync* {
yield ErrorDescription('Id: $mediumId');
},
);
}
Future<ui.Codec> _loadAsync(PhotoProvider key, ImageDecoderCallback decode) async {
2020-08-14 18:07:27 +08:00
assert(key == this);
final file = await PhotoGallery.getFile(
mediumId: mediumId, mediumType: MediumType.image, mimeType: mimeType);
2023-07-24 00:58:15 +08:00
ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromFilePath(file.path);
return decode(buffer);
2020-08-14 18:07:27 +08:00
}
@override
Future<PhotoProvider> obtainKey(ImageConfiguration configuration) {
return SynchronousFuture<PhotoProvider>(this);
}
@override
bool operator ==(dynamic other) {
if (other.runtimeType != runtimeType) return false;
final PhotoProvider typedOther = other;
return mediumId == typedOther.mediumId;
}
@override
2021-03-18 12:00:31 +00:00
int get hashCode => mediumId.hashCode;
2020-08-14 18:07:27 +08:00
@override
String toString() => '$runtimeType("$mediumId")';
}