Can someone help me confirm this bug (on Android, ...
# compose
m
Can someone help me confirm this bug (on Android, at least)? Using a
requiredSize
that's greater than the parent unexpectedly causes the child to be centered. If the
Box
is smaller, it's placed at the top left. But if it's greater, it's centered (i.e., you can't see the yellow anymore). See the pictures.
Copy code
@Preview("Test", device = "spec:width=500dp,height=500dp,dpi=240")
@Composable
private fun ReproPreview() {
    val colorStops = arrayOf(
        0.0f to Color.Yellow,
        0.2f to Color.Red,
        1f to Color.Blue
    )

    BoxWithConstraints(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.TopStart
    ) {
        Box(
            modifier = Modifier
                .background(Brush.horizontalGradient(colorStops = colorStops))
                .requiredSize(height = maxHeight * 2, width = maxWidth * 2)
                // Also happens with `offset`
                .absoluteOffset(0.dp, 0.dp)
        )
    }
}
s
That's expected behavior, it centers if you go out of bounds. If you want to control what happens instead do
wrapContentSize()
and provide the alignment you want in there.
m
Ohhh, I had tried that but was putting
wrapContentSize
after the
requiredSize
, which caused something weird. This behavior caught me off-guard, though -- should it be the default?
Appreciate the quick response!
I understand that changing this would break a lot of things, though.
s
Yeah definitely, I don't see this changing ever. It is a decent enough default anyway I think. But perhaps I am saying this only after the fact that I learned about it. Not very unbiased 😄
k