[英]Adjust Tonal Elevation for Material Design 3 Components in Jetpack Compose?
Is it possible to adjust the tonalElevation (but not the shadowElevation) of Material Design 3 components?是否可以调整 Material Design 3 组件的 tonalElevation(但不是 shadowElevation)?
It looks as though it's only possible to adjust both.看起来好像只能同时调整两者。 Below is the implementation of a Floating Action Button in Material Design 3. The same problem exists with other components.下面是 Material Design 3 中浮动操作按钮的实现。其他组件也存在同样的问题。
@Composable
fun FloatingActionButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = FabPrimaryTokens.ContainerShape,
containerColor: Color = FabPrimaryTokens.ContainerColor.toColor(),
contentColor: Color = contentColorFor(containerColor),
elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(),
content: @Composable () -> Unit,
) {
Surface(
onClick = onClick,
modifier = modifier,
shape = shape,
color = containerColor,
contentColor = contentColor,
tonalElevation = elevation.tonalElevation(interactionSource = interactionSource).value,
shadowElevation = elevation.shadowElevation(interactionSource = interactionSource).value,
interactionSource = interactionSource,
) {
CompositionLocalProvider(LocalContentColor provides contentColor) {
// Adding the text style from [ExtendedFloatingActionButton] to all FAB variations. In
// the majority of cases this will have no impact, because icons are expected, but if a
// developer decides to put some short text to emulate an icon, (like "?") then it will
// have the correct styling.
ProvideTextStyle(
MaterialTheme.typography.fromToken(ExtendedFabPrimaryTokens.LabelTextFont),
) {
Box(
modifier = Modifier
.defaultMinSize(
minWidth = FabPrimaryTokens.ContainerWidth,
minHeight = FabPrimaryTokens.ContainerHeight,
),
contentAlignment = Alignment.Center,
) { content() }
}
}
}
}
There's an extension function on a ColorScheme class, that converts an Elevation into a Color: surfaceColorAtElevation()
. ColorScheme 类上有一个扩展函数,可将 Elevation 转换为 Color: surfaceColorAtElevation()
。
TextField(
...
colors = TextFieldDefaults.textFieldColors(
containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(1.dp)
)
)
Also, there are six different Elevation levels in Material 3 ( https://m3.material.io/styles/elevation/tokens ):此外,Material 3 ( https://m3.material.io/styles/elevation/tokens ) 中有六个不同的海拔级别:
Level 0: 0dp
Level 1: 1dp
Level 2: 3dp
Level 3: 6dp
Level 4: 8dp
Level 5: 12dp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.