Created
May 10, 2024 13:38
-
-
Save tfcporciuncula/4939a9802ef5e9df8ccc7f0fa5db72f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun BottomSheet( | |
isShown: Boolean, | |
modifier: Modifier = Modifier, | |
onDismissRequest: () -> Unit = {}, | |
shape: Shape = BottomSheetDefaults.ExpandedShape, | |
sheetGesturesEnabled: Boolean = true, | |
dismissOnBack: Boolean = sheetGesturesEnabled, | |
windowInsets: WindowInsets = WindowInsets(0), // This is a nice default if the app is edge to edge | |
content: @Composable ColumnScope.() -> Unit, | |
) { | |
val sheetState = rememberModalBottomSheetState( | |
skipPartiallyExpanded = true, | |
confirmValueChange = { sheetGesturesEnabled }, | |
) | |
var visible by remember { mutableStateOf(isShown) } | |
val scope = rememberCoroutineScope() | |
DisposableEffect(isShown) { | |
if (!isShown) { | |
// Only remove the sheet from composition once the dismiss animation is done | |
scope.launch { sheetState.hide() }.invokeOnCompletion { visible = false } | |
} else { | |
visible = true | |
} | |
onDispose {} | |
} | |
if (visible) { | |
ModalBottomSheet( | |
onDismissRequest = { | |
visible = false | |
onDismissRequest() | |
}, | |
modifier = modifier, | |
sheetState = sheetState, | |
shape = shape, | |
tonalElevation = 0.dp, | |
dragHandle = null, | |
windowInsets = windowInsets, | |
properties = ModalBottomSheetDefaults.properties(shouldDismissOnBackPress = dismissOnBack), | |
content = content, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment