https://kotlinlang.org logo
#compose
Title
# compose
c

Chuck Stein

06/12/2022, 5:02 PM
The
SnackbarDuration.Indefinite
javadoc says "_Show the Snackbar indefinitely until explicitly dismissed or action is clicked_", however so far I haven't figured out any way for the user to manually dismiss a snackbar other than clicking the action. How can this be done?
2
a

Albert Chang

06/12/2022, 11:42 PM
I think it means “dismissed programmatically”.
1
c

Chuck Stein

06/13/2022, 4:07 AM
Tragic, I'd love to provide a way for the user to dismiss it manually but I am using the action button for something else already and the Snackbar API seems pretty closed for extension when using it withing a scaffold / snackbarhost
a

Albert Chang

06/13/2022, 4:13 AM
Actually it’s quite easy to extend. You can use your own Snackbar implementation or apply any modifiers to the official implementation. We implemented swipe-to-dismiss for snackbars like this:
Copy code
Scaffold(
    snackbarHost = { state ->
        SnackbarHost(hostState = state) { data ->
            SwipeDismissSnackbar(snackbarData = data)
        }
    }
)
c

Chuck Stein

06/13/2022, 12:42 PM
Thanks, I'll try this, maybe I was thinking about it the wrong way
I guess I was trying to conditionally change the Snackbar implementation, e.g. I wouldn't always want the user to be able to manually dismiss it. (Was thinking of optionally giving the entire Snackbar a click handler). But then I would need to somehow pass that click handler (and whether the Snackbar should be clickable) through SnackbarData which I couldn't think of a clean way to do. But just making all Snackbars swipe-to-dismiss seems reasonable.
Would be nice if we could extend SnackbarData to add some of our own data that our implementation might need
4 Views