# SnackbarHostState

Artifact: [androidx.compose.material:material](https://developer.android.com/jetpack/androidx/releases/compose-material) [View Source](https://cs.android.com/search?q=file:androidx/compose/material/SnackbarHost.kt+class:androidx.compose.material.SnackbarHostState) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-material#1.0.0) Common/AllAndroid/JVM
- Cmn

  ```
  class SnackbarHostState
  ```

*** ** * ** ***

State of the `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHost.composable#SnackbarHost(androidx.compose.material.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)`, controls the queue and the current `https://developer.android.com/reference/kotlin/androidx/compose/material/Snackbar.composable#Snackbar(androidx.compose.ui.Modifier,kotlin.Function0,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.Dp,kotlin.Function0)` being shown inside the `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHost.composable#SnackbarHost(androidx.compose.material.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)`.

This state usually lives as a part of a `https://developer.android.com/reference/kotlin/androidx/compose/material/ScaffoldState` and provided to the `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHost.composable#SnackbarHost(androidx.compose.material.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)` automatically, but can be decoupled from it and live separately when desired.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHostState#SnackbarHostState()()` | Cmn |

| ### Public functions |
|---|---|---|
| `suspend https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarResult` | `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHostState#showSnackbar(kotlin.String,kotlin.String,androidx.compose.material.SnackbarDuration)( message: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, actionLabel: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, duration: https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarDuration )` Shows or queues to be shown a `https://developer.android.com/reference/kotlin/androidx/compose/material/Snackbar.composable#Snackbar(androidx.compose.ui.Modifier,kotlin.Function0,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.Dp,kotlin.Function0)` at the bottom of the `https://developer.android.com/reference/kotlin/androidx/compose/material/Scaffold.composable#Scaffold(androidx.compose.foundation.layout.WindowInsets,androidx.compose.ui.Modifier,androidx.compose.material.ScaffoldState,kotlin.Function0,kotlin.Function0,kotlin.Function1,kotlin.Function0,androidx.compose.material.FabPosition,kotlin.Boolean,kotlin.Function1,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.unit.Dp,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1)` at which this state is attached and suspends until snackbar is disappeared. | Cmn |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarData?` | `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHostState#currentSnackbarData()` The current `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarData` being shown by the `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHost.composable#SnackbarHost(androidx.compose.material.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)`, of `null` if none. | Cmn |

## Public constructors

### SnackbarHostState

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

```
SnackbarHostState()
```

## Public functions

### showSnackbar

Cmn

```
suspend fun showSnackbar(
    message: String,
    actionLabel: String? = null,
    duration: SnackbarDuration = SnackbarDuration.Short
): SnackbarResult
```

Shows or queues to be shown a `https://developer.android.com/reference/kotlin/androidx/compose/material/Snackbar.composable#Snackbar(androidx.compose.ui.Modifier,kotlin.Function0,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.Dp,kotlin.Function0)` at the bottom of the `https://developer.android.com/reference/kotlin/androidx/compose/material/Scaffold.composable#Scaffold(androidx.compose.foundation.layout.WindowInsets,androidx.compose.ui.Modifier,androidx.compose.material.ScaffoldState,kotlin.Function0,kotlin.Function0,kotlin.Function1,kotlin.Function0,androidx.compose.material.FabPosition,kotlin.Boolean,kotlin.Function1,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.unit.Dp,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1)` at which this state is attached and suspends until snackbar is disappeared.

`https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHostState` guarantees to show at most one snackbar at a time. If this function is called while another snackbar is already visible, it will be suspended until this snack bar is shown and subsequently addressed. If the caller is cancelled, the snackbar will be removed from display and/or the queue to be displayed.

All of this allows for granular control over the snackbar queue from within:

```kotlin
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.ExtendedFloatingActionButton
import androidx.compose.material.Scaffold
import androidx.compose.material.ScaffoldDefaults
import androidx.compose.material.Snackbar
import androidx.compose.material.SnackbarHost
import androidx.compose.material.SnackbarHostState
import androidx.compose.material.SnackbarResult
import androidx.compose.material.Text
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier

// decouple snackbar host state from scaffold state for demo purposes
// this state, channel and flow is for demo purposes to demonstrate business logic layer
val snackbarHostState = remember { SnackbarHostState() }
// we allow only one snackbar to be in the queue here, hence conflated
val channel = remember { Channel<Int>(Channel.Factory.CONFLATED) }
LaunchedEffect(channel) {
    channel.receiveAsFlow().collect { index ->
        val result =
            snackbarHostState.showSnackbar(
                message = "Snackbar # $index",
                actionLabel = "Action on $index",
            )
        when (result) {
            SnackbarResult.ActionPerformed -> {
                /* action has been performed */
            }
            SnackbarResult.Dismissed -> {
                /* dismissed, no action needed */
            }
        }
    }
}
Scaffold(
    // attach snackbar host state to the scaffold
    scaffoldState = rememberScaffoldState(snackbarHostState = snackbarHostState),
    floatingActionButton = {
        var clickCount by remember { mutableStateOf(0) }
        ExtendedFloatingActionButton(
            text = { Text("Show snackbar") },
            onClick = {
                // offset snackbar data to the business logic
                channel.trySend(++clickCount)
            },
        )
    },
    contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
    content = { innerPadding ->
        Text(
            "Snackbar demo",
            modifier = Modifier.padding(innerPadding).fillMaxSize().wrapContentSize(),
        )
    },
)
```

To change the Snackbar appearance, change it in 'snackbarHost' on the `https://developer.android.com/reference/kotlin/androidx/compose/material/Scaffold.composable#Scaffold(androidx.compose.foundation.layout.WindowInsets,androidx.compose.ui.Modifier,androidx.compose.material.ScaffoldState,kotlin.Function0,kotlin.Function0,kotlin.Function1,kotlin.Function0,androidx.compose.material.FabPosition,kotlin.Boolean,kotlin.Function1,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.unit.Dp,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1)`.

| Parameters |
|---|---|
| `message: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | text to be shown in the Snackbar |
| `actionLabel: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html? = null` | optional action label to show as button in the Snackbar |
| `duration: https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarDuration = SnackbarDuration.Short` | duration to control how long snackbar will be shown in `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHost.composable#SnackbarHost(androidx.compose.material.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)`, either `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarDuration#Short`, `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarDuration#Long` or `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarDuration#Indefinite` |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarResult` | `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarResult#ActionPerformed` if option action has been clicked or `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarResult#Dismissed` if snackbar has been dismissed via timeout or by the user |

## Public properties

### currentSnackbarData

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

```
val currentSnackbarData: SnackbarData?
```

The current `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarData` being shown by the `https://developer.android.com/reference/kotlin/androidx/compose/material/SnackbarHost.composable#SnackbarHost(androidx.compose.material.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)`, of `null` if none.