Colton Idle
06/30/2022, 4:50 AM@Composable
fun MyImageComposable(modifier: Modifier) {
AsyncImage(model = "<https://image>", contentDescription = null,
modifier=modifier.onGloballyPositioned { layoutCoordinates ->
val width = layoutCoordinates.size.width
val hegiht = layoutCoordinates.size.height
if (width == 0 || hegiht == 0){
throw RuntimeException("You will have layout shift! Please add a size")
}
})
}
Albert Chang
06/30/2022, 5:36 AMfun Modifier.enforceFixedSize(): Modifier = layout { measurable, constraints ->
check(constraints.hasFixedWidth && constraints.hasFixedHeight) { "Non-fixed size!" }
val placeable = measurable.measure(constraints)
layout(placeable.width, placeable.height) {
placeable.placeRelative(0, 0)
}
}
Colton Idle
06/30/2022, 5:38 AM@Composable
fun MyImageComposable(modifier: Modifier) {
AsyncImage(model = "<https://image>", contentDescription = null,
modifier=modifier.enforceFixedSize())
}
but just curious if your layout {} has a benefit over my onGloballyPositionedAlbert Chang
06/30/2022, 5:40 AMColton Idle
06/30/2022, 5:42 AMcheck()
doesn't show the actual callsite of the composable in the stack, just the modifier itself. any ideas on how to get the actual composable thats usuing this modifier to be in the stacktrace?Albert Chang
06/30/2022, 6:51 AMColton Idle
06/30/2022, 2:26 PM@Composable
fun MyComposable() {
Row(modifier = Modifier.fillMaxWidth()) {
val imageUrl = "<https://someimageurl.com/img.png>"
AsyncImage(
model = imageUrl,
contentDescription = null,
modifier = Modifier.fillMaxWidth().aspectRatio(200 / 70F).enforceFixSize(),
)
}
}
Seems pretty standard. But this will crash. If I remove the Row() then everything works fine. Any ideas? I'm assuming it has something to do with the composable that actually holds MyComposable. But in itself this MyCOmposable looks pretty sane right? or have i been looking at this too long?