首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jetpack组合Material3禁用ListItem

Jetpack组合Material3禁用ListItem
EN

Stack Overflow用户
提问于 2022-10-21 07:31:10
回答 1查看 99关注 0票数 3

因此,我正在尝试实现一些Material3主题,并希望显示一个项目列表,这些项目可能被禁用或不禁用。据我所见,ListItem Composable不允许在禁用状态下显示它,因为内容的颜色被硬编码为"enabled = true“,如下面的代码示例所示。如何在禁用状态下实现ListItem?

代码语言:javascript
复制
@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
                )()
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-21 08:13:22

目前(M3 1.0.0-rc01) ListItem不支持enabled参数,也不使用disabledLeadingIconColor

作为解决办法,您可以使用以下内容:

代码语言:javascript
复制
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()
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74150163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档