我目前正在为我的应用程序开发主屏幕,但我遇到了一个问题.第val context = LocalContext.current
行导致错误,指出"@Composable调用只能从@Composable Function的上下文中发生".你能就如何解决这一问题提供一些指导吗?为了让您更好地理解,下面是完整的函数:
@Composable
fun HomeScreen(navController: NavController) {
// Use a Box to allow for layering of composables on top of each other
// Used to keep the content at the bottom center of the screen.
Box(
modifier = Modifier
.fillMaxSize()
.padding(16.dp), // Apply padding around the box
contentAlignment = Alignment.BottomCenter // Align content to the bottom center
) {
// Column to stack buttons vertically
Column(
// Center column contents horizontally
horizontalAlignment = Alignment.CenterHorizontally,
// Space between the column contents
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
// Button to NAVIGATE TO THE BENEFITS SCREEN
Button(
onClick = { navController.navigate("sleep_benefits") },
// Apply padding to only the top of the first button to push it upwards
modifier = Modifier.padding(bottom = 1.dp)
) {
Text(text = "Learn About Sleep Benefits")
}
// Button for USER LOGIN
Button(
onClick = { /* TODO: Add navigation to login screen here */ }
) {
Text(text = "Login to Track Sleep")
}
// New Sign Up Button
Button(
onClick = {
val context = LocalContext.current
context.startActivity(Intent(context, SignInActivity::class.java))
},
modifier = Modifier.padding(top = 8.dp)
) {
Text(text = "Sign Up")
}
}
}
}
我试着看这个:Error: "@Composable invocations can only happen from the context of a @Composable function"