Search code examples
androidkotlinandroid-jetpack-compose

Remove unwanted shadow when cards are placed side by side in Jetpack Compose


I've put together a custom shape using two cards that are placed side by side in Jetpack Compose. I want the shape to be elevated, i.e., have a drop shadow. If I elevate the two cards, both cards get shadows as if they're a single shape.

Unwanted shadow marked with arrow

Example code:

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBackIosNew
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex

@Composable
fun MenuExample ()
{
    Row(
        verticalAlignment = Alignment.CenterVertically
    ) {
        Card(
            elevation = CardDefaults.cardElevation(
                defaultElevation = 10.dp
            ),
            modifier = Modifier
                .padding(0.dp)
                .height(80.dp)
                .zIndex(1f)
                .width(40.dp),
            shape = RoundedCornerShape(topStart = 10.dp, bottomStart = 10.dp),
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.98f)
            )
        ) {

            Button(
                onClick = { },
                modifier = Modifier
                    .padding(0.dp)
                    .height(80.dp)
                    .width(40.dp),
                shape = RectangleShape,

                contentPadding = PaddingValues(0.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.Transparent,
                    contentColor = Color.Black
                )
            ) {
                Icon(
                    imageVector = Icons.Filled.ArrowBackIosNew,
                    contentDescription = null,
                    modifier = Modifier
                        .size(22.dp)
                        .padding(0.dp)
                )
            }
        }
        Card(
            elevation = CardDefaults.cardElevation(
                defaultElevation = 10.dp,
            ),
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.98f)
            ),
            modifier = Modifier
                .padding(0.dp)
                .width(200.dp)
                .height(200.dp)
                .zIndex(0.5f)
        ) {
        }
    }
}

@Preview
@Composable
fun MenuExamplePreview() {
    MenuExample()
}

I'd like the elevation to treat the two cards as one, meaning that the overlaping area shound not have a drop shadow. How can i remove the unvanded shadow between the two cards (marked with an arrow)?


