Reputation: 31
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
Reputation: 363439
You can apply the aspectRatio
modifier to the item.
For example:
LazyVerticalGrid(
/* .. */
) {
items(itemsList) {
Box(Modifier.aspectRatio(1f)) {
}
}
}
Upvotes: 11