我的 BottomSheet 有一个代码:
@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun BottomSheet(
modifier: Modifier = Modifier,
isBottomSheetVisible: Boolean,
sheetState: SheetState,
shape: Shape = RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp),
onDismiss: () -> Unit,
content: @Composable ColumnScope.() -> Unit
) {
if (isBottomSheetVisible) {
ModalBottomSheet(
modifier = modifier,
onDismissRequest = onDismiss,
sheetState = sheetState,
tonalElevation = 0.dp,
shape = shape,
dragHandle = null,
windowInsets = WindowInsets(0, 0, 0, 0)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(start = 16.dp, end = 16.dp, bottom = 30.dp)
.navigationBarsPadding()
.animateContentSize()
) {
Spacer(modifier = Modifier.height(10.dp))
Box(
modifier = Modifier
.size(height = 4.dp, width = 32.dp)
.clip(RoundedCornerShape(100.dp))
.background(almaColorPalette.neutral4)
.align(alignment = Alignment.CenterHorizontally)
)
Spacer(modifier = Modifier.height(15.dp))
content()
}
}
}
}
在屏幕上:
val sheetState = rememberModalBottomSheetState()
skipPartiallyExpanded
方法内部有一个rememberModalBottomSheetState
参数。在本例中,该值将为 false
,但这并不那么重要。我通常对如何计算 PartiallyExpanded
状态的高度感兴趣(这是在源代码中),因为我想为 BottomSheet 设置最小高度,该高度等于 PartiallyExpanded
的高度
当查看
ModalBottomSheet.kt
文件时,你会发现以下代码:
val fullHeight = constraints.maxHeight.toFloat()
val newAnchors = DraggableAnchors {
Hidden at fullHeight
if (
sheetSize.height > (fullHeight / 2) && !sheetState.skipPartiallyExpanded
) {
PartiallyExpanded at fullHeight / 2f // HERE
}
if (sheetSize.height != 0) {
Expanded at max(0f, fullHeight - sheetSize.height)
}
}
因此,在
PartiallyExpanded
的情况下,ModalBottomSheet
状态锚定到屏幕高度的 50%。
BottomSheetScaffold
。它有一个 sheetPeekHeight
参数,您可以在其中指定 PartiallyExpanded
状态所需的确切高度。