Farid Mrz
03/23/2021, 1:39 PMFarid Mrz
03/23/2021, 1:39 PMfun Modifier.firstBaselineToTop(
firstBaselineToTop: Dp
) = Modifier.layout { measurable, constraints ->
val placeable = measurable.measure(constraints)
// Check the composable has a first baseline
check(placeable[FirstBaseline] != AlignmentLine.Unspecified)
val firstBaseline = placeable[FirstBaseline]
val placeableY = firstBaselineToTop.roundToPx() - firstBaseline
val height = placeable.height + placeableY
layout(placeable.width, height) {
placeable.placeRelative(0, placeableY)
}
}
Farid Mrz
03/23/2021, 1:39 PM@Composable
fun BodyContent(modifier: Modifier = Modifier) {
MYColumn(modifier = modifier) {
Text(text = "Hi there!")
Text(text = "Thanks for going through the Layouts codelab")
Row {
Text(
text = "1000",
style = MaterialTheme.typography.h3,
modifier = Modifier.firstBaselineToTop(8.dp)
)
Text(
text = "EUR",
style = MaterialTheme.typography.body2,
modifier = Modifier.firstBaselineToTop(8.dp)
)
}
}
}
Farid Mrz
03/23/2021, 1:40 PMZach Klippenstein (he/him) [MOD]
03/23/2021, 3:19 PMfun Modifier.firstBaselineToTop(
firstBaselineToTop: Dp
) = Modifier.layoutWhen you define your own modifier function, it takes a
Modifier
receiver – that is not just syntax, that’s the actual upstream modifier chain. You must chain your custom modifier onto that, using then()
. What your code does is drop the upstream modifier chain completely by using the Modifier
companion object. This code should be: ) = layout { …
Farid Mrz
03/23/2021, 4:35 PM