让我们看看一个简单的Material Design 3的Card以及它在Compose中的elevation是如何工作的。
首先,什么构成了一个简单的Card?
- 一个简单的Card只是一个内部有Column的Surface。
- 一个ElevatedCard只是一个具有更高elevation默认值的Card。
其次,一个Card的elevation由什么构成?
- 首先,是其底层Surface的tonalElevation。
- 其次,是其底层Surface的shadowElevation。
如果你想要一个带有shadowElevation但没有tonalElevation的简单Material Design 3的Card,你可以像这样自己制作一个Surface和Column的组合:
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.graphics.*
import androidx.compose.ui.unit.*
@Composable
fun ShadyCard(
modifier: Modifier = Modifier,
shape: Shape = CardDefaults.elevatedShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
shadowElevation: Dp = 1.0.dp,
border: BorderStroke? = null,
content: @Composable ColumnScope.() -> Unit,
) {
Surface(
modifier = modifier,
shape = shape,
color = color,
contentColor = contentColor,
shadowElevation = shadowElevation,
border = border,
) {
Column(content = content)
}
}
如果需要点击
Card
,则需要添加交互处理,因为根据
Material Design 3规范,不同的卡片状态可能具有不同的高度级别。
(交互处理不在我的回答范围内。)