Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose - Should modifier parameter be applied to the outer / top most view only?

In jetpack compose, when passing a modifier into my composable, should the modifier be applied only to the outer most view, or all of the subviews as well?

Here's a quick example:

fun SomeComposable(modifier: Modifier = Modifier) {
    Column(modifier = modifier) {
        Text(modifier = modifier.extraModifiersIfNeeded(), text = "Text")
        Text(modifier = modifier.extraModifiersIfNeeded(), text = "Text")
        Text(modifier = modifier.extraModifiersIfNeeded(), text = "Text")
    }
}

I would initially assume this is incorrect, because any modifier passed into SomeComposable would also change all of the subviews.

Are there any documentations making this very clear? Can I please have a link to any documentations?

Thank you!

like image 270
SomeKoder Avatar asked Oct 14 '25 11:10

SomeKoder


1 Answers

When you pass a modifier to a composable, it should be elevated to the highest level that makes sense for your use case. This is because you could have unintended side effects say when you apply padding to your example. The Column will have the padding applied to its contents, but then there would be additional padding to each Text composable.

This documentation says to "extract them to the highest level possible".

like image 105
OhWittmannOne Avatar answered Oct 17 '25 04:10

OhWittmannOne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!