英文:
Jetpack Compose add custom dark/light colors?
问题
我们可以创建深色和浅色调色板,没问题。但它只有12种颜色。如何为浅色和深色调色板添加更多自定义颜色?
英文:
Wh can create dark and light color palette, it's ok.

But it has only 12 colors.
How to add more custom colors for the light and dark palette?
答案1
得分: 26
以下是翻译好的部分:
- 创建一个数据类,其中包含您想要使用的内容以及其
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() }
- 根据明亮或暗黑主题创建不同的变体:
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)
)
- 在
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.
- Create a data class that will contain what you want to use and its
staticCompositionLocalOf. For exampleCustomColorsPalette.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() }
- 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)
)
- Use the logic in the
Composetheme 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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论