diff --git a/BasicsCodelab/app/src/main/java/com/codelab/basics/MainActivity.kt b/BasicsCodelab/app/src/main/java/com/codelab/basics/MainActivity.kt index f959858f9..395844b53 100644 --- a/BasicsCodelab/app/src/main/java/com/codelab/basics/MainActivity.kt +++ b/BasicsCodelab/app/src/main/java/com/codelab/basics/MainActivity.kt @@ -44,7 +44,6 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -68,9 +67,10 @@ class MainActivity : ComponentActivity() { @Composable fun MyApp(modifier: Modifier = Modifier) { + var shouldShowOnboarding by rememberSaveable { mutableStateOf(true) } - Surface(modifier, color = MaterialTheme.colorScheme.background) { + Surface(modifier) { if (shouldShowOnboarding) { OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false }) } else { @@ -84,6 +84,7 @@ fun OnboardingScreen( onContinueClicked: () -> Unit, modifier: Modifier = Modifier ) { + Column( modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, @@ -91,12 +92,14 @@ fun OnboardingScreen( ) { Text("Welcome to the Basics Codelab!") Button( - modifier = Modifier.padding(vertical = 24.dp), + modifier = Modifier + .padding(vertical = 24.dp), onClick = onContinueClicked ) { Text("Continue") } } + } @Composable @@ -112,12 +115,12 @@ private fun Greetings( } @Composable -private fun Greeting(name: String) { +private fun Greeting(name: String, modifier: Modifier = Modifier) { Card( colors = CardDefaults.cardColors( containerColor = MaterialTheme.colorScheme.primary ), - modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp) + modifier = modifier.padding(vertical = 4.dp, horizontal = 8.dp) ) { CardContent(name) } @@ -125,7 +128,7 @@ private fun Greeting(name: String) { @Composable private fun CardContent(name: String) { - var expanded by remember { mutableStateOf(false) } + var expanded by rememberSaveable { mutableStateOf(false) } Row( modifier = Modifier @@ -151,7 +154,7 @@ private fun CardContent(name: String) { if (expanded) { Text( text = ("Composem ipsum color sit lazy, " + - "padding theme elit, sed do bouncy. ").repeat(4), + "padding theme elit, sed do bouncy. ").repeat(4), ) } } @@ -168,27 +171,29 @@ private fun CardContent(name: String) { } } + +@Preview(showBackground = true, widthDp = 320, heightDp = 320) +@Composable +fun OnboardingPreview() { + BasicsCodelabTheme { + OnboardingScreen(onContinueClicked = {}) // Do nothing on click. + } +} + @Preview( showBackground = true, widthDp = 320, uiMode = UI_MODE_NIGHT_YES, - name = "DefaultPreviewDark" + name = "GreetingPreviewDark" ) @Preview(showBackground = true, widthDp = 320) @Composable -fun DefaultPreview() { +fun GreetingPreview() { BasicsCodelabTheme { Greetings() } } -@Preview(showBackground = true, widthDp = 320, heightDp = 320) -@Composable -fun OnboardingPreview() { - BasicsCodelabTheme { - OnboardingScreen(onContinueClicked = {}) - } -} @Preview @Composable