I have the below code
LazyColumn(
contentPadding = PaddingValues(all = 64.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
item {
Box(
modifier = Modifier
.width(200.dp)
.height(200.dp)
.border(BorderStroke(2.dp, Color.Green))
) {
Box(modifier = Modifier
.fillMaxSize()
.sizeIn(100.dp, 200.dp)
.requiredWidthIn(150.dp, 180.dp)
.requiredHeightIn(150.dp, 180.dp)
.border(BorderStroke(3.dp, Color.Red))
)
}
}
}
It resulted as expected.
I just replaced requiredWidthIn
and requiredHeigthIn
with requiredSizeIn
as below.
LazyColumn(
contentPadding = PaddingValues(all = 64.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
item {
Box(
modifier = Modifier
.width(200.dp)
.height(200.dp)
.border(BorderStroke(2.dp, Color.Green))
) {
Box(modifier = Modifier
.fillMaxSize()
.sizeIn(100.dp, 200.dp)
.requiredSizeIn(150.dp, 180.dp)
.border(BorderStroke(3.dp, Color.Red))
)
}
}
I expect the result to be the same. Somehow it looks different on the height side. Why is this?
If you look at the definitions of each method you are using, you see:
requiredWidthIn(min: Dp, max: Dp)
requiredHeightIn(min: Dp, max: Dp)
requiredSizeIn(minWidth: Dp, minHeight: Dp, maxWidth: Dp, maxHeight: Dp)
So really your second code snippet is saying:
Which is not the same thing - you haven't declared any
maxWidth
ormaxHeight
like you did when usingrequiredWidthIn
andrequiredHeightIn
. That's why you are getting a different result - you're setting aminHeight
of180.dp
when usingrequiredSizeIn
while you're using aminHeight
of150.dp
when you are usingrequiredHeightIn
.You'd see the same result if you declare all four parameters to
requiredSizeIn
: