Browse Source

feat(neon): Add error builder for cached images

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/399/head
jld3103 1 year ago
parent
commit
d3332a7c0e
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 34
      packages/neon/neon/lib/src/widgets/cached_image.dart

34
packages/neon/neon/lib/src/widgets/cached_image.dart

@ -11,6 +11,7 @@ import 'package:neon/src/widgets/linear_progress_indicator.dart';
typedef CacheReviver = FutureOr<Uint8List?> Function(CacheManager cacheManager);
typedef ImageDownloader = FutureOr<Uint8List> Function();
typedef CacheWriter = Future<void> Function(CacheManager cacheManager, Uint8List image);
typedef ErrorWidgetBuilder = Widget? Function(BuildContext, dynamic);
class NeonCachedImage extends StatefulWidget {
const NeonCachedImage({
@ -21,6 +22,7 @@ class NeonCachedImage extends StatefulWidget {
this.fit,
this.svgColor,
this.iconColor,
this.errorBuilder,
});
NeonCachedImage.url({
@ -31,6 +33,7 @@ class NeonCachedImage extends StatefulWidget {
this.fit,
this.svgColor,
this.iconColor,
this.errorBuilder,
}) : image = _getImageFromUrl(url),
super(key: key ?? Key(url));
@ -44,6 +47,7 @@ class NeonCachedImage extends StatefulWidget {
this.fit,
this.svgColor,
this.iconColor,
this.errorBuilder,
}) : image = _customImageGetter(
reviver,
getImage,
@ -61,6 +65,8 @@ class NeonCachedImage extends StatefulWidget {
final Color? svgColor;
final Color? iconColor;
final ErrorWidgetBuilder? errorBuilder;
static Future<Uint8List> _getImageFromUrl(final String url) async {
final file = await _cacheManager.getSingleFile(url);
return file.readAsBytes();
@ -116,6 +122,10 @@ class _NeonCachedImageState extends State<NeonCachedImage> {
child: FutureBuilder<Uint8List>(
future: widget.image,
builder: (final context, final fileSnapshot) {
if (fileSnapshot.hasError) {
return _buildError(fileSnapshot.error);
}
if (!fileSnapshot.hasData) {
return SizedBox(
width: widget.size?.width,
@ -125,18 +135,6 @@ class _NeonCachedImageState extends State<NeonCachedImage> {
);
}
if (fileSnapshot.hasError) {
return NeonException(
fileSnapshot.error,
onRetry: () {
setState(() {});
},
onlyIcon: true,
iconSize: widget.size?.shortestSide,
color: widget.iconColor ?? Theme.of(context).colorScheme.error,
);
}
final content = fileSnapshot.requireData;
try {
@ -160,8 +158,20 @@ class _NeonCachedImageState extends State<NeonCachedImage> {
width: widget.size?.width,
fit: widget.fit,
gaplessPlayback: true,
errorBuilder: (final context, final error, final stacktrace) => _buildError(error),
);
},
),
);
Widget _buildError(final dynamic error) =>
widget.errorBuilder?.call(context, error) ??
NeonException(
error,
onRetry: () {
setState(() {});
},
onlyIcon: true,
iconSize: widget.size?.shortestSide,
);
}

Loading…
Cancel
Save