Theming & Dark Mode (Material 3)
MaterialTheme, Color Schemes & Dynamic Color
A consistent look comes from a single `MaterialTheme`. You define a light and dark `ColorScheme`, switch based on the system setting, and (on Android 12+) opt into Dynamic Color from the user's wallpaper.
📚 Explanation
`MaterialTheme` exposes colors, typography, and shapes to every child Composable via `MaterialTheme.colorScheme`. You provide separate `lightColorScheme` / `darkColorScheme` definitions and pick one with `isSystemInDarkTheme()`. On Android 12+, `dynamicLightColorScheme`/`dynamicDarkColorScheme` generate a palette from the user's wallpaper, giving each device a personalized yet on-brand look.
Note: Code execution is not available for Android/Kotlin in the browser. Use Android Studio to run and test the app.
import android.os.Buildimport androidx.compose.foundation.isSystemInDarkThemeimport androidx.compose.material3.*import androidx.compose.runtime.Composableimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.platform.LocalContextprivate val LightColors = lightColorScheme(primary = Color(0xFF006A60),secondary = Color(0xFF4A635F))private val DarkColors = darkColorScheme(primary = Color(0xFF53DBC9),secondary = Color(0xFFB1CCC6))@Composablefun AppTheme(darkTheme: Boolean = isSystemInDarkTheme(),dynamicColor: Boolean = true,content: @Composable () -> Unit) {val colorScheme = when {// Android 12+: derive colors from the wallpaperdynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {val ctx = LocalContext.currentif (darkTheme) dynamicDarkColorScheme(ctx) else dynamicLightColorScheme(ctx)}darkTheme -> DarkColorselse -> LightColors}MaterialTheme(colorScheme = colorScheme,typography = Typography(),content = content)}