How can we apply LayoutPadding to all children of ...
# compose
k
How can we apply LayoutPadding to all children of either Column or Row?
a
Copy code
Column {
    val padding = LayoutPadding(...)
    MyChild1(modifier = padding)
    MyChild2(modifier = padding)
    // ...
}
you can reuse modifiers on more than one child
k
Ah thanks 🙂 So there is no way to let the children inherit this?
a
You could write your own version of
Row
or
Column
using the
Layout()
composable, but short of that, no
k
Okay, thanks 🙂 I think the re-use solution will do. Thanks again for helping 🙂
👍 1
a
Out of curiosity, do you have a snippet of your specific code where you wanted this?
k
Oh, I'm just playing around. I have 3 buttons in a Row and I wanted to pad them equally.
Copy code
CenteredRow {
            Button(modifier = LayoutPadding(end = 5.dp), onClick = { navigateTo(Screen.Login) }) {
                Text(text = "Click Me!")
            }
            if (user != null) {
                Button(modifier = LayoutPadding(end = 5.dp), onClick = {
                    GlobalScope.launch {
                        val result = MessageService.send(user.userId, "Hello There!", user)
                        withContext(Dispatchers.Main) {
                            messageHolder.message = result.fold({ it }, { it.messageId })
                        }
                    }
                })
                {
                    Text(text = "Send Test Message")
                }
                Button(onClick = { UserService.logout() }) {
                    Text(text = stringResource(R.string.logout_buton_text))
                }
            }
        }
a
in general we've been working from the principle that applying layout modifications in a sort of scope like the question implied is intrinsically pretty confusing, it was how we initially moved away from a
Padding {}
composable that applies the padding to each child regardless of the parent layout into the explorations that became modifiers
given that code putting the common modifiers into a
val
and reusing is probably your best bet, or having a custom layout for that
CenteredRow
k
Thanks, makes sense that you decided to not apply to children. I will go with the re-using
val
for now 🙂
👍 1