From 3af6480b597d2f5619908ac30bb6365452e72f4f Mon Sep 17 00:00:00 2001 From: czeth <44951784+jasv18@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:46:53 -0400 Subject: [PATCH] Fix bug in 'Experiment with other animations' --- .../main/java/com/example/woof/MainActivity.kt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/example/woof/MainActivity.kt b/app/src/main/java/com/example/woof/MainActivity.kt index d5615d1..7821fde 100644 --- a/app/src/main/java/com/example/woof/MainActivity.kt +++ b/app/src/main/java/com/example/woof/MainActivity.kt @@ -21,6 +21,7 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.annotation.DrawableRes import androidx.annotation.StringRes +import androidx.compose.animation.animateColorAsState import androidx.compose.animation.animateContentSize import androidx.compose.animation.core.Spring import androidx.compose.animation.core.spring @@ -38,6 +39,7 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ExpandLess import androidx.compose.material.icons.filled.ExpandMore import androidx.compose.material3.Card +import androidx.compose.material3.CardDefaults import androidx.compose.material3.CenterAlignedTopAppBar import androidx.compose.material3.Icon import androidx.compose.material3.IconButton @@ -111,8 +113,14 @@ fun DogItem( modifier: Modifier = Modifier ) { var expanded by remember { mutableStateOf(false) } + val color by animateColorAsState( + targetValue = if (expanded) MaterialTheme.colorScheme.tertiaryContainer else MaterialTheme.colorScheme.primaryContainer + ) Card( - modifier = modifier + modifier = modifier, + colors = CardDefaults.cardColors( + containerColor = color + ) ) { Column( modifier = Modifier @@ -122,6 +130,13 @@ fun DogItem( stiffness = Spring.StiffnessMedium ) ) +// .background(color = color) +// 7. (Optional) Experiment with other animations +// This is a case where placing color in the composable column causes the parent +// card color to appear when the expanded variable is false and +// the Dog Hobby composable disappears leaving a colored background. So to solve it, +// put the animated color on the container color card. + ) { Row( modifier = Modifier