EnterExitState


EnterExitState contains the three states that are involved in the enter and exit transition of AnimatedVisibility. More specifically, PreEnter and Visible defines the initial and target state of an enter transition, whereas Visible and PostExit are the initial and target state of an exit transition.

See blow for an example of custom enter/exit animation in AnimatedVisibility using Transition<EnterExitState> (i.e. AnimatedVisibilityScope.transition):

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

var visible by remember { mutableStateOf(true) }
Box(modifier = Modifier.clickable { visible = !visible }) {
    AnimatedVisibility(
        visible = visible,
        modifier = Modifier.align(Alignment.Center),
        enter = fadeIn(),
        exit = fadeOut(animationSpec = tween(200)) + scaleOut()
    ) { // Content that needs to appear/disappear goes here:
        // Here we can optionally define a custom enter/exit animation by creating an animation
        // using the Transition<EnterExitState> object from AnimatedVisibilityScope:

        // As a part of the enter transition, the corner radius will be animated from 0.dp to 50.dp.
        val cornerRadius by transition.animateDp {
            when (it) {
                EnterExitState.PreEnter -> 0.dp
                EnterExitState.Visible -> 50.dp
                // No corner radius change when exiting.
                EnterExitState.PostExit -> 50.dp
            }
        }
        Box(
            Modifier.background(Color.Red, shape = RoundedCornerShape(cornerRadius))
                .height(100.dp).fillMaxWidth()
        )
    }
}

Summary

Enum Values

PostExit

Target state of a custom exit animation in AnimatedVisibility.

PreEnter

The initial state of a custom enter animation in AnimatedVisibility..

Visible

The Visible state is the target state of a custom enter animation, also the initial state of a custom exit animation in AnimatedVisibility.

Public functions

EnterExitState
valueOf(value: String)

Returns the enum constant of this type with the specified name.

Cmn
Array<EnterExitState>

Returns an array containing the constants of this enum type, in the order they're declared.

Cmn

Enum Values

PostExit

@ExperimentalAnimationApi
val EnterExitState.PostExitEnterExitState

Target state of a custom exit animation in AnimatedVisibility.

PreEnter

@ExperimentalAnimationApi
val EnterExitState.PreEnterEnterExitState

The initial state of a custom enter animation in AnimatedVisibility..

Visible

@ExperimentalAnimationApi
val EnterExitState.VisibleEnterExitState

The Visible state is the target state of a custom enter animation, also the initial state of a custom exit animation in AnimatedVisibility.

Public functions

valueOf

fun valueOf(value: String): EnterExitState

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Throws
kotlin.IllegalArgumentException

if this enum type has no constant with the specified name

values

fun values(): Array<EnterExitState>

Returns an array containing the constants of this enum type, in the order they're declared.

This method may be used to iterate over the constants.