Last active
May 5, 2021 23:16
-
-
Save rahulsainani/4e528aedd4d1037ef83bff36c891b2e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To simplfy the example, we will use the defaults provided in the Material Theme | |
private val LightThemeColors = lightColors() | |
private val DarkThemeColors = darkColors() | |
@Composable | |
fun ProvideAppColors( | |
colors: Colors, | |
content: @Composable () -> Unit | |
) { | |
val colorPalette = remember { colors } | |
CompositionLocalProvider(LocalAppColors provides colorPalette, content = content) | |
} | |
private val LocalAppColors = staticCompositionLocalOf { | |
LightThemeColors | |
} | |
@Composable | |
fun AppTheme( | |
darkTheme: Boolean = isSystemInDarkTheme(), | |
content: @Composable () -> Unit | |
) { | |
val colors = if (darkTheme) DarkThemeColors else LightThemeColors | |
ProvideAppColors(colors = colors) { | |
MaterialTheme( | |
colors = colors, | |
shapes = Shapes, | |
typography = typography, | |
content = content | |
) | |
} | |
} | |
object Theme { | |
val colors: Colors | |
@Composable | |
get() = LocalAppColors.current | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment