2024-02-27 10:51:19 -05:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
class FadeInPlaceholderImage extends StatelessWidget {
|
|
|
|
|
final Widget placeholder;
|
|
|
|
|
final ImageProvider image;
|
|
|
|
|
final Duration duration;
|
|
|
|
|
final BoxFit fit;
|
2025-08-13 18:01:47 -04:00
|
|
|
final double width;
|
|
|
|
|
final double height;
|
2024-02-27 10:51:19 -05:00
|
|
|
|
|
|
|
|
const FadeInPlaceholderImage({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.placeholder,
|
|
|
|
|
required this.image,
|
2025-08-13 18:01:47 -04:00
|
|
|
required this.width,
|
|
|
|
|
required this.height,
|
2024-02-27 10:51:19 -05:00
|
|
|
this.duration = const Duration(milliseconds: 100),
|
|
|
|
|
this.fit = BoxFit.cover,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-08-13 18:01:47 -04:00
|
|
|
final stopwatch = Stopwatch()..start();
|
|
|
|
|
return Image(
|
|
|
|
|
image: image,
|
|
|
|
|
frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
|
|
|
|
|
if (frame == null) {
|
|
|
|
|
return AnimatedSwitcher(duration: duration, child: placeholder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopwatch.stop();
|
|
|
|
|
if (stopwatch.elapsedMilliseconds < 32) {
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
return AnimatedSwitcher(duration: duration, child: child);
|
|
|
|
|
},
|
|
|
|
|
filterQuality: FilterQuality.low,
|
|
|
|
|
fit: fit,
|
|
|
|
|
width: width,
|
|
|
|
|
height: height,
|
2024-02-27 10:51:19 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|