Joseph Hawkes-Cates
09/01/2022, 9:43 PMJoseph Hawkes-Cates
09/01/2022, 9:45 PMColumn {
Row(
modifier = Modifier.fillMaxWidth()
) {
CheckBox(...)
Text(...)
}
}
Essentially there’s cases where the text can wrap and I want to make all the rows the same height when that happens.Francesc
09/01/2022, 9:46 PMJoseph Hawkes-Cates
09/01/2022, 9:47 PMFrancesc
09/01/2022, 10:00 PMLayout
and query the children's desired height, then use constrains with fixed dimensions to measure them all withFrancesc
09/01/2022, 10:01 PMweight
? just set the same weight to all childrenJoseph Hawkes-Cates
09/01/2022, 10:15 PMJoseph Hawkes-Cates
09/01/2022, 10:20 PMFrancesc
09/01/2022, 10:38 PM@Composable
fun MyColumn(
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
Layout(
modifier = modifier,
content = content,
) { measurables, constraints ->
val itemHeight = measurables.maxOfOrNull { measurable ->
measurable.maxIntrinsicHeight(width = constraints.maxWidth)
} ?: 0
val itemConstraints = Constraints(
minWidth = 0,
maxWidth = constraints.maxWidth,
minHeight = itemHeight,
maxHeight = itemHeight,
)
val placeables = measurables.map { measurable -> measurable.measure(itemConstraints) }
layout(
width = constraints.maxWidth,
height = measurables.size * itemHeight,
) {
placeables.forEachIndexed { index, placeable ->
placeable.placeRelative(
x = 0,
y = index * itemHeight,
)
}
}
}
}
which you use like this
MyTheme {
Surface(color = Theme.colors.background) {
MyColumn(modifier = Modifier.fillMaxWidth().height(300.dp)) {
Text(
text = "Foobar",
modifier = Modifier.fillMaxWidth().background(Color.Red.copy(alpha = .3f)),
)
Text(
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc metus ante, hendrerit at erat ornare, molestie sollicitudin dui. Sed porttitor a nisl vel ullamcorper. Vivamus sodales augue et dui volutpat, eu pharetra augue tincidunt",
modifier = Modifier.fillMaxWidth().background(Color.Blue.copy(alpha = .3f)),
)
Text(
text = "BarBaz",
modifier = Modifier.fillMaxWidth().background(Color.Red.copy(alpha = .3f)),
)
}
}
}
Joseph Hawkes-Cates
09/02/2022, 12:59 AMJoseph Hawkes-Cates
09/02/2022, 5:04 PMFrancesc
09/02/2022, 5:05 PMJoseph Hawkes-Cates
09/02/2022, 5:07 PMJoseph Hawkes-Cates
09/02/2022, 5:20 PMColumn {
Row {
Checkbox(...)
Text(...)
}
Divider(...)
}
If I set the height of the internal column and row to be Intrinsic.Min then it works as expectedJoseph Hawkes-Cates
09/02/2022, 5:22 PMJoseph Hawkes-Cates
09/02/2022, 5:25 PM