Marko Novaković
06/08/2024, 10:05 PMGridCells.Adaptive
does it? or I have to write logic myself?Alex Styl
06/08/2024, 10:54 PMMarko Novaković
06/08/2024, 11:36 PMGridCells.Adaptive
) documentation:
/**
* Defines a grid with as many rows or columns as possible on the condition that
* every cell has at least [minSize] space and all extra space distributed evenly.
*
* For example, for the vertical [LazyVerticalGrid] Adaptive(20.dp) would mean that
* there will be as many columns as possible and every column will be at least 20.dp
* and all the columns will have equal width. If the screen is 88.dp wide then
* there will be 4 columns 22.dp each.
*/
Marko Novaković
06/08/2024, 11:37 PMGridCells.Adaptive
code and that’s it
private fun BoxWithConstraintsScope.calculateItemSize(
density: Density,
contentPadding: PaddingValues,
arrangement: Arrangement.HorizontalOrVertical,
minItemSize: Dp,
): Dp {
with(density) {
val horizontalPadding =
contentPadding.calculateStartPadding(LayoutDirection.Ltr) +
contentPadding.calculateEndPadding(LayoutDirection.Ltr)
val spacing = arrangement.spacing.roundToPx()
val rowWidth = constraints.maxWidth - horizontalPadding.roundToPx()
val count = maxOf((rowWidth + spacing) / (minItemSize.roundToPx() + spacing), 1)
val gridSizeWithoutSpacing = rowWidth - spacing * (count - 1)
return (gridSizeWithoutSpacing / count).toDp()
}
}
Marko Novaković
06/21/2024, 5:42 PM