Ryan Casler
03/07/2022, 5:46 PMText
composable was not getting MaterialTheme
colors unless it is also wrapped in a Scaffold
as shown below. Does that sound right that you need Scaffold
with the MaterialTheme
to get the colors to work or do i have a config issue somewhere?
// Does not get theme colors
MaterialTheme { Text("test") }
// Gets theme colors
MaterialTheme { Scaffold { Text("Test") } }
Chris Sinco [G]
03/07/2022, 8:37 PMStylianos Gakis
03/07/2022, 8:41 PMText
Composable you’ll see that it gets the color like this:
val textColor = color.takeOrElse {
style.color.takeOrElse {
LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
}
}
Which means that if you don’t specify it explicitly it tries to look at the LocalContentColor
. Now if you’re at a place in your composable tree where that does not exist, I guess it would simply take the default which is Color.Black.
Now guess what Scaffold
or Surface
do? Something like:
(in params) color: Color = MaterialTheme.colors.surface,
CompositionLocalProvider(
LocalContentColor provides contentColor
)
Reading the compose source code sometimes feels like a maze, but very often is super straightforward and can help you understand a lot of things! This is one of them!Ryan Casler
03/07/2022, 10:46 PMChris Sinco [G]
03/07/2022, 11:37 PMRyan Casler
03/08/2022, 4:40 PM