I have a custom padding modifier from the codelab....
# compose
f
I have a custom padding modifier from the codelab. However when using that modifier for a text inside a collumn with other texts the position is incorrect. My guess is the position based on the constraints are not respected. Anyone can point to what im missing?
Copy code
fun 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)
    }
}
Copy code
@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)
            )
        }
    }
}
z
fun Modifier.firstBaselineToTop(
firstBaselineToTop: Dp
) = Modifier.layout
When 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 { …
f
Thanks for your reply but that was not the issue. Since I was applying only one modifier this is nog an issue and there is no chaining needed. The actual issue is that my padding was smaller than my textsize. Therefore creating a negative offset and so the text was over the previous line. So just using a padding bigger than the text size solves the issue.