Modifiers in Depth
What is a Modifier?
A Modifier decorates or adds behaviour to a composable: size, padding, background, border, click handling, and much more. Almost every composable accepts a modifier parameter, and you chain modifiers together to build up the look.
Text(
"Tap me",
modifier = Modifier
.padding(8.dp)
.background(Color(0xFFF3EEFE), RoundedCornerShape(12.dp))
.padding(12.dp)
.clickable { /* handle tap */ }
)
Order matters!
Modifiers are applied in the order you write them. This is the single most common source of confusion:
// padding THEN background -> the padding is OUTSIDE the colored area
Modifier.padding(16.dp).background(Color.Red)
// background THEN padding -> the color fills first, padding is INSIDE
Modifier.background(Color.Red).padding(16.dp)
Read a modifier chain top-to-bottom as “first do this, then that”.
Common modifiers
fillMaxWidth(),fillMaxSize(),size(48.dp),height(),width()— sizing.padding()— space inside/around.background(),border(),clip()— appearance.clickable { },pointerInput { }— interaction.weight()— share space inside Row/Column.
Reusing modifiers
val cardModifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(16.dp))
.background(MaterialTheme.colorScheme.surfaceVariant)
.padding(16.dp)
Column(modifier = cardModifier) { /* ... */ }
Pass modifiers in, don’t hard-code
A reusable composable should accept a modifier parameter (defaulting to Modifier) and apply it to its root, so callers can position it:
@Composable
fun Avatar(url: String, modifier: Modifier = Modifier) {
AsyncImage(model = url, contentDescription = null,
modifier = modifier.size(48.dp).clip(CircleShape))
}
Common mistakes
- Getting padding/background order wrong (the classic).
- Not exposing a
modifierparameter on reusable composables. - Applying
clickableafterpaddingso the tap area is smaller than expected.
Summary: Modifiers are an ordered chain that controls size, look and behaviour. Order matters — padding before vs after background changes the result. Always expose a modifier parameter on reusable composables.