android6-1 Exercise 1: A modifier and a layout =========================================== import androidx.compose.ui.Modifier import androidx.compose.ui.layout.layout import androidx.compose.ui.unit.Dp // A custom modifier: add space above the first line of content fun Modifier.paddingFromTop(top: Dp) = layout { measurable, constraints -> val placeable = measurable.measure(constraints) // 1. measure the wrapped element val extra = top.roundToPx() layout(placeable.width, placeable.height + extra) { // 2. report a size placeable.placeRelative(x = 0, y = extra) // 3. place it } } import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.layout.Layout // A custom layout: children stacked vertically, like a simple Column @Composable fun SimpleColumn(modifier: Modifier = Modifier, content: @Composable () -> Unit) { Layout(content = content, modifier = modifier) { measurables, constraints -> val placeables = measurables.map { it.measure(constraints) } val width = (placeables.maxOfOrNull { it.width } ?: 0) .coerceIn(constraints.minWidth, constraints.maxWidth) val height = placeables.sumOf { it.height } .coerceIn(constraints.minHeight, constraints.maxHeight) layout(width, height) { var y = 0 placeables.forEach { placeable -> placeable.placeRelative(x = 0, y = y) y += placeable.height } } } } Preview: three Text children inside SimpleColumn, then the same inside Box(Modifier.width(80.dp)). Expected: the children stack vertically; with a narrow width the Text children are measured with the incoming constraints, so their text wraps within 80.dp (they were given the maximum width); with the coerceIn calls removed, a tall column could report a height larger than the constraints allow, which is a bug (the layout would be given a size it cannot have). Report what you saw. (The measure, report, place pattern and placeRelative are from the docs; the coerceIn reasoning is general knowledge.) Code unrun.