簡體   English   中英

使用 bottomsheetscaffold 中的 bottom sheet 調整內容高度

[英]adjust content height with bottom sheet in bottomsheetscaffold

我想相對於 bottomsheetscoffold 中的 bottomsheet 動態更改內容的高度,如下所示:

底板折疊 展開底頁

到目前為止,這是我嘗試過的:`


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,無論它是從CollapsedExpanded還是從ExpandedCollapsed狀態:

@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
    }
}

這樣做的問題是它在展開或折疊工作表時會導致某種閃爍,這並不理想。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM