我正在使用 Jetpack Compose 开发 Android 应用程序,并且遇到了一个问题:当我与底部工作表交互(向下拖动它或使用后退按钮将其关闭)时,我的屏幕变得无响应,并且没有任何效果,直到我重新启动应用程序。这种行为不是预期的,我正在努力找出根本原因。相关详情如下:
这是底部表:
fun BottomSheet(
onDismiss: () -> Unit
) {
val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
val scope = rememberCoroutineScope()
scope.launch {
bottomSheetState.expand()
}
ModalBottomSheet(
onDismissRequest = { onDismiss() },
sheetState = bottomSheetState,
dragHandle = { BottomSheetDefaults.DragHandle() }
) {
ColorPicker()
}
}```
这是打开的地方:
Scaffold(
modifier = Modifier.fillMaxSize(),
scaffoldState = scaffoldState,
topBar = {
AppBar(
...
)
},
content = { paddingValue -\>
Box(
modifier = Modifier
.fillMaxSize()
.padding(
top = paddingValue.calculateTopPadding(),
)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
// Night Mode setting Section
...
// Font setting Section
...
// color setting Section
CustomCard(
title = "Color",
isExpandedInitially = fontColorSettingExpand,
onItemClick = {
fontColorSettingExpand = !fontColorSettingExpand
}) {
BottomSheet(
navController = navController,
onDismiss = {
//todo save
})
}```
我尝试过的:
您遇到的问题是您在函数的组合中调用
bottomSheetState.expand()
,因此当您执行“关闭”或“拖动”时,此代码会一遍又一遍地执行,从而冻结屏幕。
要解决这个问题,您可以在 LaunchedEffect 中调用
bottomSheetState.expand()
,因此它在合成之外运行。它看起来像这样:
fun BottomSheet(
onDismiss: () -> Unit
) {
val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
ModalBottomSheet(
onDismissRequest = { onDismiss() },
sheetState = bottomSheetState,
dragHandle = { BottomSheetDefaults.DragHandle() }
) {
ColorPicker()
}
LaunchedEffect(key1 = Unit, block = {
bottomSheetState.expand()
})
}