Snackbar Controller
object SnackbarController
A controller for managing snackbar events.
Provides a centralized way to send and receive snackbar events across different parts of the application.
Note: events uses a single-consumer channel flow. Ensure only one SnackbarHost collects events at the root of your UI hierarchy.
Example usage:
// In your root Composable (e.g., inside a Scaffold)
val snackbarHostState = remember { SnackbarHostState() }
SnackbarController.events.ObserveAsEvents { event ->
val result = snackbarHostState.showSnackbar(
message = event.message,
actionLabel = event.action?.name,
duration = event.duration
)
if (result == SnackbarResult.ActionPerformed) {
event.action?.action?.invoke()
}
}
// Triggering a snackbar from a ViewModel or CoroutineScope
viewModelScope.launch {
SnackbarController.sendEvent("Item saved successfully")
}Content copied to clipboard
Functions
Link copied to clipboard
Sends a SnackbarEvent to be displayed.
suspend fun sendEvent(message: String, action: SnackbarAction? = null, duration: SnackbarDuration = SnackbarDuration.Long)
Convenience function to send a SnackbarEvent without constructing a data class instance.
Link copied to clipboard
Attempts to send a SnackbarEvent synchronously without suspending.