我们如何在喷气背包中使用底片



我开始将我的项目迁移到jetpack compose,我现在正在学习jetpack compose,我想在我的项目中使用底部表单,我在互联网上搜索使用底部表单。我找到了一些代码,我在我的应用程序中使用它,一切看起来都很好,但当我运行我的应用时,它崩溃了,我不确定我错在哪里了?还有其他解决方案吗?

class MyActivity : ComponentActivity() {
@ExperimentalMaterialApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ModalBottomSheetLayoutScreen()
}
}
}
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ModalBottomSheetLayoutScreen() {
val modalBottomSheetState = rememberModalBottomSheetState(initialValue = 
ModalBottomSheetValue.Hidden)
val scope = rememberCoroutineScope()
ModalBottomSheetLayout(
sheetContent = {
},
sheetState = modalBottomSheetState,
sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
sheetBackgroundColor = colorResource(id = R.color.white),
// scrimColor = Color.Red,  // Color for the fade background when you open/close the drawer
) {
Scaffold(
backgroundColor = colorResource(id = R.color.white)
) {
MyScreen(scope = scope, state = modalBottomSheetState)
}
}
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MyScreen(
scope: CoroutineScope, state: ModalBottomSheetState
) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {

Column(
modifier = Modifier
.width(170.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {

Text(
text = "click",
modifier = Modifier
.clickable {
scope.launch {
state.show()
}
}  
)}} }

@Preview(showBackground = true)
@Composable
fun ModalBottomSheetLayoutScreenPreview() {
ModalBottomSheetLayoutScreen()
}

它崩溃是因为sheetContent为空。我已经报告了这个错误。在一个真正的应用程序中,你应该有一些内容,否则你根本不需要ModalBottomSheetLayout

sheetContent中指定任何视图都可以解决问题。

相关内容

最新更新