I'm using ModalBottomSheetLayout
@Composable
fun BottomSheetWrapper() {
ModalBottomSheetLayout(
sheetState = sheetState,
sheetContent = {
Spacer(modifier = Modifier.height(1.dp))
content()
},
modifier = modifier,
content = {}
)}
and I want to trigger some logic when user dismisses the bottomsheet
(back press or touch outside of the bottomsheet
). In another words I need something similar to onCancel
event of BottomSheetDialogFragment
.
What would be the best way to do it with ModalBottomsheetLayout
?
You can use the
sheetState
object to observe the state changes of theModalBottomSheetLayout
and trigger the required logic when the bottom sheet is dismissed by the user. ThesheetState
object contains anisExpanded
property that can be used to check whether the bottom sheet is expanded or not.To listen for the bottom sheet dismissal event, you can add a
DisposableEffect
composable that will execute a callback function when the bottom sheet is dismissed. TheDisposableEffect
composable is used to perform a side effect when a composable is added or removed from the composition.In this implementation, the
BottomSheetWrapper
composable takes anonDismiss
lambda as a parameter that will be called when the bottom sheet is dismissed. We use theDisposableEffect
composable to add and remove aModalBottomSheetCallback
that will listen for the bottom sheet dismissal event. When the bottom sheet is dismissed, theonDismiss
lambda is called.