Skip to content

Instantly share code, notes, and snippets.

@takahirom
Created November 9, 2021 01:17
Show Gist options
  • Save takahirom/9d7b03cb4a74ea2289c2fd18017e2e0f to your computer and use it in GitHub Desktop.
Save takahirom/9d7b03cb4a74ea2289c2fd18017e2e0f to your computer and use it in GitHub Desktop.
@Composable
inline fun MyVerticalBox(
name: String,
modifier: Modifier = Modifier,
content: @Composable () -> Unit = {}
) {
Layout(
content = { content() },
modifier = modifier
) { measurables: List<Measurable>,
constraints: Constraints ->
// measure children
val placeables = measurables.mapIndexed { index, measurable ->
measurable.measure(constraints)
}
// deside own size
val height = placeables.sumOf { it.height }.takeIf { it != 0 } ?: constraints.minHeight
val width = placeables.maxOfOrNull { it.width } ?: constraints.minWidth
val layout = layout(width, height) {
// place children
var y = 0
placeables.forEach { placable: Placeable ->
placable.placeRelative(x = 0, y = y)
y += placable.height
}
}
layout
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment