javaandroidkotlinandroid-jetpack-composepadding

How can we define a column with padding and just one item doesn't inherit the padding of the column in Jetpack Compose?


I have a column with many items, and I want to set the padding for the column because all items have the same padding except one of them which doesn't have padding, how can I consider an exception for that specific item? Or is it possible to add an attribute that says don't get your parent padding? I know I can remove padding for column and add one by one, but I want to ask anybody knows that, is there any other way?

Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState()),
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
 Text(text = "Teaser1")

Text(text = "Teaser2")

Text(text = "Teaser3")

Text(text = "Teaser")

Text(text = "Teaser4")

Text(text = "Teaser5")

...

}

Thank you in advance for your help.


Solution

  • You can use Modifier.layout.

    I override the width constraint by adding padding for measure, and applying the padding offset during place.

    Text(
        text = "Teaser",
        modifier = Modifier
            .layout { measurable, constraints ->
                val noPaddingConstraints = constraints.copy(
                    maxWidth = constraints.maxWidth + (padding * 2).roundToPx()
                ) 
                val placeable = measurable.measure(noPaddingConstraints)
                layout(placeable.width, placeable.height) {
                    placeable.place(x = -padding.roundToPx(), y = 0)
                }
            }
    )