---
title: "SearchBarState"
description: "The state of a search bar."
type: "class"
---

<div class='type'>Class</div>


<a id='references'></a>

<div class='sourceset sourceset-common'>Common</div>


```kotlin
@ExperimentalMaterial3Api
class SearchBarState
private constructor(
    internal val animatable: Animatable<Float, AnimationVector1D>,
    private val contentAnimatable: Animatable<Float, AnimationVector1D>,
    internal val animationSpecForExpand: AnimationSpec<Float>,
    internal val animationSpecForCollapse: AnimationSpec<Float>,
    private val animationSpecForContentFadeIn: AnimationSpec<Float>,
    private val animationSpecForContentFadeOut: AnimationSpec<Float>,
)
```


The state of a search bar.


## Secondary Constructors

```kotlin
constructor(
    initialValue: SearchBarValue,
    animationSpecForExpand: AnimationSpec<Float>,
    animationSpecForCollapse: AnimationSpec<Float>,
) : this(
    animatable =
        Animatable(if (initialValue == SearchBarValue.Expanded) Expanded else Collapsed),
    contentAnimatable =
        Animatable(if (initialValue == SearchBarValue.Expanded) Expanded else Collapsed),
    animationSpecForExpand = animationSpecForExpand,
    animationSpecForCollapse = animationSpecForCollapse,
    animationSpecForContentFadeIn = snap(),
    animationSpecForContentFadeOut = snap(),
)
```


Construct a `SearchBarState`.

#### Parameters

| | |
| --- | --- |
| initialValue | the initial value of whether the search bar is collapsed or expanded. |
| animationSpecForExpand | the animation spec used when the search bar expands. |
| animationSpecForCollapse | the animation spec used when the search bar collapses. |



```kotlin
constructor(
    initialValue: SearchBarValue,
    animationSpecForExpand: AnimationSpec<Float>,
    animationSpecForCollapse: AnimationSpec<Float>,
    animationSpecForContentFadeIn: AnimationSpec<Float>,
    animationSpecForContentFadeOut: AnimationSpec<Float>,
) : this(
    animatable =
        Animatable(if (initialValue == SearchBarValue.Expanded) Expanded else Collapsed),
    contentAnimatable =
        Animatable(if (initialValue == SearchBarValue.Expanded) Expanded else Collapsed),
    animationSpecForExpand = animationSpecForExpand,
    animationSpecForCollapse = animationSpecForCollapse,
    animationSpecForContentFadeIn = animationSpecForContentFadeIn,
    animationSpecForContentFadeOut = animationSpecForContentFadeOut,
)
```


Construct a `SearchBarState`.

#### Parameters

| | |
| --- | --- |
| initialValue | the initial value of whether the search bar is collapsed or expanded. |
| animationSpecForExpand | the animation spec used when the search bar expands. |
| animationSpecForCollapse | the animation spec used when the search bar collapses. |
| animationSpecForContentFadeIn | the animation spec used for the content when the search bar expands. |
| animationSpecForContentFadeOut | the animation spec used for the content when the search bar collapses. |



## Properties

<div class='sourceset sourceset-common'>Common</div>


```kotlin
var collapsedCoords: LayoutCoordinates?
```


The layout coordinates, if available, of the search bar when it is collapsed. Used to
coordinate the expansion animation.



<div class='sourceset sourceset-common'>Common</div>


```kotlin
@get:FloatRange(from = 0.0, to = 1.0)
val progress: Float
```


The animation progress of the search bar, where 0 represents `SearchBarValue.Collapsed` and 1
represents `SearchBarValue.Expanded`.



<div class='sourceset sourceset-common'>Common</div>


```kotlin
val isAnimating: Boolean
```


Whether the state is currently animating



<div class='sourceset sourceset-common'>Common</div>


```kotlin
val targetValue: SearchBarValue
```


Whether the search bar is going to be expanded or collapsed.



<div class='sourceset sourceset-common'>Common</div>


```kotlin
val currentValue: SearchBarValue
```


Whether the search bar is currently expanded or collapsed. If the search bar is currently
animating to/from the expanded state, `currentValue` is `SearchBarValue.Expanded` until the
animation completes.



## Functions

```kotlin
suspend fun animateToExpanded()
```


Animate the search bar to its expanded state.


```kotlin
suspend fun animateToCollapsed()
```


Animate the search bar to its collapsed state.


```kotlin
suspend fun snapTo(fraction: Float)
```


Snap the search bar progress to the given `fraction`, where 0 represents
`SearchBarValue.Collapsed` and 1 represents `SearchBarValue.Expanded`.


## Companion Object

#### Methods

<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun Saver(
            animationSpecForExpand: AnimationSpec<Float>,
            animationSpecForCollapse: AnimationSpec<Float>,
        ): Saver<SearchBarState, *>
```


The default `Saver` implementation for `SearchBarState`.



<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun Saver(
            animationSpecForExpand: AnimationSpec<Float>,
            animationSpecForCollapse: AnimationSpec<Float>,
            animationSpecForContentFadeIn: AnimationSpec<Float>,
            animationSpecForContentFadeOut: AnimationSpec<Float>,
        ): Saver<SearchBarState, *>
```


The default `Saver` implementation for `SearchBarState`.





