Skip to content

Instantly share code, notes, and snippets.

@hyoban
Created November 21, 2021 13:11
Show Gist options
  • Save hyoban/77e49f82b5255dc1d6f065e45e4b8c13 to your computer and use it in GitHub Desktop.
Save hyoban/77e49f82b5255dc1d6f065e45e4b8c13 to your computer and use it in GitHub Desktop.

Crossfade

使用 crossfade 动画来在多个 layout 之间切换, 接收可选的 animationSpec.

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