https://kotlinlang.org logo
#compose
Title
# compose
m

Marc-Antoine Sauvé

10/18/2023, 6:53 PM
Hello! I have an Android Compose question. My colleague created a StackOverflow question for this: https://stackoverflow.com/questions/77317620/aspectratio-modifier-breaks-min-vertical-sizing-of-containing-box TLDR: The AspectRatio modifier breaks min vertical sizing of containing box and we don't understand why and how to work around it. More details in thread.
In short, we have an outer box that should be as small as needed in height. We use
IntrinsicSize.Min
for that with
propagateMinConstraints = true
which does the trick. We have an Image inside that have an
aspectRatio
. For some reason, the aspect ratio is also applied to the outer
Box
and we do not know why. We expect the
Box
to be the same height as the
Image
.
Copy code
@Preview(showBackground = true, widthDp = 300)
@Composable
fun TestZStackCompose() {
    // ZStack
    Box(
        modifier = Modifier
            .width(intrinsicSize = IntrinsicSize.Min)
            .height(intrinsicSize = IntrinsicSize.Min)
        ,
        propagateMinConstraints = true,
    ) {
        // Aligned layer
        Box(contentAlignment = Alignment.Center, modifier = Modifier.background(Color.Red)) {
            // Image content
            Image(
                painter = rememberVectorPainter(image = Icons.Filled.Close),
                contentDescription = null,
                modifier = Modifier
                    .background(Color.Blue)
                    .width(40.dp)
                    .aspectRatio(ratio = 1f)
            )
        }
    }
}