I currently have the following composable: ```@Co...
# compose
e
I currently have the following composable:
Copy code
@Composable
fun ColorPicker(
  modifier: Modifier = Modifier
) {
  BoxWithConstraints(
    modifier = modifier.aspectRatio(1F)
  ) {
    val diameter = constraints.maxWidth
    ...
  }
}
The problem is that it looks like if a user passes a
Modifier
to
ColorPicker
with size constraints, it would follow those. However,
BoxWithConstraints
uses the incoming constraints to size itself, not the constraints in its
Modifier
. In a case like this, should I be wrapping
BoxWithConstraints
in a
Box
that uses the
Modifier
parameter, or is it expected that the caller of
ColorPicker
should wrap it in whatever layout it needs, and set the size constraints appropriately?
z
The modifier passed to a composable should, if it follows best practices, affect the constraints that that composable sees. If BoxWithConstraints isn’t doing that, it’s probably a bug
e
Looks like I have to use
requiredSize
because of a
fillMaxSize
somewhere higher in the hierarchy. Guess I need to re-read how layout and placement works 😅
@Zach Klippenstein (he/him) [MOD] any good blog posts / videos on how all the different size modifiers and measurement constraints work? This issue kind of goes against my mental model of how it should work.
Copy code
Surface(
      modifier = Modifier.fillMaxSize() // bigger than 250.dp
) {
      Box(
        modifier = Modifier.size(250.dp).background(Color.Yellow)
      )
}
This results in the
Box
taking up the full size of the
Surface
. I have to use
requiredSize
to get the behavior I expect. But it seems weird to me that the incoming constraints override
size
.
d
Looking at the code for Surface it's actually just a Box with propagateMinConstraints set to true.
z
size actually used to be defaultSize or something, and requiredSize was just called size. I don’t remember why they changed. In this case, i would probably use wrapContentSize before your size modifier to be able to control alignment.
e
Any chance of that ever getting reverted? I looked at the source, and based on how it works, I think it would make more sense to be called
preferredSize
(which is actually what the kdoc calls it).
z
These are stable apis now and this decision was made very intentionally. It’s not going back, for better or worse
e
Any insight into what the factors leading to the decision were?