Last active
December 2, 2021 12:21
-
-
Save titoaesj/df818185c52967430d86d1730143df7e to your computer and use it in GitHub Desktop.
Android Compose - ChipVerticalGrid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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