Is there a way to ensure that a composable would a...
# compose
a
Is there a way to ensure that a composable would always be square? So that regardless what the size of its content, the Width or Height would always be equal? I tried:
Copy code
modifier.layout { measurable, constraints ->
    val placeable = measurable.measure(constraints)

    val wrapperWidth = placeable.width.coerceIn(constraints.minWidth, constraints.maxWidth)
    val wrapperHeight = placeable.height.coerceIn(constraints.minHeight, constraints.maxHeight)
    val size = wrapperWidth.coerceAtLeast(wrapperHeight)
    layout(size, size) {
        placeable.placeRelative(0, 0)
    }
}
But I am only altering the position here since I already measured the placeable. Any hints?
a
Modifier.aspectRatio(1f)
a
Aspect ratio seems to completely consume all the space though...
a
Not really, we use it for thumbnails
One of the dimensions should be defined I think
it has to relate to something
wrapContent() works too if I remember correctly
I think it takes the bigger side and matches the other
We use it in combo with `fillMaxWidth`or
weight
,
wrapContentSize
. It really depends on your use-case
a
doesn't work unless you define the width or height or else it fills the whole space... your thumbnail probably have defined width or height thats why
the problem is that I can't define the width or height as it needs to adjust to its content...
j
cc @Adam Powell I think maybe this would require multi-pass layout, which is prohibited?
a
@Archie I see, I got the difference from your use case
a
@jim I'm also thinking if
SubcomposeLayout
would be the best fit for this usecase? I haven't really tried not really sure.
t
Did you tried this? :
Copy code
Modifier.preferredHeight(IntrinsicSize.Min).aspectRatio(1f)
a
@Timo Drick, it would work. That assumes that the largest dimension is Height... If the bigger dimension is Width, then it will fail.