precacheImages function
A custom PumpAction to ensure that the images for all Image, FadeInImage, and DecoratedBox widgets are loaded before the golden file is generated.
See PumpAction for more details.
Implementation
Future<void> precacheImages(WidgetTester tester) async {
await tester.runAsync(() async {
final images = <Future<void>>[];
for (final element in find.byType(Image).evaluate()) {
final widget = element.widget as Image;
final image = widget.image;
images.add(precacheImage(image, element));
}
for (final element in find.byType(FadeInImage).evaluate()) {
final widget = element.widget as FadeInImage;
final image = widget.image;
images.add(precacheImage(image, element));
}
for (final element in find.byType(DecoratedBox).evaluate()) {
final widget = element.widget as DecoratedBox;
final decoration = widget.decoration;
if (decoration is BoxDecoration && decoration.image != null) {
final image = decoration.image!.image;
images.add(precacheImage(image, element));
}
}
await Future.wait(images);
});
await tester.pumpAndSettle();
}