2

I have a Row with two buttons, each setting a different state (SomeState.FirstState or SomeState.SecondState) when clicked. Depending on the state, I want to show a different composable, with smooth transitions between them.

Row(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.SpaceEvenly
) {
    Button(onClick = {
        state = SomeState.FirstState
    }) {
        Text(text = "Set first state")
    }
    Button(onClick = {
        state = SomeState.SecondState("Second state text")
    }) {
        Text(text = "Set second state")
    }
}

AnimatedVisibility(
    visible = state is SomeState.SecondState,
    modifier = Modifier.align(Alignment.CenterHorizontally)
) {
    if (state is SomeState.SecondState) {
        SecondStateText(state = state as SomeState.SecondState)
    }
}

The problem is that when SomeState.FirstState is set, the previous composable abruptly disappears. How can I smoothly transition between the composables when switching states, to provide a better user experience?

2 Answers 2

0
  1. Don't need to repeatedly check the condition inside the AnimatedVisibility. Content inside it will only show if the visible paramter is true.

  2. Specify the enter & exit transition to provide a better animation when view gets visible/invisible.

     val enterTransition = fadeIn() + scaleIn() // Enter animation
     val exitTransition = fadeOut() + scaleOut() // Exit animation
    
     AnimatedVisibility(
         visible = state is SomeState.SecondState,
         modifier = Modifier.align(Alignment.CenterHorizontally),
         enter = enterTransition,
         exit = exitTransition
     ) {
         SecondStateText(state = state as SomeState.SecondState)
     }
    
     AnimatedVisibility(
         visible = state is SomeState.FirstState,
         modifier = Modifier.align(Alignment.CenterHorizontally),
         enter = enterTransition,
         exit = exitTransition
     ) {
         FirstStateText(state = state as SomeState.FirstState)
     }
    
Sign up to request clarification or add additional context in comments.

2 Comments

I had almost the same code with a strict cast, and then the application crashed when state is changed
Can you please also share the error of the crash with this. It'll help to debug the issue.
0

you can use AnimatedContent to transition between your composable smoothly. AnimatedContent is a container that automatically animates its content when targetState changes. Its content for different target states is defined in a mapping between a target state and a composable function.

@Composable
@Sampled
fun SimpleAnimatedContentSample() {
    // enum class ContentState { Foo, Bar, Baz }
    @Composable
    fun Foo() {
        Box(
            Modifier
                .size(200.dp)
                .background(Color(0xffffdb00))
        )
    }

    @Composable
    fun Bar() {
        Box(
            Modifier
                .size(40.dp)
                .background(Color(0xffff8100))
        )
    }

    @Composable
    fun Baz() {
        Box(
            Modifier
                .size(80.dp, 20.dp)
                .background(Color(0xffff4400))
        )
    }

    var contentState: ContentState by remember { mutableStateOf(ContentState.Foo) }
    AnimatedContent(contentState) {
        when (it) {
            // Specifies the mapping between a given ContentState and a composable function.
            ContentState.Foo -> Foo()
            ContentState.Bar -> Bar()
            ContentState.Baz -> Baz()
        }
    }
}

credit : https://www.composables.com/animation/animatedcontent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.