Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating between Composables in Navigation with Compose

I have started trying out Navigation for compose.

I created my 2 Composables and everything is working fine.

But what I'm missing is Animations (or Transitions) between the pages. I didn't find any resources pointing out how to do it in Compose.

I know all animations are based on states in Compose, but the only thing I know is the Navigation Back Stack.

like image 723
Ahmad Sattout Avatar asked Jan 09 '21 13:01

Ahmad Sattout


2 Answers

You can use the composable I made to show enter animation (configure preferable effects in "enter" and "exit" parameters)

fun EnterAnimation(content: @Composable () -> Unit) {
    AnimatedVisibility(
        visible = true,
        enter = slideInVertically(
            initialOffsetY = { -40 }
        ) + expandVertically(
            expandFrom = Alignment.Top
        ) + fadeIn(initialAlpha = 0.3f),
        exit = slideOutVertically() + shrinkVertically() + fadeOut(),
        content = content,
        initiallyVisible = false
    )
}

You can use it like this:

NavHost(
    navController = navController,
    startDestination = "dest1"
) {
    composable("dest1") {
        EnterAnimation {
            FirstScreen(navController)
        }
    }
    composable("dest2") {
        EnterAnimation {
            SecondScreen(navController)
        }
    }
}
like image 127
Константин Семочкин Avatar answered Oct 12 '22 10:10

Константин Семочкин


In alpha-09 this is not supported. :(

Please, star this issue: https://issuetracker.google.com/issues/172112072

like image 6
nglauber Avatar answered Oct 12 '22 10:10

nglauber