|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.compose.previewscreenshot.ui.theme |
| 18 | + |
| 19 | +import android.os.Build |
| 20 | +import androidx.compose.foundation.isSystemInDarkTheme |
| 21 | +import androidx.compose.material3.MaterialTheme |
| 22 | +import androidx.compose.material3.darkColorScheme |
| 23 | +import androidx.compose.material3.dynamicDarkColorScheme |
| 24 | +import androidx.compose.material3.dynamicLightColorScheme |
| 25 | +import androidx.compose.material3.lightColorScheme |
| 26 | +import androidx.compose.runtime.Composable |
| 27 | +import androidx.compose.ui.platform.LocalContext |
| 28 | + |
| 29 | +private val DarkColorScheme = darkColorScheme( |
| 30 | + primary = Purple80, |
| 31 | + secondary = PurpleGrey80, |
| 32 | + tertiary = Pink80 |
| 33 | +) |
| 34 | + |
| 35 | +private val LightColorScheme = lightColorScheme( |
| 36 | + primary = Purple40, |
| 37 | + secondary = PurpleGrey40, |
| 38 | + tertiary = Pink40 |
| 39 | + |
| 40 | + /* Other default colors to override |
| 41 | + background = Color(0xFFFFFBFE), |
| 42 | + surface = Color(0xFFFFFBFE), |
| 43 | + onPrimary = Color.White, |
| 44 | + onSecondary = Color.White, |
| 45 | + onTertiary = Color.White, |
| 46 | + onBackground = Color(0xFF1C1B1F), |
| 47 | + onSurface = Color(0xFF1C1B1F), |
| 48 | + */ |
| 49 | +) |
| 50 | + |
| 51 | +@Composable |
| 52 | +fun SampleTheme( |
| 53 | + darkTheme: Boolean = isSystemInDarkTheme(), |
| 54 | + // Dynamic color is available on Android 12+ |
| 55 | + dynamicColor: Boolean = true, |
| 56 | + content: @Composable () -> Unit |
| 57 | +) { |
| 58 | + val colorScheme = when { |
| 59 | + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { |
| 60 | + val context = LocalContext.current |
| 61 | + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) |
| 62 | + } |
| 63 | + |
| 64 | + darkTheme -> DarkColorScheme |
| 65 | + else -> LightColorScheme |
| 66 | + } |
| 67 | + |
| 68 | + MaterialTheme( |
| 69 | + colorScheme = colorScheme, |
| 70 | + typography = Typography, |
| 71 | + content = content |
| 72 | + ) |
| 73 | +} |
0 commit comments