Is .then() syntax a strict requirement when concatenating Compose modifiers?

Viewed 43

I would like to know whether this is necessary to use .then() when combining Compose modifiers. Let's consider a simple example.

@Composable
fun TestComposable(
    modifier: Modifier = Modifier
) {
   // content 1 or 2
}
  1. Row(modifier = modifier.then(Modifier.fillMaxWidth().height(10.dp))
  2. Row(modifier = modifier.fillMaxWidth().height(10.dp)

As I understand it, the only drawback of doing it the "2" way, is that we can only concatenate internal modifiers to the passed ones, not the other way around. Am I missing something?

1 Answers

In your example above it's not strictly required.

As @Rafsanjani mentioned in comments it lets you create conditional modifiers.

Or change order of modifiers in situations when you don't want modifier from user to be in first order since order of Modifiers matter for padding, drawing behind or front, or borders, size modifiers use initial one if not forced with Modifier.required.

And order of touch modifiers with padding or graphicsLayer determines clickable area bounds or shape. Modifier.then gives opportunity to customize order and give chance to create conditional modifiers.

Edit

You can use it to swap order of Modifiers but you shouldn't do it with modifiers that are exposed in public Composable functions. However this is an approach you can use if you know internally.
Modifier.myModifier().then(modifier)
Related