Skip to content

Instantly share code, notes, and snippets.

@fvilarino
Last active April 26, 2021 16:24
Show Gist options
  • Save fvilarino/2ac059e8d0544483e238554d6d704f96 to your computer and use it in GitHub Desktop.
Save fvilarino/2ac059e8d0544483e238554d6d704f96 to your computer and use it in GitHub Desktop.
Info Label Basic
@Composable
fun InfoLabels(
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
// 1
Layout(
content = content,
modifier = modifier,
) { measurables, constraints ->
// 2
require(measurables.size % 2 == 0) { "InfoLabels requires an even number of children" }
// 3
val looseConstraints = constraints.copy(
minWidth = 0,
minHeight = 0,
)
// 4
val placeables = measurables.map { measurable ->
measurable.measure(looseConstraints)
}
// 5
val labels = List(placeables.size / 2) { index ->
placeables[2 * index]
}
// 6
val descriptions = List(placeables.size / 2) { index ->
placeables[2 * index + 1]
}
// 7
val maxLabelWidth = labels.maxByOrNull { it.width }?.width ?: 0
// 8
val height = List(labels.size) { index ->
max(labels[index].height, descriptions[index].height)
}.sum()
// 9
layout(
constraints.maxWidth,
height.coerceAtMost(constraints.maxHeight)
) {
var yPosition = 0
// 10
for (i in labels.indices) {
val label = labels[i]
val description = descriptions[i]
// 11
val cellHeight = max(label.height, description.height)
// 12
label.place(x = 0, y = yPosition)
// 13
description.place(x = maxLabelWidth, y = yPosition)
// 14
yPosition += cellHeight
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment