Skip to content

Instantly share code, notes, and snippets.

@c5inco
Created May 4, 2023 20:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c5inco/28af7456b3abd67f5b49bd1cdf510a9b to your computer and use it in GitHub Desktop.
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
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