Last active
November 15, 2022 04:05
-
-
Save AlexGladkov/064d556e52ed19242e0854229bd2588e to your computer and use it in GitHub Desktop.
Jetpack Compose Layout To Tag Cloud
This file contains 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 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 | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/