I'm trying to create a custom color scheme for Com...
# compose
j
I'm trying to create a custom color scheme for Compose. According to the official docs it can be done like this:
Copy code
@Immutable
data class CustomColors(
    val content: Color,
    val component: Color,
    val background: List<Color>
)
Then I'd use a
CompositionLocal
inside my custom theme to provide these colors. However, looking at different community samples I noticed that they do it like this:
Copy code
class AppColors(
    primary: Color,
    secondary: Color
)
    var primary by mutableStateOf(primary)
        private set
    var secondary by mutableStateOf(secondary)
        private set

    fun copy(
        primary: Color = this.primary,
        secondary: Color = this.secondary
    ): AppColors = AppColors(
        primary,
        secondary
    )
    
    fun updateColorsFrom(other: AppColors) {
        primary = other.primary
        secondary = other.seconday
    }
}
Basically replicating the
MaterialTheme
Colors
class implementation. My question is which approach should I follow? I understand that by using
mutableStateOf()
I can guarantee that Composables will react to individual color updates, but what if I know that my color scheme will never change at runtime? Is that the only reason to use
mutableStateOf()
? Any thoughts on this will be appreciated
l
If your colour scheme is static and you don't want to animate colours then just having a plain data class is fine. Using mutable state backed properties is only useful if you change some (but not all colours) at runtime.
🙏 1
j
my thoughts as well, thanks for clearing it out