Tolriq
05/14/2022, 5:56 PMChris Sinco [G]
05/14/2022, 6:29 PMTolriq
05/14/2022, 6:33 PMTolriq
05/14/2022, 6:34 PMChris Sinco [G]
05/14/2022, 6:35 PMTolriq
05/14/2022, 6:43 PMTolriq
05/14/2022, 6:45 PMTolriq
05/14/2022, 6:46 PMTolriq
05/15/2022, 1:09 PMChris Sinco [G]
05/15/2022, 3:25 PMTolriq
05/15/2022, 3:27 PM@Composable
fun ModalBottomSheetLayout(
sheetContent: @Composable ColumnScope.() -> Unit,
modifier: Modifier = Modifier,
sheetState: ModalBottomSheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden),
sheetShape: Shape = MaterialTheme.shapes.large,
sheetElevation: Dp = ModalBottomSheetDefaults.Elevation,
sheetBackgroundColor: Color = MaterialTheme.colorScheme.surface,
sheetContentColor: Color = contentColorFor(sheetBackgroundColor),
scrimColor: Color = ModalBottomSheetDefaults.scrimColor,
content: @Composable () -> Unit
) {
val scope = rememberCoroutineScope()
BoxWithConstraints(modifier) {
val fullHeight = constraints.maxHeight.toFloat()
val sheetHeightState = remember { mutableStateOf<Float?>(null) }
Box(Modifier.fillMaxSize()) {
content()
Scrim(
color = scrimColor,
onDismiss = {
if (sheetState.confirmStateChange(ModalBottomSheetValue.Hidden)) {
scope.launch { sheetState.hide() }
}
},
visible = sheetState.targetValue != ModalBottomSheetValue.Hidden
)
}
Column(
modifier = Modifier
.widthIn(max = 640.dp)
.fillMaxWidth()
.align(BottomCenter)
.nestedScroll(sheetState.nestedScrollConnection)
.offset {
val y = if (sheetState.anchors.isEmpty()) {
// if we don't know our anchors yet, render the sheet as hidden
fullHeight.roundToInt()
} else {
// if we do know our anchors, respect them
sheetState.offset.value.roundToInt()
}
IntOffset(0, y)
}
.bottomSheetSwipeable(sheetState, fullHeight, sheetHeightState)
.onGloballyPositioned {
sheetHeightState.value = it.size.height.toFloat()
}
.semantics {
if (sheetState.isVisible) {
dismiss {
if (sheetState.confirmStateChange(ModalBottomSheetValue.Hidden)) {
scope.launch { sheetState.hide() }
}
true
}
if (sheetState.currentValue == ModalBottomSheetValue.HalfExpanded) {
expand {
if (sheetState.confirmStateChange(ModalBottomSheetValue.Expanded)) {
scope.launch { sheetState.expand() }
}
true
}
} else if (sheetState.hasHalfExpandedState) {
collapse {
if (sheetState.confirmStateChange(ModalBottomSheetValue.HalfExpanded)) {
scope.launch { sheetState.halfExpand() }
}
true
}
}
}
}
) {
Spacer(modifier = Modifier.statusBarsPadding())
Surface(
modifier = Modifier.fillMaxWidth(),
shape = sheetShape,
tonalElevation = sheetElevation,
color = sheetBackgroundColor,
contentColor = sheetContentColor
) {
Column(content = sheetContent)
}
}
}
}
Tolriq
05/15/2022, 3:28 PMTolriq
05/15/2022, 3:28 PMTolriq
05/15/2022, 3:29 PM