# SeekableTransitionState

Artifact: [androidx.compose.animation:animation-core](https://developer.android.com/jetpack/androidx/releases/compose-animation) [View Source](https://cs.android.com/search?q=file:androidx/compose/animation/core/Transition.kt+class:androidx.compose.animation.core.SeekableTransitionState) Added in [1.6.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.6.0) Common/All
- Cmn

  ```
  class SeekableTransitionState<S : Any?> : TransitionState<S>
  ```

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.compose.animation.core.TransitionState](https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TransitionState)\<S\> ||
|   | ↳ | [androidx.compose.animation.core.SeekableTransitionState](https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState) |

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TransitionState` that can manipulate the progress of the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Transition` by seeking with `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` or animating with `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)`.

A `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState` can only be used with one `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Transition` instance. Once assigned, it cannot be reassigned to a different `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Transition` instance.

```kotlin
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.SeekableTransitionState
import androidx.compose.animation.core.Transition
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.rememberTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.systemGestureExclusion
import androidx.compose.material.Button
import androidx.compose.material.Slider
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.dp

Column {
    val seekingState = remember { SeekableTransitionState(BoxSize.Small) }
    val scope = rememberCoroutineScope()
    Column {
        Row {
            Button(
                onClick = { scope.launch { seekingState.animateTo(BoxSize.Small) } },
                Modifier.wrapContentWidth().weight(1f),
            ) {
                Text("Animate Small")
            }
            Button(
                onClick = { scope.launch { seekingState.seekTo(0f, BoxSize.Small) } },
                Modifier.wrapContentWidth().weight(1f),
            ) {
                Text("Seek Small")
            }
            Button(
                onClick = { scope.launch { seekingState.seekTo(0f, BoxSize.Medium) } },
                Modifier.wrapContentWidth().weight(1f),
            ) {
                Text("Seek Medium")
            }
            Button(
                onClick = { scope.launch { seekingState.seekTo(0f, BoxSize.Large) } },
                Modifier.wrapContentWidth().weight(1f),
            ) {
                Text("Seek Large")
            }
            Button(
                onClick = { scope.launch { seekingState.animateTo(BoxSize.Large) } },
                Modifier.wrapContentWidth().weight(1f),
            ) {
                Text("Animate Large")
            }
        }
    }
    Slider(
        value = seekingState.fraction,
        modifier = Modifier.systemGestureExclusion().padding(10.dp),
        onValueChange = { value -> scope.launch { seekingState.seekTo(fraction = value) } },
    )
    val transition = rememberTransition(seekingState)

    val scale: Float by
        transition.animateFloat(
            transitionSpec = { tween(easing = LinearEasing) },
            label = "Scale",
        ) { state ->
            when (state) {
                BoxSize.Small -> 1f
                BoxSize.Medium -> 2f
                BoxSize.Large -> 3f
            }
        }

    transition.AnimatedContent(
        transitionSpec = {
            fadeIn(tween(easing = LinearEasing)) togetherWith
                fadeOut(tween(easing = LinearEasing))
        }
    ) { state ->
        if (state == BoxSize.Large) {
            Box(Modifier.size(50.dp).background(Color.Magenta))
        } else {
            Box(Modifier.size(50.dp))
        }
    }
    Box(
        Modifier.fillMaxSize()
            .wrapContentSize(Alignment.Center)
            .size(100.dp)
            .graphicsLayer {
                scaleX = scale
                scaleY = scale
            }
            .background(Color.Blue)
    )
}
```

## Summary

| ### Public constructors |
|---|---|
| `<S : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#SeekableTransitionState(kotlin.Any)(initialState: S)` | Cmn |

| ### Public functions |
|---|---|---|
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)(targetState: S, animationSpec: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/FiniteAnimationSpec<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html>?)` Updates the current `targetState` to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)` and begins an animation to the new state. | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)(fraction: @https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange(from = 0.0, to = 1.0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, targetState: S)` Starts seeking the transition to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` with `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` used to indicate the progress towards `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)`. | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)(targetState: S)` Sets `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` and `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)` to `targetState` and snaps all values to those at that state. | Cmn |

| ### Public properties |
|---|---|---|
| `open S` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` Current state of the transition. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#fraction()` The progress of the transition from `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#targetState()` as a fraction of the entire duration. | Cmn |
| `open S` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#targetState()` Target state of the transition. | Cmn |

