3

How can override the standard durations of the Scaffold SnackBar to apply my own durations in MS. I can't see a way to do it

is EventsToAddAlbumScreen.ShowSnackbarEventToAddAlbumScreen -> scaffoldState.snackbarHostState.showSnackbar(
                    message = event.message,
                    duration = SnackbarDuration.Short // <-- want to change this to 500ms for example
                )
Jerome
  • 2,429
  • 2
  • 22
  • 37

2 Answers2

6

You can use SnackbarDuration.Indefinite and cancel it manually after the necessary delay:

LaunchedEffect(Unit) {
    val job = launch {
        scaffoldState.snackbarHostState.showSnackbar("Hi", duration = SnackbarDuration.Indefinite)
    }
    delay(500)
    job.cancel()
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
1

You can use this trick:

val scope = rememberCoroutineScope()
val snackBarMessage = stringResource(id = R.string.snackbar_message)

scope.launch {
    val job = scope.launch {
        snackbarHostState.showSnackbar(
            message = snackBarMessage,
            duration = SnackbarDuration.Indefinite,
        )
    }
    delay(500)
    job.cancel()
}
Rustam
  • 33
  • 6