https://kotlinlang.org logo
#compose
Title
# compose
l

Leland Richardson [G]

05/25/2020, 2:29 PM
Basically, Box will add the padding modifier for you, and it won't be the "left most" modifier in the chain, which means it can't be emulated exactly by applying the padding modifier yourself
k

Kazemihabib1996

05/26/2020, 2:40 PM
Yeah, that's exactly the problem. The Box is nothing more than a Column, it just accepts some padding, background, border and etc parameters and applies some
modifiers
based on those parameters. But every time I want to use these parameters I have to check the source code to know the order of the applied modifiers. So why should we bother ourselves when we can easily do the same with modifiers with full control over the order of
modifiers
.
Copy code
@Composable
fun PaddingModifier() {
    Stack(Modifier.drawBackground(Color.Gray)) {
        Box(
            Modifier
                .padding(start = 20.dp, top = 30.dp, end = 20.dp, bottom = 30.dp)
                .preferredSize(200.dp)
                .drawBorder(Border(10.dp, Color.Green))
                .drawBackground(Color.Blue)
        )
    }
}

@Composable
fun PaddingModifier3() {
    Stack(Modifier.drawBackground(Color.Gray)) {
        Box(
            Modifier
                .padding(start = 20.dp, top = 30.dp, end = 20.dp, bottom = 30.dp)
                .preferredSize(200.dp)
                .drawBackground(Color.Blue)
        )
    }
}

@Composable
fun PaddingModifier4() {
    Stack(Modifier.drawBackground(Color.Gray)) {
        Box(
            Modifier
                .drawBorder(Border(10.dp, Color.Green))
                .padding(start = 20.dp, top = 30.dp, end = 20.dp, bottom = 30.dp)
                .preferredSize(200.dp)
                .drawBackground(Color.Blue)
        )
    }
}
IMHO those parameters are just making it more complex because we have to check the source code. Specially it makes harder for teaching. When I said
these padding parameters implies that the Box is different from other layout composables.
I mean, it makes harder for new users.
l

Leland Richardson [G]

05/26/2020, 4:28 PM
Thanks, this is really good feedback. We’ve talked at length about Box internally and some points similar to yours have come up. We will keep this in mind. The API is meant as somewhat of a convenience API for things that come up repeatedly and often. We try not to provide too many “convenience APIs” unless we really think it will be high traffic and that everyone is likely to just create their own if we don’t. That said, there is merit to being consistent and hurting early learning opportunities with a convenience API.
👍 1