There is nothing wrong in your example.
When the child Constraints
do not match the one it receives from parent it's bounded in that Constraints
unless you use Modifier.wrapContentX(unbounded=true/false
) or Modifier.requiredX
modifier. X can be width/height/size etc.
In your example minWidth/Height and maxWidth/Height in parent Constraints
will be similar to what i have on my device in pixels(dp * density)
I Parent Constraints(minWidth = 165, maxWidth = 165, minHeight = 165, maxHeight = 165)
I Top Constraints(minWidth = 83, maxWidth = 83, minHeight = 83, maxHeight = 83)
I Bottom Constraints(minWidth = 165, maxWidth = 165, minHeight = 165, maxHeight = 165)
I replaced each Box
with BoxWithConstraints
to log Constraints
.
When you assign a Modifier.size() both min and max width or height are equal and they should be in parent's Constraints
bounds.
In bottom Box since 200.dp is out of parent's 60.dp size, it's min and max dimensions are limited to 60.dp in pixels. With default Composables and without Modifier.wrapContentX
or Modifier.requiredX
you cannot force 200.dp size to child Composable while parent is 60.dp
And why blue Box is on top is due to how Box places items. the last Placeable
or Composable is drawn on top of others unless any Modifier.zIndex is assigned.
You can check out Constraints section of this answer for more detail about Constraints
ranges.
Also you can refer this official medium article for more details about Constraints and Modifiers.
https://medium.com/androiddevelopers/constraints-and-modifier-order-a3912461ecd6