Jetpack Compose 如何添加自定义的深色/浅色颜色?

huangapple go评论75阅读模式
英文:

Jetpack Compose add custom dark/light colors?

问题

我们可以创建深色和浅色调色板,没问题。但它只有12种颜色。如何为浅色和深色调色板添加更多自定义颜色?

英文:

Wh can create dark and light color palette, it's ok.
Jetpack Compose 如何添加自定义的深色/浅色颜色?

But it has only 12 colors.
How to add more custom colors for the light and dark palette?

答案1

得分: 26

以下是翻译好的部分:

  1. 创建一个数据类,其中包含您想要使用的内容以及其 staticCompositionLocalOf。例如,CustomColorsPalette.kt
@Immutable
data class CustomColorsPalette(
    val custom1OnBackground: Color = Color.Unspecified,
    val custom2OnBackground: Color = Color.Unspecified,
    val custom1OnSurface: Color = Color.Unspecified,
    val custom2OnSurface: Color = Color.Unspecified,
    val other1: Color = Color.Unspecified,
    val other2: Color = Color.Unspecified
)

val LocalCustomColorsPalette = staticCompositionLocalOf { CustomColorsPalette() }
  1. 根据明亮或暗黑主题创建不同的变体:
val OnLightCustomColorsPalette = CustomColorsPalette(
    custom1OnBackground = Color(color = 0xFF1A237E),
    custom2OnBackground = Color(color = 0xFF1B5E20),
    custom1OnSurface = Color(color = 0xFFE53935),
    custom2OnSurface = Color(color = 0xFFD81B60),
    other1 = Color(color = 0xFF006064),
    other2 = Color(color = 0xFF643700)
)

val OnDarkCustomColorsPalette = CustomColorsPalette(
    custom1OnBackground = Color(color = 0xFF1E88E5),
    custom2OnBackground = Color(color = 0xFF43A047),
    custom1OnSurface = Color(color = 0xFFC62828),
    custom2OnSurface = Color(color = 0xFFAD1457),
    other1 = Color(color = 0xFF00897B),
    other2 = Color(color = 0xFF896200)
)
  1. Compose 主题中使用逻辑来决定何时使用其中一个:
@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) DarkColorPalette else LightColorPalette
    
    val customColorsPalette =
        if (darkTheme) OnDarkCustomColorsPalette
        else OnLightCustomColorsPalette

    CompositionLocalProvider(
        LocalCustomColorsPalette provides customColorsPalette
    ) {
        MaterialTheme(
            colors = colors,
            typography = Typography,
            shapes = Shapes,
            content = content
        )
    }
}

现在可以直接从可组合代码中使用这些颜色,使用 LocalCustomColorsPalette.current

Text(
    text = "Anything...",
    color = LocalCustomColorsPalette.current.custom1OnBackground
)

有关 CompositionLocalProvider 的更多信息,请参阅 文档

奖励

可以更改对 LocalCustomColorsPalette.current 的调用,使其类似于 MaterialTheme 的其他属性。要做到这一点,您可以添加以下代码(可以在第一步的相同文件中):

// ...

val MaterialTheme.customColorsPalette: CustomColorsPalette
    @Composable
    @ReadOnlyComposable
    get() = LocalCustomColorsPalette.current

现在可以这样使用:

Text(
    text = "Anything...",
    color = MaterialTheme.customColorsPalette.custom1OnBackground
)
英文:

You can use CompositionLocalProvider for that.

  1. Create a data class that will contain what you want to use and its staticCompositionLocalOf. For example CustomColorsPalette.kt:
@Immutable
data class CustomColorsPalette(
    val custom1OnBackground: Color = Color.Unspecified,
    val custom2OnBackground: Color = Color.Unspecified,
    val custom1OnSurface: Color = Color.Unspecified,
    val custom2OnSurface: Color = Color.Unspecified,
    val other1: Color = Color.Unspecified,
    val other2: Color = Color.Unspecified
)

val LocalCustomColorsPalette = staticCompositionLocalOf { CustomColorsPalette() }
  1. Create different variations, based on light or dark theme:
val OnLightCustomColorsPalette = CustomColorsPalette(
    custom1OnBackground = Color(color = 0xFF1A237E),
    custom2OnBackground = Color(color = 0xFF1B5E20),
    custom1OnSurface = Color(color = 0xFFE53935),
    custom2OnSurface = Color(color = 0xFFD81B60),
    other1 = Color(color = 0xFF006064),
    other2 = Color(color = 0xFF643700)
)

val OnDarkCustomColorsPalette = CustomColorsPalette(
    custom1OnBackground = Color(color = 0xFF1E88E5),
    custom2OnBackground = Color(color = 0xFF43A047),
    custom1OnSurface = Color(color = 0xFFC62828),
    custom2OnSurface = Color(color = 0xFFAD1457),
    other1 = Color(color = 0xFF00897B),
    other2 = Color(color = 0xFF896200)
)
  1. Use the logic in the Compose theme to decide when to use one or the other:
@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) DarkColorPalette else LightColorPalette
    
    val customColorsPalette =
        if (darkTheme) OnDarkCustomColorsPalette
        else OnLightCustomColorsPalette

    CompositionLocalProvider(
        LocalCustomColorsPalette provides customColorsPalette
    ) {
        MaterialTheme(
            colors = colors,
            typography = Typography,
            shapes = Shapes,
            content = content
        )
    }
}

It is now possible to use these colors directly from the composable code with with LocalCustomColorsPalette.current:

Text(
    text = "Anything...",
    color = LocalCustomColorsPalette.current.custom1OnBackground
)

More information about CompositionLocalProvider on docs.


Bonus

It is possible to change the call to LocalCustomColorsPalette.current to make it similar to what we have with the other properties of MaterialTheme. To do this, you can add the following code (it could be in the same file as the first step):

// ...

val MaterialTheme.customColorsPalette: CustomColorsPalette
    @Composable
    @ReadOnlyComposable
    get() = LocalCustomColorsPalette.current

And now you can use like this:

Text(
    text = "Anything...",
    color = MaterialTheme.customColorsPalette.custom1OnBackground
)

huangapple
  • 本文由 发表于 2023年2月23日 20:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定