A design fault in Compose themeing/coloring :think...
# compose
d
A design fault in Compose themeing/coloring 🤔 ? Why does
LocalContentColor
always default to black, and this default is not overridden by a
MaterialTheme
block? This means that even in Dark themes, the text colour inside a
TextField
, for example, is also black, leaving it hard to read. The API comments talk about this being 'typically' set to
MaterialTheme.onSurface
- but don't say whether this is the direct responsibility of the developer, or how to do it. This feels like something I shouldn't be handling directly - why do other Material elements adjust well to dark/light scheme, but the text inside a
TextField
does not? Either I'm missing something or this is inconsistent/broken? Edit: Straw-man solution 👉 🧵
I 'fixed' this issue in the Android Studio Compose template project (in which it's confirmed present), by editing the
MyMaterialTheme
class like so:
Copy code
@Composable
fun MyApplicationTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }

    MaterialTheme(
        colors = colors,
        typography = Typography,
        shapes = Shapes
    ) {
        CompositionLocalProvider(LocalContentColor provides MaterialTheme.colors.onSurface) {
            content()
        }
    }
}
...specifically by adding the block beginning:
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colors.onSurface) { ...
This sets the default content color to be the 'on surface' color of the theme. Which seems to me like what the default should always be.
k
Why don't you use
Surface
?
i
See the previous thread on why the default template always includes a
Surface
(/or get one for free by using
Scaffold
): https://kotlinlang.slack.com/archives/CJLTWPH7S/p1628359360407100
💡 2