SnackbarHostState


State of the SnackbarHost, controls the queue and the current Snackbar being shown inside the SnackbarHost.

This state usually lives as a part of a ScaffoldState and provided to the SnackbarHost automatically, but can be decoupled from it and live separately when desired.

Summary

Public constructors

Cmn

Public functions

suspend SnackbarResult
showSnackbar(
    message: String,
    actionLabel: String?,
    duration: SnackbarDuration
)

Shows or queues to be shown a Snackbar at the bottom of the Scaffold at which this state is attached and suspends until snackbar is disappeared.

Cmn

Public properties

SnackbarData?

The current SnackbarData being shown by the SnackbarHost, of null if none.

Cmn

Public constructors

SnackbarHostState

SnackbarHostState()

Public functions

showSnackbar

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

Shows or queues to be shown a Snackbar at the bottom of the Scaffold at which this state is attached and suspends until snackbar is disappeared.

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:

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.SnackbarHostState
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

// 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 Scaffold.

Parameters
message: String

text to be shown in the Snackbar

actionLabel: String? = null

optional action label to show as button in the Snackbar

duration: SnackbarDuration = SnackbarDuration.Short

duration to control how long snackbar will be shown in SnackbarHost, either SnackbarDuration.Short, SnackbarDuration.Long or SnackbarDuration.Indefinite

Returns
SnackbarResult

SnackbarResult.ActionPerformed if option action has been clicked or SnackbarResult.Dismissed if snackbar has been dismissed via timeout or by the user

Public properties

currentSnackbarData

val currentSnackbarDataSnackbarData?

The current SnackbarData being shown by the SnackbarHost, of null if none.