Before I define a color with a name in resources file such as Code A, and access it using such as Divider(color = colorResource(R.color.colorGrey)).
Now I read the article. I get the following warning:
Prefer colors from the theme rather than hard-coded colors. Even though it's possible to access colors using the colorResource function, it's recommended that the colors of your app are defined in a MaterialTheme which can be accessed from your composables like MaterialTheme.colors.primary.
So I study Code B, I find I can use such as MaterialTheme.colors.secondary to access a color.
I find there are only some preset colors with name in colors such as primary, primaryVariant, ...etc, it seems that I can only overwrite preset colors such as primary = Purple900.
I hope I can customize a color with a name in MaterialTheme, so I can use such as MaterialTheme.colors.myIcon to access color, how can I do?
Code A
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="colorGrey">#FF6200EE</color>
</resources>
Code B
@Composable
fun SoundMeterTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}
MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes,
content = content
)
}
private val DarkColorPalette = darkColors(
primary = Purple200,
primaryVariant = Purple700,
secondary = Teal200
)
fun darkColors(
primary: Color = Color(0xFFBB86FC),
...
): Colors = Colors(
primary,
...
)