I created custom button with coil. But the preview...
# compose
c
I created custom button with coil. But the preview isn't working. How can I make it displayable in preview?
Copy code
fun GifImage(
    @DrawableRes resId: Int,
    contentDescription: String,
    modifier: Modifier = Modifier,
) {
    val imageLoader = ImageLoader.Builder(LocalContext.current)
        .components {
            if (SDK_INT >= 28) {
                add(ImageDecoderDecoder.Factory())
            } else {
                add(GifDecoder.Factory())
            }
        }.build()
    Image(
        painter = rememberAsyncImagePainter(resId, imageLoader),
        contentDescription = contentDescription,
        modifier = modifier
    )
}
c
Coil only displays the
placeholder
element in preview since the preview environment is a bit special. IIRC certain decoders won’t work inside preview
Also I’d
remember
the
ImageLoader
so it’s not recreated on each composition
s
Is this still the case? Would it not be nice to be able to provide a preview only ImageLoader, something like this
class PreviewImageLoader(private val drawable: Drawable) : ImageLoader {...
to your previews to do something like render all the preview images with a filled red color or something like that, just to know a but better what the final UI will look like before running the app?