Skip to content

Instantly share code, notes, and snippets.

@titoaesj
Last active December 2, 2021 12:21
Show Gist options
  • Save titoaesj/df818185c52967430d86d1730143df7e to your computer and use it in GitHub Desktop.
Save titoaesj/df818185c52967430d86d1730143df7e to your computer and use it in GitHub Desktop.
Android Compose - ChipVerticalGrid
@Composable
fun ChipVerticalGrid(
modifier: Modifier = Modifier,
spacing: Dp,
content: @Composable () -> Unit
) {
Layout(
content = content,
modifier = modifier
) { measurables, constraints ->
var currentRow = 0
var currentOrigin = IntOffset.Zero
val spacingValue = spacing.toPx().toInt()
val placeables = measurables.map { measurable ->
val placeable = measurable.measure(constraints)
if (currentOrigin.x > 0f && currentOrigin.x + placeable.width > constraints.maxWidth) {
currentRow += 1
currentOrigin = currentOrigin.copy(x = 0, y = currentOrigin.y + placeable.height + spacingValue)
}
placeable to currentOrigin.also {
currentOrigin = it.copy(x = it.x + placeable.width + spacingValue)
}
}
layout(
width = constraints.maxWidth,
height = placeables.lastOrNull()?.run { first.height + second.y } ?: 0
) {
placeables.forEach {
val (placeable, origin) = it
placeable.place(origin.x, origin.y)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment