Jetpack compose 如何等待动画结束

Jetpack compose how to wait for animation ends

我通过 AnimatedVisibility (slideInVertically/SlideOut) 制作了简单的动画。 当我按下“nextScreenButton”时,我通过 navController 进行新导航。 过渡是立即完成的,所以没有时间制作退出动画。 如何让等到动画结束

我可以为动画时间输入一些延迟,但这不是个好方法。

代码:

      Scaffold() {
        AnimatedVisibility(
            //Boolean State for open animation 
            OpenChooseProfilePageAnim.value,
            
            initiallyVisible= false,
            enter = slideInVertically(
                initialOffsetY = { fullHeight -> fullHeight },
                animationSpec = tween(
                    durationMillis = 3000,
                    easing = LinearOutSlowInEasing
                )
            ),
            exit = slideOutVertically(
                targetOffsetY = { fullHeight -> fullHeight },
                animationSpec = tween(
                    durationMillis = 3000,
                    easing = LinearOutSlowInEasing
                )
            )
        ) {
            ConstraintLayout() {
                Card() {
                    Column() {
                        
                        //Some other Composable items
                        
                        //Composable button
                        NextScreenButton() {
                            //calling navigation here
                        }
                    }
                }
            }
        }
    }

求助。

NextScreenButton 代码:

    fun navigateToMainListPage(navController: NavController) {
        //change State of visibility for "AnimatedVisibility"
        AnimationsState.OpenChooseProfilePageAnim.value = false
        //navigate to another route in NavHost
        navController.navigate(ROUTE_MAIN_LIST)
    }

导航主机:


    @Composable
    fun LoginGroupNavigation(startDestination: String) {
        val navController = rememberNavController()
        NavHost(navController, startDestination = startDestination) {
            composable(LoginScreens.LoginScreen.route) {
                LoginMainPage(navController)
            }
            composable(LoginScreens.EnteringPhoneNumScreen.route,
                arguments = listOf(navArgument("title") { type = NavType.StringType },
            )) {
                val title =  it.arguments?.getString("title") ?: ""
                EnterPhoneNumberForSmsPage(
                    navController = navController,
                    title
                )
            }
    //more composable screens

   

主要思路如下:

   val animVisibleState = remember { MutableTransitionState(false) }
    .apply { targetState = true }

//Note: Once the exit transition is finished,
//the content composable will be removed from the tree, 
//and disposed.
//Both currentState and targetState will be false for 
//visibleState.
if (!animVisibleState.targetState &&
    !animVisibleState.currentState
) {
    //navigate to another route in NavHost
    navController.navigate(ROUTE_MAIN_LIST)
    return
}


AnimatedVisibility(
    visibleState = animVisibleState,
    enter = fadeIn(
        animationSpec = tween(durationMillis = 200)
    ),
    exit = fadeOut(
        animationSpec = tween(durationMillis = 200)
    )
) {
    NextButton() {
        //start exit animation
        animVisibleState.targetState = false
    }

}

在 Navigation 2.4.0-alpha05 中引入了淡入淡出作为导航的默认过渡。使用最新版本的 Navigation 2.4.0-alpha06,您可以通过 Accompanist

添加自定义转换