How to align a bigger child within a smaller parent in Jetpack Compose?

38 Views Asked by At

I have created a Box with a fixed size and it has a child that doesn't respect the parent's constraints. The child uses a requiredSize modifier to do this.

Box(Modifier
    .size(50.dp)
    .border(2.dp, Color.Red)
) {
    Box(Modifier
       .requiredSize(100.dp)
       .background(Color.Yellow)) {}
}

By default, the Yellow child is aligned at the center position within the parent container.

enter image description here

I want to align it differently (say Alignment.BottomCenter) as shown below:

enter image description here

The documentation website says that I can apply wrapContentSize modifier to the child to do this. Here is the snippet from the documentation which I am referring to.

enter image description here

When I do as mentioned in the documentation, it does nothing.

Box(Modifier.size(50.dp).border(2.dp, Color.Red)) {
    Box(Modifier
            .requiredSize(100.dp)
            .background(Color.Yellow)
            .wrapContentSize(Alignment.BottomCenter)
    ) {}
}

Am I doing something wrong? How do I achieve the desired result?

0

There are 0 best solutions below