跳转到内容

BreadcrumbBar

BreadcrumbBar 是 COUI 中的水平导航组件,以箭头图标分隔显示路径段,每段呈现为胶囊形状。当内容超出可用宽度时,组件会水平滚动而非折叠,与移动设备上文件管理器的惯例一致。

highlightIndexitems 列表解耦:调用方可以显示完整路径的同时高亮任意一段(例如当前目录,或用户回退到的某个父级目录)。组件会自动滚动以保持高亮项可见。

引入

kotlin
import io.github.suqi8.coui.kmp.basic.BreadcrumbBar
import io.github.suqi8.coui.kmp.basic.BreadcrumbItem
import io.github.suqi8.coui.kmp.basic.joinToPath

基本用法

kotlin
val items = listOf(
    BreadcrumbItem(path = "/storage/emulated/0", text = "Internal storage"),
    BreadcrumbItem(path = "DataBackup"),
    BreadcrumbItem(path = "apps"),
    BreadcrumbItem(path = "com.tencent.mobileqq"),
    BreadcrumbItem(path = "user_0"),
)

BreadcrumbBar(
    items = items,
    onItemClick = { index -> /* 跳转到对应段 */ },
)

高亮控制

highlightIndex 参数与 items 列表解耦。你可以显示完整路径的同时高亮任意一段:

kotlin
var highlightIndex by remember { mutableIntStateOf(items.lastIndex) }

BreadcrumbBar(
    items = items,
    onItemClick = { index -> highlightIndex = index },
    highlightIndex = highlightIndex,
)

拼接路径段

使用 joinToPath 扩展函数从 BreadcrumbItem 列表重建完整路径:

kotlin
val fullPath = items.joinToPath("/")
// 结果: "/storage/emulated/0/DataBackup/apps/com.tencent.mobileqq/user_0"

val windowsPath = items.joinToPath("\\")
// 结果: "/storage/emulated/0\\DataBackup\\apps\\com.tencent.mobileqq\\user_0"

组件状态

禁用状态

kotlin
BreadcrumbBar(
    items = items,
    onItemClick = {},
    enabled = false,
)

属性

属性名类型说明默认值是否必须
itemsList<BreadcrumbItem>要显示的面包屑项列表-
onItemClick(Int) -> Unit点击某段时触发,回传索引-
modifierModifier应用于面包屑栏的修饰符Modifier
highlightIndexInt高亮段的索引;负值表示不高亮且不自动滚动items.lastIndex
enabledBoolean面包屑项是否可点击;禁用时仍可水平滚动true
colorsBreadcrumbBarColors面包屑栏颜色配置BreadcrumbBarDefaults.breadcrumbBarColors()
insideMarginPaddingValues面包屑栏内部边距BreadcrumbBarDefaults.InsideMargin
itemMaxWidthDp每个胶囊项的最大宽度;超出此宽度的文本会被截断BreadcrumbBarDefaults.ItemMaxWidth
scrollStateScrollState?水平滚动状态;传入外部提升的 state 可在重组时保留滚动位置null
interactionSourceMutableInteractionSource?交互源null
indicationIndication?点击交互的反馈效果LocalIndication.current
属性名类型说明默认值是否必须
pathString路径段,joinToPath 用它重建完整路径-
textString?显示文本,为 null 时显示 pathnull

BreadcrumbBarDefaults 对象提供了面包屑栏组件的默认值和颜色配置。

常量

常量名类型说明默认值
InsideMarginPaddingValues面包屑栏内部边距PaddingValues(horizontal = 12.dp, vertical = 8.dp)
ItemHeightDp每个胶囊项的高度32.dp
ItemHorizontalPaddingDp每个胶囊项的水平内边距(等于圆角半径,使左右端形成完整半圆)10.dp
ItemMaxWidthDp每个胶囊项的最大宽度;超出此宽度的文本会被截断160.dp

方法

方法名类型说明
breadcrumbBarColors()BreadcrumbBarColors创建面包屑栏的颜色配置
属性名类型说明
colorColor普通段的文字颜色
highlightColorColor高亮段的文字颜色
disabledColorColor禁用时的文字颜色
separatorColorColor箭头分隔符的颜色
backgroundColorColor普通段的胶囊背景色
highlightBackgroundColorColor高亮段的胶囊背景色
disabledBackgroundColorColor禁用时的胶囊背景色

进阶用法

自定义颜色

kotlin
BreadcrumbBar(
    items = items,
    onItemClick = { index -> /* 处理点击 */ },
    colors = BreadcrumbBarDefaults.breadcrumbBarColors(
        color = COUITheme.colorScheme.onBackground.copy(alpha = 0.55f),
        highlightColor = COUITheme.colorScheme.primary,
        backgroundColor = COUITheme.colorScheme.onBackground.copy(alpha = 0.1f),
        highlightBackgroundColor = COUITheme.colorScheme.primary.copy(alpha = 0.2f),
    ),
)

高亮与导航解耦

highlightIndex 独立于 items 列表,支持"显示完整路径但高亮某个父级段"的场景:

kotlin
// 完整路径: /storage/emulated/0/DataBackup/apps/com.tencent.mobileqq/user_0
// 高亮在 "apps"(索引 2),表示用户回退到了这里
BreadcrumbBar(
    items = items,
    onItemClick = { index ->
        // 按需更新 items 和 highlightIndex
        highlightIndex = index
    },
    highlightIndex = 2,
)

变更日志

基于 Apache-2.0 许可发布