使用 crossfade 动画来在多个 layout 之间切换, 接收可选的 animationSpec.
Created
November 21, 2021 13:11
-
-
Save hyoban/77e49f82b5255dc1d6f065e45e4b8c13 to your computer and use it in GitHub Desktop.
This file contains 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
import androidx.compose.animation.Crossfade | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.material.Button | |
import androidx.compose.material.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.setValue | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
@Composable | |
fun CrossfadeExample() { | |
Box( | |
modifier = Modifier.fillMaxSize() | |
) { | |
Column( | |
modifier = Modifier.align(Alignment.Center) | |
) { | |
var currentPage by remember { mutableStateOf("A") } | |
Button( | |
onClick = { | |
currentPage = "B" | |
} | |
) { | |
Text(text = "Change to B") | |
} | |
Crossfade( | |
targetState = currentPage | |
) { screen -> | |
when (screen) { | |
"A" -> Text("Page A") | |
"B" -> Text("Page B") | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment