user19611385
user19611385

Reputation: 31

LazyVerticalGrid Adaptive square items

Is there an efficient way to make adaptive grid's item height equal to the width determined in runtime? Basically I want square items.

So far I've tried this:

var size by remember { mutableStateOf(IntSize.Zero) }

Column( // item
...
     .onSizeChanged {
        size = it
     }
    .size(with(LocalDensity.current) {
        size.width.toDp()
    })
)

but I'm new to compose and think it might be an expensive operation. Any alternatives?

Upvotes: 3

Views: 1365

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363439

You can apply the aspectRatio modifier to the item. For example:

LazyVerticalGrid(
    /* .. */
) {
    items(itemsList) {
        Box(Modifier.aspectRatio(1f)) {

        }
    }
}

enter image description here

Upvotes: 11

Related Questions