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

Bryan Herbst

03/02/2021, 6:52 PM
What is the right way to get a callback when a
ModalBottomSheetLayout
is dismissed, either from accessibility actions or from tapping on the scrim outside the sheet? Drop a
DisposableEffect
inside the sheet’s content? I don’t see any sort of callback or observable state.
k

KoskiA

03/02/2021, 6:54 PM
👍 1
b

Bryan Herbst

03/02/2021, 6:59 PM
I like the LaunchedEfffect approach- that would support getting a callback for half expanded and fully expanded states as well. Both that and DisposableEffect feel a little weird, but that might just be me getting used to the declarative UI paradigm
m

matvei

03/02/2021, 7:00 PM
the closure of the ModalSheet is an event you can observe. If you want to trigger a side effect when happens (which is why you are reaching out for a callback probably, right?) you can do it via DisposableEffect or LaunchedEffect, if you need a scope:
Copy code
val state = rememberModalBottomsheetState(BottmsheetValue.Expanded)
DisposableEffect(state.isClosed) {
    //this is called when state.isClosed changed during last composition, so you can trigger a side effect when you want (true or false)
    onDispose { /* cleanup here */}
}

ModalBottomSheetLayout(state, ....)
💯 1
b

Bryan Herbst

03/02/2021, 7:01 PM
Thanks! A side effect is exactly what I’m thinking, for example sending analytics events.
m

matvei

03/02/2021, 7:01 PM
So this tools will suit your case perfectly then 🙂
👌 2
👍 1