Skip to content

Instantly share code, notes, and snippets.

@takahirom
Last active November 10, 2021 00:28
Show Gist options
  • Save takahirom/aee818f8a25c097b6b782514543c143c to your computer and use it in GitHub Desktop.
Save takahirom/aee818f8a25c097b6b782514543c143c 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)
}
// ② Decide own size
val height = maxOf(placeables.sumOf { it.height }, constraints.minHeight)
val width = maxOf(placeables.maxOfOrNull { it.width } ?: 0, 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