因此,我正在尝试实现一些Material3主题,并希望显示一个项目列表,这些项目可能被禁用或不禁用。据我所见,ListItem Composable不允许在禁用状态下显示它,因为内容的颜色被硬编码为"enabled = true“,如下面的代码示例所示。如何在禁用状态下实现ListItem?
@Composable
@ExperimentalMaterial3Api
fun ListItem(
headlineText: @Composable () -> Unit,
modifier: Modifier = Modifier,
overlineText: @Composable (() -> Unit)? = null,
supportingText: @Composable (() -> Unit)? = null,
leadingContent: @Composable (() -> Unit)? = null,
trailingContent: @Composable (() -> Unit)? = null,
colors: ListItemColors = ListItemDefaults.colors(),
tonalElevation: Dp = ListItemDefaults.Elevation,
shadowElevation: Dp = ListItemDefaults.Elevation,
) {
if (overlineText == null && supportingText == null) {
// One-Line List Item
ListItem(
modifier = modifier,
containerColor = colors.containerColor().value,
contentColor = colors.headlineColor(enabled = true).value, // headlineColor is always enabled
tonalElevation = tonalElevation,
shadowElevation = shadowElevation,
minHeight = ListTokens.ListItemContainerHeight,
paddingValues = PaddingValues(ListItemHorizontalPadding, ListItemVerticalPadding)
) {
if (leadingContent != null) {
leadingContent(
leadingContent = leadingContent,
contentColor = colors.leadingIconColor(enabled = true).value,
topAlign = false
)()
}
Box(
Modifier
.weight(1f)
.align(Alignment.CenterVertically)
) {
ProvideTextStyleFromToken(
colors.headlineColor(enabled = true).value,
ListTokens.ListItemLabelTextFont,
headlineText
)
}
if (trailingContent != null) {
trailingContent(
trailingContent = trailingContent,
contentColor = colors.trailingIconColor(enabled = true).value,
topAlign = false
)()
}
}
}发布于 2022-10-21 08:13:22
目前(M3 1.0.0-rc01) ListItem不支持enabled参数,也不使用disabledLeadingIconColor。
作为解决办法,您可以使用以下内容:
var enabled by remember { mutableStateOf(true)}
Column {
ListItem(
headlineText = { Text("One line list item with 24x24 icon") },
leadingContent = {
Icon(
Icons.Filled.Favorite,
contentDescription = "Localized description",
)
},
colors = ListItemDefaults.colors(
leadingIconColor = if (enabled) Teal200 else Red)
)
Divider()
}https://stackoverflow.com/questions/74150163
复制相似问题