In Jetpack Compose, Does paddingFromBaseline alter the padding of a Text element?

561 Views Asked by At
@Composable
fun Sample(){
    val startPadding = 10.dp
    Column {
        Text(
            text = "sample text",
            modifier = Modifier
                .paddingFromBaseline(top = 30.dp)
                .padding(start = startPadding)
        )
        Text(
            text = "sample text",
            modifier = Modifier.padding(start = startPadding)
        )
    }
}

The above Composable contains two Text elements arranged vertically in a Column. Both Text elements have the same value set for their start padding. The preview is below

code preview 1

something weird happens when the paddingFromBaseline is set after padding i.e

@Composable
fun Sample(){
    val startPadding = 10.dp
    Column {
        Text(
            text = "sample text",
            modifier = Modifier
                .padding(start = startPadding)
                .paddingFromBaseline(top = 30.dp)
        )
        Text(
            text = "sample text",
            modifier = Modifier.padding(start = startPadding)
        )
    }
}

code preview 2

The padding before the first Text seems to have doubled, is this a bug or a feature?

I'm under the impression that the order in which Modifier methods are called doesn't matter so the extra padding shouldn't be there.

1

There are 1 best solutions below

0
On

I no longer see this issue you mention on the emulator or preview. The .padding(start= should add padding to the start. The paddingFromBaseline(top= should add padding from Baseline (ie) you should see top padding getting added. This is the output of your code that I see

enter image description here

I'm under the impression that the order in which Modifier methods are called doesn't matter so the extra padding shouldn't be there.

No. The order of the Modifier methods MATTER and you should be concise when you chain your Modifier methods. Refer the official doc