When using `Card` with a `backgroundColor` that ha...
# compose
j
When using
Card
with a
backgroundColor
that has an alpha value (i.e. not 00 and not FF) I’m seeing visual artefacts in the form of a border around the card. See attached image (code in 🧵), is this working as intended or is this a bug?
1
Copy code
@Composable
fun CardColorAlphaGlitch() {
    Column(
        modifier = Modifier
            .background(Color(0xFFFFFFFF))
            .padding(16.dp)
            .width(200.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Card(backgroundColor = Color(0x80283593)) {
            Text(
                text = "This card has a background with alpha",
                modifier = Modifier.padding(16.dp)
            )
        }
        Card(backgroundColor = Color(0xFF283593)) {
            Text(
                text = "This card has a background without alpha",
                modifier = Modifier.padding(16.dp)
            )
        }
    }
}
l
This is because of the elevation that
Card
has by default, if you remove the elevation this won't happen
t
Yes unfortunatley the shadow is only drawn at the boarder of the shape. Not sure if this is a bug or a feature.
j
But the point of using a card was because of elevation and shadow.
t
Maybe there should be some kind of clipping that the shadow is not drawn under the shape.
g
Shouldn't the shadow cast by something semitransparent also be semitransparent?
a
This is how shadows are drawn by RenderNodes unfortunately. The same would happen if you set elevation and semitransparent background for a View. There is a possible workaround to convert semitransparent color to the non transparent one if you know the intended background below the Card. for example in this case you can do
backgroundColor = Color(0xFF283593).compositeOver(Color.White)
🙌 1
🙏 1
a
better approach:
Copy code
backgroundColor = Color(0xFF283593).compositeOver(MaterialTheme.colors.surface)
SO it composites over white or black, or whatever your current surface color is
🙏 1
🙌 1