跳转到内容

Chip

胶囊形可选中筛选标签,对应 ColorOS 的 COUIChip 选择样式(Widget.COUI.Chip.Choice)。未选中时是半透明灰色胶囊配主文本色标签;选中后胶囊填充强调色、标签变为白色。两种状态间用 COUI 选中弹簧交叉渐变,按压时缩小并叠加按压色(COUIPressFeedbackHelper)。

引入

kotlin
import io.github.suqi8.coui.kmp.basic.Chip
import io.github.suqi8.coui.kmp.basic.ChipDefaults

基本用法

kotlin
var selected by remember { mutableStateOf(false) }

Chip(
    selected = selected,
    onClick = { selected = !selected },
    label = "推荐",
)

带前置图标

图标放置在 16dp 盒子里,并通过 LocalContentColor 染成当前标签色(COUIChip chipIconApplyTint)。

kotlin
Chip(
    selected = selected,
    onClick = { selected = !selected },
    label = "推荐",
    icon = {
        Icon(
            imageVector = COUIIcons.Basic.Check,
            contentDescription = null,
            modifier = Modifier.size(16.dp),
        )
    },
)

单选组

kotlin
var selectedIndex by remember { mutableIntStateOf(0) }

Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
    listOf("全部", "照片", "视频").forEachIndexed { index, label ->
        Chip(
            selected = selectedIndex == index,
            onClick = { selectedIndex = index },
            label = label,
        )
    }
}

8dp 间距对应 COUIChipGroup 的默认横/纵间距。

组件状态

禁用状态

禁用的 Chip 保持其选中/未选中形态,但切换为禁用容器色与内容色,且不再响应按压:

kotlin
Chip(
    selected = true,
    onClick = { /* Ignored while disabled */ },
    label = "Disabled",
    enabled = false,
)

属性

Chip

属性类型说明默认值必需
selectedBoolean是否选中-
onClick() -> Unit点击回调-
labelString文本标签(单行,超长省略)-
modifierModifier应用于 Chip 的修饰符Modifier
enabledBoolean是否启用true
icon(@Composable () -> Unit)?可选前置图标null
cornerRadiusDp胶囊圆角ChipDefaults.CornerRadius
minWidthDp最小宽度ChipDefaults.MinWidth
minHeightDp最小高度ChipDefaults.MinHeight
maxWidthDp最大宽度ChipDefaults.MaxWidth
colorsChipColors颜色配置ChipDefaults.chipColors()
insideMarginPaddingValuesChip 内部横向内边距ChipDefaults.InsideMargin
interactionSourceMutableInteractionSource?交互源null
indicationIndication?指示效果(按压反馈已内置)null

ChipDefaults

常量类型默认值COUI 来源
MinWidthDp52.dpcoui_chip_default_min_width
MinHeightDp32.dpcoui_chip_selection_style_height
MaxWidthDp300.dpcoui_chip_default_max_width
TextMaxWidthDp200.dpcoui_chip_default_max_text_width
CornerRadiusDp16.dpchipCornerRadius -1 → 胶囊(高度 / 2)
IconSizeDp16.dpcoui_chip_selection_style_chip_icon_size
IconSpacingDp4.dpcoui_chip_selection_style_chip_icon_end_padding
InsideMarginPaddingValuesPaddingValues(horizontal = 12.dp)coui_chip_selection_style_chip_horizontal_padding

方法

方法名类型说明
textStyle()TextStyle默认标签样式(COUITheme.textStyles.body2,COUI couiTextBodyM 14sp)
chipColors()ChipColors创建 Chip 的颜色配置

chipColors() 工厂

参数类型默认值COUI 属性
containerColorColorCOUITheme.colorScheme.secondaryVariantuncheckedBackgroundColor
selectedContainerColorColorCOUITheme.colorScheme.primarycheckedBackgroundColor
disabledContainerColorColorCOUITheme.colorScheme.disabledSecondaryVariantuncheckedDisabledBackgroundColor
selectedDisabledContainerColorColorCOUITheme.colorScheme.disabledPrimaryButtoncheckedDisabledBackgroundColor
contentColorColorCOUITheme.colorScheme.onSurfaceuncheckedTextColor
selectedContentColorColorCOUITheme.colorScheme.onPrimarycheckedTextColor
disabledContentColorColorCOUITheme.colorScheme.disabledOnSecondaryVariantuncheckedDisabledTextColor
selectedDisabledContentColorColorCOUITheme.colorScheme.disabledOnPrimaryButtoncheckedDisabledTextColor

行为

  • 选中/取消选中时,胶囊、标签与图标颜色在临界阻尼弹簧上交叉渐变(COUIChipDrawable.TintAnimation,response 0.3s / bounce 0)。
  • 按压时 Chip 向 0.92 倍缩小并叠加按压色(COUIPressFeedbackHelper + COUIMaskEffectDrawable);快速点按也会明显闪现按压色。
  • 标签单行显示,超过 TextMaxWidth 省略;整个 Chip 宽度被钳制在 MinWidthMaxWidth 之间。

变更日志

基于 Apache-2.0 许可发布