Skip to content

Instantly share code, notes, and snippets.

@rahulsainani
Last active May 5, 2021 23:16
Show Gist options
  • Save rahulsainani/4e528aedd4d1037ef83bff36c891b2e2 to your computer and use it in GitHub Desktop.
Save rahulsainani/4e528aedd4d1037ef83bff36c891b2e2 to your computer and use it in GitHub Desktop.
// 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