aves/lib/widgets/fullscreen/uri_picture_provider.dart
Thibault Deckers 6c8441642c svg support
2020-03-23 13:00:16 +09:00

55 lines
1.7 KiB
Dart

import 'package:aves/model/image_file_service.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:pedantic/pedantic.dart';
class UriPicture extends PictureProvider<UriPicture> {
const UriPicture(this.uri, {this.colorFilter}) : assert(uri != null);
final String uri;
/// The [ColorFilter], if any, to use when drawing this picture.
final ColorFilter colorFilter;
@override
Future<UriPicture> obtainKey(PictureConfiguration configuration) {
return SynchronousFuture<UriPicture>(this);
}
@override
PictureStreamCompleter load(UriPicture key, {PictureErrorListener onError}) {
return OneFramePictureStreamCompleter(_loadAsync(key, onError: onError), informationCollector: () sync* {
yield DiagnosticsProperty<String>('Uri', uri);
});
}
Future<PictureInfo> _loadAsync(UriPicture key, {PictureErrorListener onError}) async {
assert(key == this);
final data = await ImageFileService.readAsBytes(uri);
if (data == null || data.isEmpty) {
return null;
}
final decoder = SvgPicture.svgByteDecoder;
if (onError != null) {
final future = decoder(data, colorFilter, key.toString());
unawaited(future.catchError(onError));
return future;
}
return decoder(data, colorFilter, key.toString());
}
@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) return false;
return other is UriPicture && other.uri == uri && other.colorFilter == colorFilter;
}
@override
int get hashCode => hashValues(uri, colorFilter);
@override
String toString() => '${objectRuntimeType(this, 'UriPicture')}("$uri", colorFilter: $colorFilter)';
}