julioromano
09/24/2021, 1:15 PMjulioromano
09/24/2021, 1:26 PM@Composable
fun SomethingNice(
modifier: Modifier = Modifier,
) {
Box(
modifier = modifier.size(24.dp),
) {
// Something more...
}
}
It is working.
But I was afraid the “default” 24dp modifier would be called after the one passed in via parameter therefore always overriding it. Turns out this is not true. But can’t understand why.Zoltan Demant
09/24/2021, 1:28 PMModifier.defaultMinSize()
Adam Powell
09/24/2021, 1:59 PMmin = max = myRequestedSize.coerceIn(parentSuppliedMin, parentSuppliedMax)
Adam Powell
09/24/2021, 2:00 PMAdam Powell
09/24/2021, 2:01 PMrequiredSize
modifiers override parent constraintsjulioromano
09/24/2021, 2:04 PMSomethingNice()
It gets drawn at 24dp as expected.
And if I draw this:
SomethingNice(modifier = Modifier.size(64.dp))
It gets drawn at 64dp. But it is strange to me as in the code it will happen something like:
@Composable
fun SomethingNice() {
Box(
modifier = Modifier.size(64.dp).size(24.dp),
) {
// Something more...
}
}
Shouldn’t the second call to .size(24.dp)
override the first call to .size(64.dp)
?Adam Powell
09/24/2021, 2:28 PM24.coerceIn(64, 64) = 64
Adam Powell
09/24/2021, 2:29 PMsize(64.dp)
so it's coerced into the constraints of the upstream parentjulioromano
09/24/2021, 2:31 PMModifier.size(12.dp)
)
In this case we’d have:
24.coerceIn(12, 12) = ?
julioromano
09/24/2021, 2:32 PMSomethingNice
it is displayed with 12dp sizejulioromano
09/24/2021, 2:40 PMjulioromano
09/24/2021, 3:05 PMjulioromano
09/24/2021, 3:26 PMmodifier = modifier.size(24.dp)
with modifier = Modifier.size(24.dp).then(modifier)
Solve any issues with sizing I had 🤷Scott Kruse
09/24/2021, 3:52 PMjulioromano
09/24/2021, 3:53 PMAdam Powell
09/24/2021, 6:51 PMAdam Powell
09/24/2021, 7:13 PMAdam Powell
09/24/2021, 7:18 PMmin
and max
size; how big are you?" Modifier.size
narrows those min and max constraints to be as close to the requested value as possible, within the constraints of how it was measured by its immediate parent (whether that parent is another layout modifier or a layout composable)Adam Powell
09/24/2021, 7:21 PM@Composable fun DefaultSizeBox(
modifier: Modifier = Modifier,
content: @Composable BoxScope.() -> Unit
) = Box(modifier.size(24.dp), content = content)
and then call it like this:
DefaultSizeBox(Modifier.size(64.dp)) {
Text("Hello, world")
}
then the final modifier for the Box
created by DefaultSizeBox
is Modifier.size(64.dp).size(24.dp)
.
Measurement then proceeds; let's say the parent of the DefaultSizeBox
measures it with the constraints (min = 0, max = 500)
(simplified to only one dimension here for the sake of this discussion; both width and height each have a min and max)Adam Powell
09/24/2021, 7:21 PM.size(64.dp)
. 64 is between 0 and 500, so now the measurement proceeds, but with (min = 64, max = 64)
Adam Powell
09/24/2021, 7:24 PM.size(24.dp)
. 24 is not between 64 and 64; in order to be within that range the constraints are "changed" to (min = 64, max = 64)
. The final box is measured with those constraints, and reports that it is size 64. The "outer" modifier passed to the DefaultSizeBox
call thereby overrode the requested default defined in `DefaultSizeBox`'s implementation.julioromano
09/27/2021, 9:23 AMRafiul Islam
09/28/2021, 11:49 AM.size(24.dp)
. 24 is not between 64 and 64; in order to be within that range the constraints are "changed" to (min = 64, max = 64)
But what happened in this case? 24.coerceIn(12, 12) = ?julioromano
09/28/2021, 11:53 AM= 12
Rafiul Islam
09/28/2021, 11:56 AM