Browse Source

neon: improve loading animation of NeonCachedImage

pull/432/head
Nikolas Rimikis 1 year ago
parent
commit
de4f2397e1
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 69
      packages/neon/neon/lib/src/widgets/cached_image.dart

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

@ -27,46 +27,34 @@ abstract class NeonCachedImage extends StatefulWidget {
} }
class _NeonCachedImageState extends State<NeonCachedImage> { class _NeonCachedImageState extends State<NeonCachedImage> {
late Future<File> future = widget.getImageFile(); Future<Uint8List> _readImage() async {
final file = await widget.getImageFile();
return file.readAsBytes();
}
late Future<Uint8List> _future = _readImage();
@override @override
Widget build(final BuildContext context) => Center( Widget build(final BuildContext context) => Center(
child: FutureBuilder<File>( child: FutureBuilder<Uint8List>(
future: future, future: _future,
builder: (final context, final fileSnapshot) { builder: (final context, final fileSnapshot) {
if (fileSnapshot.hasData) { if (!fileSnapshot.hasData) {
final content = fileSnapshot.requireData.readAsBytesSync(); return SizedBox(
try {
// TODO: Is this safe enough?
if (widget.isSvgHint || utf8.decode(content).contains('<svg')) {
return SvgPicture.memory(
content,
height: widget.size?.height,
width: widget.size?.width,
fit: widget.fit ?? BoxFit.contain,
colorFilter: widget.svgColor != null ? ColorFilter.mode(widget.svgColor!, BlendMode.srcIn) : null,
);
}
} catch (_) {
// If the data is not UTF-8
}
return Image.memory(
content,
height: widget.size?.height,
width: widget.size?.width, width: widget.size?.width,
fit: widget.fit, child: NeonLinearProgressIndicator(
gaplessPlayback: true, color: widget.iconColor,
),
); );
} }
if (fileSnapshot.hasError) { if (fileSnapshot.hasError) {
return NeonException( return NeonException(
fileSnapshot.error, fileSnapshot.error,
onRetry: () { onRetry: () {
setState(() { setState(() {
// ignore: discarded_futures // ignore: discarded_futures
future = widget.getImageFile(); _future = _readImage();
}); });
}, },
onlyIcon: true, onlyIcon: true,
@ -74,11 +62,30 @@ class _NeonCachedImageState extends State<NeonCachedImage> {
color: widget.iconColor ?? Theme.of(context).colorScheme.error, color: widget.iconColor ?? Theme.of(context).colorScheme.error,
); );
} }
return SizedBox(
final content = fileSnapshot.requireData;
try {
// TODO: Is this safe enough?
if (widget.isSvgHint || utf8.decode(content).contains('<svg')) {
return SvgPicture.memory(
content,
height: widget.size?.height,
width: widget.size?.width,
fit: widget.fit ?? BoxFit.contain,
colorFilter: widget.svgColor != null ? ColorFilter.mode(widget.svgColor!, BlendMode.srcIn) : null,
);
}
} catch (_) {
// If the data is not UTF-8
}
return Image.memory(
content,
height: widget.size?.height,
width: widget.size?.width, width: widget.size?.width,
child: NeonLinearProgressIndicator( fit: widget.fit,
color: widget.iconColor, gaplessPlayback: true,
),
); );
}, },
), ),

Loading…
Cancel
Save