Pedro Alberto
10/31/2022, 3:41 PMStylianos Gakis
10/31/2022, 3:52 PMPedro Alberto
10/31/2022, 3:54 PMPedro Alberto
10/31/2022, 3:54 PMPedro Alberto
10/31/2022, 3:54 PMPedro Alberto
10/31/2022, 3:55 PMPedro Alberto
10/31/2022, 3:56 PMinternal fun BaseButton(
modifier: Modifier = Modifier,
even thought internally I do apply fix height for the buttonPHondogo
10/31/2022, 3:57 PMPedro Alberto
10/31/2022, 3:59 PMStylianos Gakis
10/31/2022, 4:00 PMPHondogo
10/31/2022, 4:03 PM@Composable
fun FancyButton(
text: String,
onClick: () -> Unit,
modifier: Modifier = Modifier
) = Text(
text = text,
modifier = modifier.surface(elevation = 4.dp)
.clickable(onClick)
.padding(horizontal = 32.dp, vertical = 16.dp)
)
It's example code from that article how to merge argument modifier with internals.Pedro Alberto
10/31/2022, 4:03 PMPedro Alberto
10/31/2022, 4:07 PMpadding(horizontal = 32.dp, vertical = 16.dp)
so if you use
FancyButton
is not clear that if use
FancyButton(modifier = Modifier.padding(0.dp))
nothing happens because internally you are overridingPedro Alberto
10/31/2022, 4:10 PMPHondogo
10/31/2022, 4:16 PMChris Sinco [G]
11/01/2022, 5:18 AMColton Idle
11/01/2022, 8:18 PMStylianos Gakis
11/01/2022, 8:29 PMrequiredSize
do that? Lemme test actually 👀Stylianos Gakis
11/01/2022, 8:41 PMsize
requiredSize
and the such work and interact with each other 🤯Stylianos Gakis
11/01/2022, 10:27 PM@Composable
fun BoxScope.SizingItems() {
Box(Modifier.width(100.dp).height(5.dp).background(Color.Red))
Box(Modifier.width(75.dp).height(10.dp).background(Color.Yellow))
Box(Modifier.width(50.dp).height(15.dp).background(Color.Green))
Box(Modifier.height(100.dp).width(5.dp).background(Color.Red))
Box(Modifier.height(75.dp).width(10.dp).background(Color.Yellow))
Box(Modifier.height(50.dp).width(15.dp).background(Color.Green))
}
And then the code:
@Composable
fun ModifiersCheck() {
Row(
horizontalArrangement = Arrangement.spacedBy(40.dp),
modifier = Modifier.padding(10.dp).border(1.dp, Color.Red).padding(50.dp)
) {
Column(verticalArrangement = Arrangement.spacedBy(25.dp)) {
Text(
"Green = 50.dp\nYellow = 75.dp\nRed = 100.dp"
)
Box {
SizingItems()
}
}
Column(verticalArrangement = Arrangement.spacedBy(40.dp)) {
Text("#1:size(50.dp)\n#2:size(100.dp))")
Box {
Box(Modifier.size(50.dp).size(100.dp).background(Color.Blue))
SizingItems()
}
}
Column(verticalArrangement = Arrangement.spacedBy(40.dp)) {
Text("#1:size(50.dp)\n#2:requiredSize(100.dp))")
Box {
Box(Modifier.size(50.dp).requiredSize(100.dp).background(Color.Blue))
SizingItems()
}
}
Column(verticalArrangement = Arrangement.spacedBy(40.dp)) {
Text("#1:requiredSize(50.dp)\n#2:requiredSize(100.dp))")
Box {
Box(Modifier.requiredSize(50.dp).requiredSize(100.dp).background(Color.Blue))
SizingItems()
}
}
Column(verticalArrangement = Arrangement.spacedBy(40.dp)) {
Text("#1:requiredSize(100.dp)\n#2:requiredSize(50.dp))")
Box {
Box(Modifier.requiredSize(100.dp).requiredSize(50.dp).background(Color.Blue))
SizingItems()
}
}
Column(verticalArrangement = Arrangement.spacedBy(40.dp)) {
Text("#1:requiredSize(50.dp)\n#2:size(100.dp))")
Box {
Box(Modifier.requiredSize(50.dp).size(100.dp).background(Color.Blue))
SizingItems()
}
}
}
}
And then the output in the screenshot:
Some thoughts:
So the latest modifier wins when it comes to using requiredSize
. This does however come with the side effect that the item does measure at your required size, but it might be placed a bit off if the caller decides to be weird and call its own requiredSize
. You can see that in the 4th and the 5th item.
Do not just use size
as the 6th item does in your design system, that allows the caller to use requiredSize
itself and override your size. However using requiredSize
makes it so that items get placed at the center of whatever the call-site decided, but just make your item overflow to the edges as seen in items #3 and #4
I think this tells us that if you want your design system item to ALWAYS be of some size, you can safely use the modifier without doing anything to it, since you’ll then after it use your requiredHeight
which will override whatever comes from the modifier. And this doesn’t require you to do anything out of the ordinary, it is standard practice to use the passed modifier
first, and chain other modifiers after it.
But still quite a pickle, I guess I might be missing something in general, or it’s just that you can’t really do this flawlessly no matter what you chose in your internal component and you’re at the mercy of the call-site. Maybe it’s not actually such a big deal when there’s more room anyway and your item won’t have to measure itself in such a way that it goes “out of bounds” so to say, like #3 and #4, but 🤷♂️Pedro Alberto
11/02/2022, 10:25 AMStylianos Gakis
11/02/2022, 11:02 AMmodifier
parameter in your composables. Especially not in your design system ones which are meant to be reused a lot.
But yeah in general I’m not sure what you meant here.Pedro Alberto
11/02/2022, 1:13 PM@Composable
fun FancyButton(
text: String,
onClick: () -> Unit,
modifier: Modifier = Modifier
) = Text(
text = text,
modifier = modifier.surface(elevation = 4.dp)
.clickable(onClick)
.padding(horizontal = 32.dp, vertical = 16.dp)
)
@Composable
fun TestFancyButtonOverride() {
FancyButton(modifier = Modifier.overridePadding(horizontal = 0.dp, vertical = 0.dp))
}
this way if the caller actually wants to override the Padding or Size or anything else we could. Not sure if this is feasible for the modifier.PHondogo
11/02/2022, 1:19 PMPedro Alberto
11/02/2022, 1:41 PM