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. 59
      packages/neon/neon/lib/src/widgets/cached_image.dart

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

@ -27,15 +27,43 @@ abstract class NeonCachedImage extends StatefulWidget {
}
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
Widget build(final BuildContext context) => Center(
child: FutureBuilder<File>(
future: future,
child: FutureBuilder<Uint8List>(
future: _future,
builder: (final context, final fileSnapshot) {
if (fileSnapshot.hasData) {
final content = fileSnapshot.requireData.readAsBytesSync();
if (!fileSnapshot.hasData) {
return SizedBox(
width: widget.size?.width,
child: NeonLinearProgressIndicator(
color: widget.iconColor,
),
);
}
if (fileSnapshot.hasError) {
return NeonException(
fileSnapshot.error,
onRetry: () {
setState(() {
// ignore: discarded_futures
_future = _readImage();
});
},
onlyIcon: true,
iconSize: widget.size?.shortestSide,
color: widget.iconColor ?? Theme.of(context).colorScheme.error,
);
}
final content = fileSnapshot.requireData;
try {
// TODO: Is this safe enough?
@ -59,27 +87,6 @@ class _NeonCachedImageState extends State<NeonCachedImage> {
fit: widget.fit,
gaplessPlayback: true,
);
}
if (fileSnapshot.hasError) {
return NeonException(
fileSnapshot.error,
onRetry: () {
setState(() {
// ignore: discarded_futures
future = widget.getImageFile();
});
},
onlyIcon: true,
iconSize: widget.size?.shortestSide,
color: widget.iconColor ?? Theme.of(context).colorScheme.error,
);
}
return SizedBox(
width: widget.size?.width,
child: NeonLinearProgressIndicator(
color: widget.iconColor,
),
);
},
),
);

Loading…
Cancel
Save