SnackbarHostState

Class

Common
class SnackbarHostState

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

This state is usually remembered and used to provide a SnackbarHost to a Scaffold.

Properties

Common
var currentSnackbarData

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

Functions

suspend fun showSnackbar(
        message: String,
        actionLabel: String? = null,
        withDismissAction: Boolean = false,
        duration: SnackbarDuration =
            if (actionLabel == null) SnackbarDuration.Short else SnackbarDuration.Indefinite,
    ): SnackbarResult

Shows or queues to be shown a Snackbar at the bottom of the Scaffold to which this state is attached and suspends until the snackbar has 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 snackbar 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:

To change the Snackbar appearance, change it in 'snackbarHost' on the Scaffold.

Parameters

messagetext to be shown in the Snackbar
actionLabeloptional action label to show as button in the Snackbar
withDismissActiona boolean to show a dismiss action in the Snackbar. This is recommended to be set to true for better accessibility when a Snackbar is set with a SnackbarDuration.Indefinite
durationduration to control how long snackbar will be shown in SnackbarHost, either SnackbarDuration.Short, SnackbarDuration.Long or SnackbarDuration.Indefinite.

Returns

SnackbarResult.ActionPerformed if option action has been clicked or SnackbarResult.Dismissed if snackbar has been dismissed via timeout or by the user
suspend fun showSnackbar(visuals: SnackbarVisuals): SnackbarResult

Shows or queues to be shown a Snackbar at the bottom of the Scaffold to which this state is attached and suspends until the snackbar has 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 snackbar 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:

Parameters

visualsSnackbarVisuals that are used to create a Snackbar

Returns

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