Created
May 4, 2023 20:01
-
-
Save c5inco/28af7456b3abd67f5b49bd1cdf510a9b to your computer and use it in GitHub Desktop.
Demonstration of how Preview works with a background, across themes, and using MaterialTheme and Surface
This file contains hidden or 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
package com.example.c5inco | |
import android.content.res.Configuration.UI_MODE_NIGHT_YES | |
import androidx.compose.foundation.isSystemInDarkTheme | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.darkColorScheme | |
import androidx.compose.material3.lightColorScheme | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.tooling.preview.Preview | |
@PreviewsWithBackground | |
@PreviewsWithNoBackground | |
@Composable | |
fun PreviewWithNoThemeOrSurface() { | |
Greeting() | |
} | |
@PreviewsWithBackground | |
@PreviewsWithNoBackground | |
@Composable | |
fun PreviewWithThemeNoSurface() { | |
MaterialTheme( | |
colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else lightColorScheme() | |
) { | |
Greeting() | |
} | |
} | |
@PreviewsWithBackground | |
@PreviewsWithNoBackground | |
@Composable | |
fun PreviewWithThemeAndSurface() { | |
MaterialTheme( | |
colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else lightColorScheme() | |
) { | |
Surface { | |
Greeting() | |
} | |
} | |
} | |
@Composable | |
fun Greeting() { | |
Text("Hello") | |
} | |
@Preview(name = "BG, Dark", showBackground = true, uiMode = UI_MODE_NIGHT_YES) | |
@Preview(name = "BG, Light", showBackground = true) | |
annotation class PreviewsWithBackground | |
@Preview(name = "No BG, Dark", uiMode = UI_MODE_NIGHT_YES) | |
@Preview("No BG, Light") | |
annotation class PreviewsWithNoBackground |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment