如何在 Jetpack Compose 中为 `NavigationBar()` 和 `NavigationBarItem()` 设置主题?

How do I theme `NavigationBar()` and `NavigationBarItem()` in Jetpack Compose?

我很难尝试更改 selected/unselected 图标和活动指示器的颜色。 docs 没有示例或正确的 Kdocs,而且我似乎无法在网上找到任何示例(请向我指出您知道的任何示例)。图标只是不改变颜色并保持黑色。

我的 NavigationBar 看起来像这样:

NavigationBar(
    containerColor = NavBarColor,
    contentColor = ContentColor, // <-- Can't tell what this is for.
    modifier = Modifier
        .align(Alignment.BottomCenter)
) {
    // ...
    destinations.forEachIndexed { index, item ->
        NavigationBarItem(
            selected = currentDestination?.hierarchy?.any { it.route == item.route } == true,

            onClick = {
                // ...
            },

            icon = {
                when (index) {
                    0 -> {
                        Icon(
                            imageVector = Icons.Rounded.Add,
                            contentDescription = stringResource(id = item.description)
                        )
                    }
                    1 -> {
                        Icon(
                            imageVector = Icons.Rounded.Home,
                            contentDescription = stringResource(id = item.description)
                        )
                    }
                    2 -> {
                        Icon(
                            imageVector = Icons.Rounded.Call,
                            contentDescription = stringResource(id = item.description)
                        )
                    }
                }
            },

            // Why on Earth does this not want to work:
            colors = NavigationBarItemDefaults.colors(
                selectedIconColor = NavBarColor, // <-- This doesn't work.
                unselectedIconColor = ContentColor, // <-- This doesn't work.
                indicatorColor = ContentColor // <-- This works.
            )
        )
    }
}

使用 import androidx.compose.material3.Icon 作为您的图标。

您在此处混合了 material 和 material3 代码:Icon 从 material 导入并使用 material LocalContentColor,另一方面 NavigationBarItem 是 material3 视图并提供 material3 LocalContentColor.