Search code examples
kotlinandroid-jetpack-composeandroid-snackbarscaffold

jetpack compose Scaffold Possible to override the standard durations of SnackBar?


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
                )

Solution

  • 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()
    }