android 底页:未解析的引用:分数

thtygnil  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(97)

我在sheetState.progress.fraction中遇到了问题。
它本来工作得很好,但现在没有解决。不知道为什么?

Text(
   text = "Toggle Sheet fraction ${sheetState.progress.fraction}",
   color = Color.White
)

这是我一直在做的事

@ExperimentalMaterialApi
class BottomSheetActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyComposableTheme {
                val sheetState = rememberBottomSheetState(
                    initialValue = BottomSheetValue.Collapsed
                    ,animationSpec = SpringSpec(
                        dampingRatio = Spring.DampingRatioHighBouncy
                    )
                )
                val scaffoldState = rememberBottomSheetScaffoldState(
                    bottomSheetState = sheetState
                )
                val scope = rememberCoroutineScope()
                BottomSheetScaffold(
                    scaffoldState = scaffoldState,
                    sheetContent = {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(300.dp),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(
                                text = "BottomSheet",
                                fontSize = 60.sp,
                                color = Color.White
                            )
                        }
                    },
                    sheetBackgroundColor = Color.Black,
                    sheetPeekHeight = 0.dp
                ) {
                    Box(
                        modifier = Modifier.fillMaxSize(),
                        contentAlignment = Alignment.Center
                    ) {
                        Button(onClick = {
                            scope.launch {
                                if (sheetState.isCollapsed) {
                                    sheetState.expand()
                                } else {
                                    sheetState.collapse()
                                }
                            }
                        }) {
                            Text(
                                text = "Toggle Sheet fraction ${sheetState.progress.fraction}",
                                color = Color.White
                            )
                        }
                    }
                }
            }
        }
    }
}

有谁能帮我解决这个问题吗?

qhhrdooz

qhhrdooz1#

你可以使用sheetState.currentFraction来获取底部工作表位置的当前分数

这样编辑代码:

Button(onClick = {
    scope.launch {
        if (sheetState.isCollapsed) {
            sheetState.expand()
        } else {
            sheetState.collapse()
        }
    }
}) {
    Text(
        text = "Toggle Sheet fraction ${sheetState.currentValue}",
        color = Color.White
    )
}

通过使用sheetState.currentFraction而不是sheetState.progress.fraction,您应该能够访问底部工作表位置的当前分数。

相关问题