Skip to content

Instantly share code, notes, and snippets.

@AlexGladkov
Last active November 15, 2022 04:05
Show Gist options
  • Save AlexGladkov/064d556e52ed19242e0854229bd2588e to your computer and use it in GitHub Desktop.
Save AlexGladkov/064d556e52ed19242e0854229bd2588e to your computer and use it in GitHub Desktop.
Jetpack Compose Layout To Tag Cloud
@Composable
fun TagHost(
modifier: Modifier = Modifier,
verticalPadding: Dp = 24.dp,
content: @Composable () -> Unit
) {
Layout(
modifier = modifier,
content = content
) { measurables, constraints ->
// Don't constrain child views further, measure them with given constraints
// List of measured children
val placeables = measurables.map { measurable ->
// Measure each children
// Set minHeight = 0 for weight = 1f variant
measurable.measure(constraints.copy(minHeight = 0))
}
// Set the size of the layout as big as it can
layout(constraints.minWidth, constraints.maxHeight) {
// Track the y co-ord we have placed children up to
var xPosition = 0
var yPosition = 0
// Place children in the parent layout
placeables.forEach { placeable ->
// Position item on the screen
placeable.placeRelative(x = xPosition, y = yPosition)
xPosition += placeable.width
if (xPosition >= constraints.maxWidth) {
yPosition += placeable.height + verticalPadding.value.toInt()
xPosition = 0
}
}
}
}
}
@AlexGladkov
Copy link
Author

This is very old gist, I did it because compose was in alpha. Now compose has accompanist library with FlowRow component

https://google.github.io/accompanist/flowlayout/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment