https://kotlinlang.org logo
#compose
Title
# compose
f

Francois Morvillier

02/25/2022, 5:07 PM
Hi! I find myself needing to specify background and foreground colors for every component in order to have the Dark Theme properly applied. This feels like too much work. Is there a better way?
Copy code
Card(backgroundColor = MaterialTheme.colorScheme.surface) {
    Text(text, color = MaterialTheme.colorScheme.onSurface)
}
Is there a good guide to best practices on the topic?
c

CRamsan

02/25/2022, 5:09 PM
You can set the colors directly into the Theme you are using. Then your components can get the colors from there: https://developer.android.com/jetpack/compose/themes/material
f

Francois Morvillier

02/25/2022, 5:22 PM
I'm using MaterialTheme from androidx.compose.material3. Is that the issue?
k

Kirill Grouchnikov

02/25/2022, 6:44 PM
These components should be getting the correct default M3 colors under light and dark mode without you having to specify them explicitly - unless you want to change the palette entries to use. Perhaps you're not applying dark mode correctly. How are you switching to dark mode?
f

Francois Morvillier

02/25/2022, 7:11 PM
Copy code
setContent {
  CompositionLocalProvider(
        LocalNavController provides navController,
        // ...
    ) {

        MyTheme(darkTheme = LocalUiSettings.current.darkMode.collectAsState(initial = false).value) {
            Scaffold( ... ) {
                // everything goes here
            }
       }
}
c

Chris Sinco [G]

02/26/2022, 1:25 AM
I find myself needing to specify background and foreground colors for every component in order to have the Dark Theme properly applied.
Where are you seeing issues where the dark theme is not being applied to your expectations? On device and/or in Previews?
Also looks like you are toggling dark theme based on a local app user setting correct? Does that have any tie in to the state of the system, i.e.
androidx.compose.foundation.isSystemInDarkTheme()
?
As folks mentioned, you should not need to be explicitly setting theme colors to all your components, especially ones from the Material library and Text/Icon, as those are already wired up to use MaterialTheme values and CompositionLocals like contentColor
The theme color mapping is needed for custom components, like setting background color of a Box/Row/Column via Modifiers if you don’t want to inherit the background of its parent.
f

Francois Morvillier

02/26/2022, 2:13 AM
The issues I'm seeing are on device. I don't use Previews. Indeed I am using local app user settings and not the system wide setting at the moment. The toggling works fine but I have to specify colors for each component otherwise I get the light theme by default. I do believe the issues started when I switched to material3 but I didn't realize it at the time.
k

Kirill Grouchnikov

02/26/2022, 4:18 AM
The way you're using dark mode switch is probably not the way Material 3 theme wrapper does.
f

Francois Morvillier

02/28/2022, 5:10 PM
I think I now understand: only the material3 composables work with Material 3 theming. I didn't realize all the components had been duplicated.
c

Chris Sinco [G]

02/28/2022, 5:35 PM
Yes, the Material components have defaults that map to MaterialTheme in their implementation. For custom Composables, outside of contentColor for Text/Icons, you will have to map the MaterialTheme keys manually
I didn’t realize all the components had been duplicated
Could you elaborate?
f

Francois Morvillier

02/28/2022, 6:20 PM
Could you elaborate?
Text, Card, Surface etc. each have a version in androidx.compose.material and in androidx.compose.material3. In my tests only the later respond automatically to the material 3 theming which makes it unfortunately unwieldy to switch back and forth from material to material 3 theming.
c

Chris Sinco [G]

02/28/2022, 6:23 PM
I see. Yeah if you are using a mix of Material2 and M3 components, managing themes between the two is quite cumbersome right now. This is something the team is working on resolving before Material3 goes to beta. Essentially, one should not have to depend on multiple versions of Material libraries in the stable future.
But until then, that is unfortunately the state of things if you are using the Material3 alpha libraries in Compose
f

Francois Morvillier

02/28/2022, 6:41 PM
I see. I'm trying to assess if I should continue trying to use Material 3 for my app (I plan to release later this year) or just revert to Material 2. I'm missing TextField in particular with Material 3. I can afford to wait a couple months before a I sort all this out. Any advice?
c

Chris Sinco [G]

02/28/2022, 7:35 PM
It’s unlikely the Material3 library will hit beta in a couple months, so to be safe I’d go with the Material2 library if you are releasing the app to production. But it depends also how much you want to use Material3 and why, like for dynamic colors, new components, etc, that are not available in Material2.
f

Francois Morvillier

02/28/2022, 9:18 PM
My main motivation to use material 3 was anticipating future changes and benefiting from the slightly improved color definitions (such as tertiary). Thanks for all your answers!
c

Chris Sinco [G]

02/28/2022, 11:17 PM
You’re welcome!
7 Views