Hello, I have issues trying to use `Crossfade` wit...
# compose
n
Hello, I have issues trying to use
Crossfade
with an elevated
Card
(dropping shadows). Is there any way to avoid the clipping of the Card during the Crossfade animation?
Sample code:
Copy code
var switch by remember { mutableStateOf(false) }

LaunchedEffect(Unit) {
    while (true) {
        delay(2.seconds)

        switch = !switch
    }
}

Crossfade(
    modifier = Modifier.padding(vertical = 16.dp),
    targetState = switch,
    animationSpec = tween(1_500),
) { state ->
    if (state) {
        Card(
            modifier = Modifier.padding(horizontal = 16.dp),
            elevation = CardDefaults.cardElevation(
                defaultElevation = 8.dp,
            ),
        ) {
            Text(
                modifier = Modifier.padding(16.dp),
                text = "One",
            )
        }
    } else {
        Card(
            modifier = Modifier.padding(horizontal = 16.dp),
            elevation = CardDefaults.cardElevation(
                defaultElevation = 8.dp,
            ),
        ) {
            Text(
                modifier = Modifier.padding(16.dp),
                text = "Two",
            )
        }
    }
}
The obvious fix would be to set the vertical padding to the
Card
itself insead of the Crossfade. Unfortunately, I’m working with a design system and the component “above” the Card in the List has some padding and I can’t modify it. Is there any hope ?
z
Ive ran into this issue a lot too. I think its a bug where animations clip to the bounds of the composables, whereas the shadows draw outside of that. My workaround is basically Modifier.padding(12.dp) and a custom layout that retracts the added padding.
Copy code
Box(
            modifier = Modifier.padding(ShadowRoom),
            propagateMinConstraints = true,
            content = { content() },
        )
modifier.overhang:
Copy code
layout { measurable, constraints ->
            val room = ShadowRoom.roundToPx()
            val placeable = measurable.measure(constraints.offset(room * 2, room * 2))
            val width = (placeable.width - room * 2).coerceAtLeast(0)
            val height = (placeable.height - room * 2).coerceAtLeast(0)

            layout(width, height) {
                placeable.place(-room, -room)
            }
Copy code
AnimatedContent(
    modifier = modifier.overhang(definition),
    ...
    content = { currentState -> Room(definition) { content(currentState) } },
)