2024-09-10 13:53:05 +00:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
|
|
|
import 'package:solian/platform.dart';
|
|
|
|
import 'package:solian/widgets/sized_container.dart';
|
|
|
|
|
|
|
|
class AutoCacheImage extends StatelessWidget {
|
|
|
|
final String url;
|
|
|
|
final double? width, height;
|
|
|
|
final BoxFit? fit;
|
|
|
|
final bool noProgressIndicator;
|
|
|
|
final bool noErrorWidget;
|
2024-09-17 12:59:01 +00:00
|
|
|
final bool isDense;
|
2024-09-10 13:53:05 +00:00
|
|
|
|
|
|
|
const AutoCacheImage(
|
|
|
|
this.url, {
|
|
|
|
super.key,
|
|
|
|
this.width,
|
|
|
|
this.height,
|
|
|
|
this.fit,
|
|
|
|
this.noProgressIndicator = false,
|
|
|
|
this.noErrorWidget = false,
|
2024-09-17 12:59:01 +00:00
|
|
|
this.isDense = false,
|
2024-09-10 13:53:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (PlatformInfo.canCacheImage) {
|
|
|
|
return CachedNetworkImage(
|
|
|
|
imageUrl: url,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
progressIndicatorBuilder: noProgressIndicator
|
|
|
|
? null
|
|
|
|
: (context, url, downloadProgress) => Center(
|
2024-09-23 15:19:52 +00:00
|
|
|
child: TweenAnimationBuilder(
|
|
|
|
tween: Tween(
|
|
|
|
begin: 0,
|
|
|
|
end: downloadProgress.progress ?? 0,
|
|
|
|
),
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
builder: (context, value, _) => CircularProgressIndicator(
|
|
|
|
value: downloadProgress.progress != null
|
|
|
|
? value.toDouble()
|
|
|
|
: null,
|
|
|
|
),
|
2024-09-10 13:53:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
errorWidget: noErrorWidget
|
|
|
|
? null
|
|
|
|
: (context, url, error) {
|
|
|
|
return Material(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: CenteredContainer(
|
|
|
|
maxWidth: 280,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2024-09-17 12:59:01 +00:00
|
|
|
Icon(Icons.close, size: isDense ? 24 : 32)
|
2024-09-10 13:53:05 +00:00
|
|
|
.animate(onPlay: (e) => e.repeat(reverse: true))
|
|
|
|
.fade(duration: 500.ms),
|
2024-09-17 12:59:01 +00:00
|
|
|
if (!isDense)
|
|
|
|
Text(
|
|
|
|
error.toString(),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
2024-09-10 13:53:05 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Image.network(
|
|
|
|
url,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
fit: fit,
|
|
|
|
loadingBuilder: noProgressIndicator
|
|
|
|
? null
|
|
|
|
: (BuildContext context, Widget child,
|
|
|
|
ImageChunkEvent? loadingProgress) {
|
|
|
|
if (loadingProgress == null) return child;
|
|
|
|
return Center(
|
2024-09-23 15:19:52 +00:00
|
|
|
child: TweenAnimationBuilder(
|
|
|
|
tween: Tween(
|
|
|
|
begin: 0,
|
|
|
|
end: loadingProgress.expectedTotalBytes != null
|
|
|
|
? loadingProgress.cumulativeBytesLoaded /
|
|
|
|
loadingProgress.expectedTotalBytes!
|
|
|
|
: 0,
|
|
|
|
),
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
builder: (context, value, _) => CircularProgressIndicator(
|
|
|
|
value: loadingProgress.expectedTotalBytes != null
|
|
|
|
? value.toDouble()
|
|
|
|
: null,
|
|
|
|
),
|
2024-09-10 13:53:05 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
errorBuilder: noErrorWidget
|
|
|
|
? null
|
|
|
|
: (context, error, stackTrace) {
|
|
|
|
return Material(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: CenteredContainer(
|
|
|
|
maxWidth: 280,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2024-09-17 12:59:01 +00:00
|
|
|
Icon(Icons.close, size: isDense ? 24 : 32)
|
2024-09-10 13:53:05 +00:00
|
|
|
.animate(onPlay: (e) => e.repeat(reverse: true))
|
|
|
|
.fade(duration: 500.ms),
|
2024-09-17 12:59:01 +00:00
|
|
|
if (!isDense)
|
|
|
|
Text(
|
|
|
|
error.toString(),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
2024-09-10 13:53:05 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ImageProvider provider(String url) {
|
|
|
|
if (PlatformInfo.canCacheImage) {
|
|
|
|
return CachedNetworkImageProvider(url);
|
|
|
|
}
|
|
|
|
return NetworkImage(url);
|
|
|
|
}
|
|
|
|
}
|