Provides horizontal shared axis navigation transforms adhering to Material 3 motion specifications.
HorizontalSharedAxisTransitionSample
@Preview
@Sampled
@Composable
fun HorizontalSharedAxisTransitionSample() {
var targetScreen by remember { mutableIntStateOf(1) }
var isGestureInProgress by remember { mutableStateOf(false) }
var backEvent by remember { mutableStateOf(NavigationEvent()) }
// TODO(b/561731740): Replace this manual navigation setup with NavDisplay once it adopts
// MutableContentTransform, which provides built-in transition and back gesture handling.
val transitionState = remember { DeferredTransitionState(targetScreen) }
val transition = rememberDeferredTransition(transitionState)
LaunchedEffect(isGestureInProgress, targetScreen) {
if (isGestureInProgress) {
transitionState.defer(targetScreen)
} else {
transitionState.animateTo(targetScreen)
}
}
val scrimColor = HorizontalSharedAxisNavigationTransitions.scrimColor
val density = LocalDensity.current
transition.DeferredAnimatedContent(
transitionSpec = {
if (targetState > initialState) {
HorizontalSharedAxisNavigationTransitions.forwardTransform()
} else {
HorizontalSharedAxisNavigationTransitions.backwardTransform()
}
},
mutableTransformSpec = {
if (initialState > targetState) {
HorizontalSharedAxisNavigationTransitions.predictiveBackTransform(
backEvent = { backEvent },
density = density,
scrimColor = scrimColor,
)
} else {
null
}
},
) { screen ->
Surface(
modifier = Modifier.fillMaxSize(),
color =
if (screen == 1) {
MaterialTheme.colorScheme.primaryContainer
} else {
MaterialTheme.colorScheme.secondaryContainer
},
) {
Box(Modifier.fillMaxSize().padding(16.dp), contentAlignment = Alignment.Center) {
Text("Screen $screen", style = MaterialTheme.typography.headlineLarge)
}
}
}
}