Solution

  • I don't know if there is another easier way to achieve this, but one one that i came up with, is to create a custom shape for the card and treating them as a single card.

    Make sure to use the latest version of compose to be able to use quadraticTo method

    import androidx.compose.foundation.BorderStroke
    import androidx.compose.foundation.layout.Box
    import androidx.compose.foundation.layout.BoxScope
    import androidx.compose.foundation.layout.Column
    import androidx.compose.foundation.layout.ColumnScope
    import androidx.compose.foundation.layout.PaddingValues
    import androidx.compose.foundation.layout.Row
    import androidx.compose.foundation.layout.fillMaxHeight
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.foundation.layout.size
    import androidx.compose.foundation.layout.width
    import androidx.compose.foundation.shape.GenericShape
    import androidx.compose.material.icons.Icons
    import androidx.compose.material.icons.filled.ArrowBackIosNew
    import androidx.compose.material3.Button
    import androidx.compose.material3.ButtonDefaults
    import androidx.compose.material3.CardColors
    import androidx.compose.material3.CardDefaults
    import androidx.compose.material3.Icon
    import androidx.compose.material3.MaterialTheme
    import androidx.compose.material3.Surface
    import androidx.compose.runtime.Composable
    import androidx.compose.runtime.remember
    import androidx.compose.ui.Alignment
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.geometry.Size
    import androidx.compose.ui.graphics.Color
    import androidx.compose.ui.graphics.RectangleShape
    import androidx.compose.ui.platform.LocalDensity
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.Dp
    import androidx.compose.ui.unit.DpSize
    import androidx.compose.ui.unit.dp
    
    
    @Composable
    fun CustomCard(
        anchorSize : DpSize,
        modifier: Modifier = Modifier,
        roundedCornerSize : Dp = 0.dp,
        elevation: Dp = 0.dp,
        border: BorderStroke? = null,
        colors: CardColors = CardDefaults.cardColors(),
        anchorContent : @Composable BoxScope.() -> Unit = {},
        content : @Composable ColumnScope.() -> Unit = {}
    ) {
        val anchorSizePx = with(LocalDensity.current) { Size(anchorSize.width.toPx(), anchorSize.height.toPx()) }
        val roundedCornerSizePx = with(LocalDensity.current) { roundedCornerSize.toPx() }
    
        val customShape = remember {
            GenericShape { size, _ ->
                val width = size.width
                val height = size.height
    
                val a = (height - anchorSizePx.height) / 2
    
                moveTo(0f, a + roundedCornerSizePx)
    
                quadraticTo(0f, a, roundedCornerSizePx, a)
                lineTo(anchorSizePx.width, a)
                lineTo(anchorSizePx.width, roundedCornerSizePx)
                quadraticTo(anchorSizePx.width, 0f, anchorSizePx.width + roundedCornerSizePx, 0f)
                lineTo(width - roundedCornerSizePx, 0f)
                quadraticTo(width, 0f, width, roundedCornerSizePx)
                lineTo(width, height - roundedCornerSizePx)
                quadraticTo(width, height, width - roundedCornerSizePx, height)
                lineTo(anchorSizePx.width + roundedCornerSizePx, height)
                quadraticTo(anchorSizePx.width, height, anchorSizePx.width, height - roundedCornerSizePx)
                lineTo(anchorSizePx.width, height - a)
                lineTo(roundedCornerSizePx, height - a)
                quadraticTo(0f, height - a, 0f, height - a - roundedCornerSizePx)
            }
        }
    
        Surface(
            modifier = modifier,
            shape = customShape,
            color = colors.containerColor,
            contentColor = colors.contentColor,
            shadowElevation = elevation,
            border = border,
        ) {
            Row{
                Box(
                    modifier = Modifier
                        .fillMaxHeight()
                        .width(anchorSize.width),
                    contentAlignment = Alignment.Center,
                ) {
                    Box(
                        modifier = Modifier
                            .size(anchorSize),
                        content = anchorContent
                    )
                }
                Column(content = content)
            }
        }
    }
    
    
    @Preview(showBackground = false)
    @Composable
    fun CardExamplePreview() {
        CustomCard(
            anchorSize = DpSize(40.dp, 80.dp),
            roundedCornerSize = 10.dp,
            elevation = 10.dp,
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.98f)
            ),
            modifier = Modifier
                .size(200.dp),
    
            anchorContent = {
                Button(
                    onClick = { },
                    modifier = Modifier
                        .fillMaxSize(),
    
                    shape = RectangleShape,
    
                    contentPadding = PaddingValues(0.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color.Transparent,
                        contentColor = Color.Black
                    )
                ) {
                    Icon(
                        imageVector = Icons.Filled.ArrowBackIosNew,
                        contentDescription = null,
                    )
                }
            },
            content = {
                // Panel content goes here
            }
        )
    }
    

    Result:

    Result


    Edit

    Another option is to use graphics-shapes library, as pointed by @Kolyneh :

    implementation("androidx.graphics:graphics-shapes:1.0.1")
    
    

    Code:

    class CustomShape(
        private val anchorSize : Size,
        private val roundedCornerSize : Float
    ) : Shape {
        override fun createOutline(
            size: Size,
            layoutDirection: LayoutDirection,
            density: Density
        ): Outline {
    
            val width = size.width
            val height = size.height
    
            val anchorWidth = anchorSize.width
            val anchorHeight = anchorSize.height
    
            val a = (height - anchorHeight) / 2
    
            val vertices = floatArrayOf(
                0f, a,
                anchorWidth, a,
                anchorWidth, 0f,
                width, 0f,
                width, height,
                anchorWidth, height,
                anchorWidth, height - a,
                0f, height - a
            )
    
            val rounding = listOf(
                CornerRounding(roundedCornerSize),
                CornerRounding(0f),
                CornerRounding(roundedCornerSize),
                CornerRounding(roundedCornerSize),
                CornerRounding(roundedCornerSize),
                CornerRounding(roundedCornerSize),
                CornerRounding(0f),
                CornerRounding(roundedCornerSize),
            )
    
            val polygon = RoundedPolygon(
                vertices = vertices,
                perVertexRounding = rounding
            )
    
            val path = polygon.toPath().asComposePath()
    
            return Outline.Generic(path)
        }
    }
    
    @Composable
    fun CustomCard(
        anchorSize : DpSize,
        modifier: Modifier = Modifier,
        roundedCornerSize : Float = 0f,
        elevation: Dp = 0.dp,
        border: BorderStroke? = null,
        colors: CardColors = CardDefaults.cardColors(),
        anchorContent : @Composable BoxScope.() -> Unit = {},
        content : @Composable ColumnScope.() -> Unit = {}
    ) {
        val anchorSizePx = with(LocalDensity.current) { Size(anchorSize.width.toPx(), anchorSize.height.toPx()) }
    
        val customShape = remember(anchorSizePx, roundedCornerSize) {
            CustomShape(anchorSizePx, roundedCornerSize)
        }
    
        Surface(
            modifier = modifier,
            shape = customShape,
            color = colors.containerColor,
            contentColor = colors.contentColor,
            shadowElevation = elevation,
            border = border,
        ) {
            Row{
                Box(
                    modifier = Modifier
                        .fillMaxHeight()
                        .width(anchorSize.width),
                    contentAlignment = Alignment.Center,
                ) {
                    Box(
                        modifier = Modifier
                            .size(anchorSize),
                        content = anchorContent
                    )
                }
                Column(content = content)
            }
        }
    }
    
    
    @Preview(showBackground = false)
    @Composable
    fun CardExamplePreview() {
        CustomCard(
            anchorSize = DpSize(40.dp, 80.dp),
            roundedCornerSize = 20f,
            elevation = 10.dp,
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.98f)
            ),
            modifier = Modifier
                .size(200.dp),
    
            anchorContent = {
                Button(
                    onClick = { },
                    modifier = Modifier
                        .fillMaxSize(),
    
                    shape = RectangleShape,
    
                    contentPadding = PaddingValues(0.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color.Transparent,
                        contentColor = Color.Black
                    )
                ) {
                    Icon(
                        imageVector = Icons.Filled.ArrowBackIosNew,
                        contentDescription = null,
                    )
                }
            },
            content = {
                // Panel content goes here
            }
        )
    }