0

How can I clip the content to match the corner of the Card.

@Composable
fun PreviewWindow() {
    Card {
        Column(
            modifier = Modifier
                .padding(2.dp)
                .background(Color.Blue)
                .width(200.dp)
                .aspectRatio(1F)
        ) { }
    }
}

Preview of the code

Look at the corners of the Card. I want them to clip at the padding. The CardDefalts has shape but I can't get corner radius.Thus it is not very dynamic.

I know that I can set custom shape to achieve this. But I want to know any dynamic solution.

1 Answer 1

1

The issue occurs because Card has its own default shape CardDefaults.shape. To ensure that the Column inside it respects this shape, you need to clip it accordingly.

Here’s how you can do it:

@Composable
fun PreviewWindow() {
    Card {
        Column(
            modifier = Modifier
                .padding(2.dp)
                .clip(CardDefaults.shape) // Clip the Column to match the Card's shape
                .background(Color.Blue)
                .width(200.dp)
                .aspectRatio(1F)
        ) { }
    }
}

By applying .clip(CardDefaults.shape), the Column will take on the same rounded shape as the Card, ensuring proper clipping.

Output Image

Sign up to request clarification or add additional context in comments.

1 Comment

But the inner content Radius should account for the padding. Meaning its radius should equal to Card corner radius - padding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.