## Public constructors

### SeekableTransitionState

Cmn

```
<S : Any?> SeekableTransitionState(initialState: S)
```

## Public functions

### animateTo

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.7.0)

```
suspend fun animateTo(
    targetState: S = this.targetState,
    animationSpec: FiniteAnimationSpec<Float>? = null
): Unit
```

Updates the current `targetState` to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)` and begins an animation to the new state. If the current `targetState` is the same as `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)` then the current transition animation is continued. If a previous transition was interrupted, `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` is changed to the former `targetState` and the start values are animated toward the former `targetState`.

Upon completion of the animation, `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` will be changed to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)`.

| Parameters |
|---|---|
| `targetState: S = this.targetState` | The state to animate towards. |
| `animationSpec: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/FiniteAnimationSpec<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html>? = null` | If provided, is used to animate the animation fraction. If `null`, the transition is linearly traversed based on the duration of the transition. |

### seekTo

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.7.0)

```
suspend fun seekTo(
    fraction: @FloatRange(from = 0.0, to = 1.0) Float,
    targetState: S = this.targetState
): Unit
```

Starts seeking the transition to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` with `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` used to indicate the progress towards `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)`. If the previous `targetState` was already `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` then `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` only stops any current animation towards that state and snaps the fraction to the new value. Otherwise, the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` is changed to the former `targetState` and `targetState` is changed to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` and an animation is started, moving the start values towards the former `targetState`. This will return when the initial values have reached `currentState` and the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` has been reached.

`https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)` also allows the developer to change the state, but does not animate any values. Instead, it instantly moves all values to those at the new `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)`.

```kotlin
import androidx.compose.animation.core.SeekableTransitionState
import androidx.compose.animation.core.Transition
import androidx.compose.animation.core.rememberTransition
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.systemGestureExclusion
import androidx.compose.material.Slider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

val seekingState = remember { SeekableTransitionState(BoxSize.Small) }
LaunchedEffect(seekingState.targetState) { seekingState.seekTo(0f, BoxSize.Large) }
val scope = rememberCoroutineScope()
Slider(
    value = seekingState.fraction,
    modifier = Modifier.systemGestureExclusion().padding(10.dp),
    onValueChange = { value -> scope.launch { seekingState.seekTo(fraction = value) } },
)
val transition = rememberTransition(seekingState)
// use the transition
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)` |   |

### snapTo

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.7.0)

```
suspend fun snapTo(targetState: S): Unit
```

Sets `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` and `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)` to `targetState` and snaps all values to those at that state. The transition will not have any animations running after running `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)`.

This can have a similar effect as `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)`. However, `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` moves the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` to the former `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)` and animates the initial values of the animations from the current values to those at `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()`. `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#seekTo(kotlin.Float,kotlin.Any)` also allows the developer to move the state between any fraction between `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` and `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)`, while `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)` moves all state to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#snapTo(kotlin.Any)` without any further seeking allowed.

```kotlin
import androidx.compose.animation.core.SeekableTransitionState
import androidx.compose.animation.core.Transition
import androidx.compose.animation.core.rememberTransition
import androidx.compose.animation.core.snap
import androidx.compose.foundation.layout.Box
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope

val seekingState = remember { SeekableTransitionState(BoxSize.Small) }
val scope = rememberCoroutineScope()
Button(onClick = { scope.launch { seekingState.snapTo(BoxSize.Large) } }) {
    Text("Snap to the Small state")
}
val transition = rememberTransition(seekingState)
// use the transition
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#animateTo(kotlin.Any,androidx.compose.animation.core.FiniteAnimationSpec)` |   |

## Public properties

### currentState

Cmn

```
open val currentState: S
```

Current state of the transition. If there is an active transition, `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TransitionState#currentState()` and `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TransitionState#targetState()` are different.

### fraction

Cmn Added in [1.6.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.6.0)

```
val fraction: Float
```

The progress of the transition from `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#targetState()` as a fraction of the entire duration.

If `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#targetState()` and `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#currentState()` are the same, `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SeekableTransitionState#fraction()` will be 0.

### targetState

Cmn

```
open val targetState: S
```

Target state of the transition. If this is the same as `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TransitionState#currentState()`, no transition is active.