I wanted to know how we can add multiple modifier, for example adding background, padding and more to an android jetpack composable?
Jetpack Compose - How can add multiple modifier to a composable and is the order important?
Asked Answered
It's really simple; You can chain multiple modifiers.
Column(modifier = Modifier.preferredHeight(500.dp).padding(100.dp)) {
Text("Hello") }
And The order is important; Modifier elements to the left are applied before modifier elements to the right.
@Fredella Column(modifier = Modifier.padding(24.dp).fillMaxWidth()) { Text(text = "hello") Text(text = name) } –
Gubernatorial
if you have to use two different modifier, you can simply use .then() function to add second modifier
For Example, let's say you have a composable function in which we have passed a modifier as a parameter and inside that function we have a box which already have a modifier. so by using then modifier you can add the modifier which have been passed as a parameter
@Composable
fun myView(modifier: Modifier) {
Box(
modifier = Modifier
.padding(8.dp)
.background(Color.Black)
.then(modifier)
)
}
and yes order your in which you are adding modifiers matters
© 2022 - 2024 — McMap. All rights reserved.
fillMaxSize
withpadding
? – Fredella