Android Compose:通过拖动或后按关闭底部工作表时屏幕冻结

问题描述 投票:0回答:1

我正在使用 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
                                })

}```

我尝试过的:

  • 检查了我的状态管理以确保其正确。
  • 检查了我的协程使用情况是否存在任何潜在的阻塞问题。
  • 已验证我正在使用适当的 Compose 组件和 ScaffoldState。
android kotlin android-jetpack-compose bottom-sheet jetpack-compose-modalbottomsheet
1个回答
0
投票

您遇到的问题是您在函数的组合中调用

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()
  })
    
}
© www.soinside.com 2019 - 2024. All rights reserved.