Stylianos Gakis
02/26/2023, 3:44 PMval someColor = if (MaterialTheme.colors.isLight) {
MaterialTheme.colors.primary
} else {
MaterialTheme.colors.secondary
},
But in m3, MaterialTheme.colorScheme.isLight
doesn’t exist anymore. Is there some other way I can get this information straight from the MaterialTheme object?isSystemInDarkMode()
directly and assume that my MaterialTheme is actually following whatever that returned. But it may not be the case, as I may have overwritten that for some small part of the UI or whatever 🤔Ian Lake
02/26/2023, 3:54 PMStylianos Gakis
02/26/2023, 10:32 PMinternal val LocalAppNameMaterial3ColorScheme = staticCompositionLocalOf<AppNameMaterial3ColorScheme> {
error("AppNameMaterial3ColorScheme not provided")
}
class AppNameMaterial3ColorScheme(
val containedButtonContainer: Color,
val onContainedButtonContainer: Color,
)
internal fun darkAppNameColorScheme(
colorScheme: ColorScheme,
) = AppNameMaterial3ColorScheme(
containedButtonContainer = colorScheme.tertiary,
onContainedButtonContainer = colorScheme.onTertiary,
)
internal fun lightAppNameColorScheme(
colorScheme: ColorScheme,
) = AppNameMaterial3ColorScheme(
containedButtonContainer = colorScheme.primary,
onContainedButtonContainer = colorScheme.onPrimary,
)
inside my AppTheme, create it:
val (colorScheme, appNameColorTheme) = when {
darkTheme -> DarkColorScheme to darkAppNameColorScheme(DarkColorScheme)
else -> LightColorScheme to lightAppNameColorScheme(LightColorScheme)
}
And pass it down through a local inside my AppTheme composable
LocalHedvigMaterial3ColorScheme provides appNameColorTheme,
And finally add this to my colorScheme so that it’s convenient to use, just like I would any other item from inside colorScheme
val ColorScheme.containedButtonContainer: Color
@Composable
get() = LocalAppNameMaterial3ColorScheme.current.containedButtonContainer
val ColorScheme.onContainedButtonContainer: Color
@Composable
get() = LocalAppNameMaterial3ColorScheme.current.onContainedButtonContainer
Where it’s an extension on ColorScheme, simply to not make this look odd from all the other cases, now can just call MaterialTheme.colorScheme.containedButtonContainer
.