wintersoldier
01/19/2023, 7:12 AMBox(
modifier = Modifier
.background(Color.Yellow)
.shadow(
elevation = 2.dp,
),
) {
content()
}
Oleksandr Balan
01/19/2023, 7:38 AMdrawWithContent
modifier to clip the content as you need. For example recently I create a custom modifier which is uses drawWithContent
inside:
fun Modifier.clipSides(
left: Boolean = true,
top: Boolean = true,
right: Boolean = true,
bottom: Boolean = true
): Modifier = drawWithContent {
clipRect(
left = if (left) 0f else -Float.MAX_VALUE,
top = if (top) 0f else -Float.MAX_VALUE,
right = if (right) size.width else Float.MAX_VALUE,
bottom = if (bottom) size.height else Float.MAX_VALUE,
) {
this@drawWithContent.drawContent()
}
}
Thus you can use it on your Box to clip only sides you need:
Box(
modifier = Modifier
.background(Color.Yellow)
.shadow(elevation = 2.dp)
.clipSides(top = false, bottom = false),
) {
content()
}
wintersoldier
01/19/2023, 9:04 AMBox(
modifier = Modifier
.background(Color.Yellow)
.shadow(elevation = 2.dp)
.clipSides(top = false, bottom = false),
) {
content()
}
I have used this code exactlyOleksandr Balan
01/19/2023, 9:29 AMSurface
:
Surface(
shadowElevation = 8.dp,
color = Color.Yellow,
modifier = Modifier.clipSides(top = false, bottom = false),
) {
Button(
onClick = { /* Empty */ },
modifier = Modifier.padding(16.dp)
) {
Text(text = "Test")
}
}
wintersoldier
01/19/2023, 10:32 AMOleksandr Balan
01/19/2023, 10:58 AMRow(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterHorizontally),
modifier = Modifier.fillMaxSize()
) {
Surface(
shadowElevation = 2.dp,
color = Color.Yellow,
) {
Button(
onClick = { /* Empty */ },
modifier = Modifier.padding(16.dp)
) {
Text(text = "Test")
}
}
Box(
modifier = Modifier
.shadow(2.dp)
.background(Color.Yellow)
) {
Button(
onClick = { /* Empty */ },
modifier = Modifier.padding(16.dp)
) {
Text(text = "Test")
}
}
}
wintersoldier
01/19/2023, 11:05 AMSurface
can't find a param called shadowElevation
like you shared above. All I can find is elevation
.
Will that be the issue😕, other than that Its exactly the same code I have writtenOleksandr Balan
01/19/2023, 11:15 AMSurface
comes from Material3 library, yours if from Material2. Just tried it with M2 Surface
and it looks same to me 😅wintersoldier
01/19/2023, 11:26 AMOleksandr Balan
01/19/2023, 11:30 AMBox(
modifier = Modifier
.background(...)
.shadow(elevation = 2.dp)
)
or
Box(
modifier = Modifier
.shadow(elevation = 2.dp)
.background(...)
)
?
The last should behave as Surface
, because Surface
is literally a Box 😄wintersoldier
01/19/2023, 11:54 AM