我想动态更改底部片材折叠中相对于底部片材的内容高度,如下所示:
这是我迄今为止尝试过的: `
val bottomSheetState = rememberBottomSheetState(initialValue =BottomSheetValue.Expanded )
val bottomSheetScaffoldState= rememberBottomSheetScaffoldState(
bottomSheetState = bottomSheetState)
val heightPixels= LocalContext.current.resources.displayMetrics.heightPixels
BottomSheetScaffold(
modifier = Modifier.fillMaxSize()
sheetPeekHeight = 68.dp,
scaffoldState = bottomSheetScaffoldState,
sheetContent = {
/*
sheet content
*/
}) {
Box(modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(((bottomSheetState.offset.value)
/ (heightPixels)).let { if (it == 0f) 1f else it })
) {
/*
content
*/
}
}
但它依赖于系统高度(以像素为单位),并且每次底部工作表的高度发生变化时,其内容的框都会重新组合
我设法设置
Modifier.fillMaxHeight()
使用脚手架状态的 fraction
值,如下所示:
val bottomSheetState = rememberBottomSheetState(
initialValue = BottomSheetValue.Expanded
)
val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
bottomSheetState = bottomSheetState
)
BottomSheetScaffold(
modifier = Modifier.fillMaxSize(),
sheetPeekHeight = 68.dp,
scaffoldState = bottomSheetScaffoldState,
sheetContent = {
/*
sheet content
*/
}) {
val fraction = 1f - bottomSheetScaffoldState.currentFraction
Box(
modifier = Modifier
.padding(bottom = 68.dp)
.fillMaxHeight(fraction)
) {
/*
content
*/
}
}
currentFraction
是对其值的一个小调整,因为拖动动画的进度总是从0到1,无论是从Collapsed
到Expanded
还是从Expanded
到Collapsed
状态:
@OptIn(ExperimentalMaterialApi::class)
val BottomSheetScaffoldState.currentFraction: Float
get() {
val fraction = bottomSheetState.progress.fraction
val targetValue = bottomSheetState.targetValue
val currentValue = bottomSheetState.currentValue
return when {
currentValue == BottomSheetValue.Collapsed && targetValue == BottomSheetValue.Collapsed -> 0f
currentValue == BottomSheetValue.Expanded && targetValue == BottomSheetValue.Expanded -> 1f
currentValue == BottomSheetValue.Collapsed && targetValue == BottomSheetValue.Expanded -> fraction
else -> 1f - fraction
}
}
这样做的问题是,在展开或折叠纸张时会导致某种闪烁,并不理想。