zt
03/20/2025, 4:20 AMfun Theme(color: Color, theme: Theme, useDynamicTheme: Boolean, content: @Composable () -> Unit)
to
fun Theme(color: ColorProducer, theme: Theme, useDynamicTheme: Boolean, content: @Composable () -> Unit)
What I don't get is why ColorProducer lambda fixes it. Color is immutable, so it shouldn't trigger recomposition, right? Is there something else I'm not understanding?Stylianos Gakis
03/20/2025, 8:08 AMzt
03/20/2025, 2:10 PM@Composable
fun Theme(color: ColorProducer, theme: Theme, useDynamicTheme: Boolean, content: @Composable () -> Unit) {
if (useDynamicTheme) {
DynamicMaterialTheme(
seedColor = color(),
useDarkTheme = theme == Theme.DARK || theme == Theme.SYSTEM && isSystemInDarkTheme(),
style = PaletteStyle.Fidelity,
content = content
)
} else {
MaterialTheme(
colorScheme = if (theme == Theme.DARK || theme == Theme.SYSTEM && isSystemInDarkTheme()) {
darkColorScheme()
} else {
lightColorScheme()
}
) {
content()
}
}
}
Stylianos Gakis
03/20/2025, 2:15 PMColorProducer
the ColorProducer
itself does not change, so there is no reason for this composable to recompose. In the case of color, the color itself changes, so this composable needs to recompose to make sure that it does not show any stale data.
I would assume that if you changed it to
fun Theme(getColor: () -> Color, theme: Theme, useDynamicTheme: Boolean, content: @Composable () -> Unit) {
it would also manage to not recompose, since it'd be the same lambda across compositions and nobody would be asking for the color from inside of it