Using AsyncImagePreviewHandler with rememberAsyncImagePainter for Preview not working #2851
Answered
by
colinrtwhite
MihaMarkic
asked this question in
Q&A
-
Using coil 3.1.0 and Compose (obviously). Trying to use Relevant code below. @OptIn(ExperimentalCoilApi::class)
@Preview
@Composable
private fun Preview() {
val previewHandler = AsyncImagePreviewHandler {
ColorImage(Color.Yellow.toArgb(), 1920, 1080)
}
CompositionLocalProvider(LocalAsyncImagePreviewHandler provides previewHandler) {
FullPhoto(
"", "Sample"
)
}
} @Composable
fun FullPhoto(url: String, description: String, modifier: Modifier = Modifier) {
val painter =
rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalPlatformContext.current)
.data(url)
.build()
)
val state by painter.state.collectAsState()
when (state) {
is AsyncImagePainter.State.Empty, is AsyncImagePainter.State.Loading -> {
CircularProgressIndicator(
modifier = Modifier
.width(60.dp)
.height(60.dp)
)
}
is AsyncImagePainter.State.Success -> {
Image(
painter = painter,
contentScale = ContentScale.Crop,
contentDescription = description,
modifier = modifier
.fillMaxWidth()
)
}
is AsyncImagePainter.State.Error -> {
Text("?")
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
colinrtwhite
Feb 25, 2025
Replies: 1 comment 2 replies
-
Hmm your code looks OK. Going to move this into an issue ticket to investigate: #2859 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MihaMarkic After investigating,
AsyncImagePreviewHandler
is working as expected, though it returns theLoading
state instead ofSuccess
. ReturningLoading
allows us to return a nullImage
, however I think it's more important to returnSuccess
so I've modified the behaviour here.