是否可以从 a 可组合的屏幕中设置 a 在FloatingActionButton
aonClick
内部的值,该屏幕也在 a 内部?在我的示例中,我希望能够更改 FAB 的 onClick 以从屏幕可组合内的值 ( ) 进行一些计算,而无需将值提升到位于可组合( ) 的位置。Scaffold
NavHost
Scaffold
name
Scaffold
MyActivity()
@Composable
fun MyActivity() {
val navController = rememberNavController()
Scaffold(
floatingActionButton = {
FloatingActionButton(onClick = { /* set this lambda from Screen1 */ }) { ... }
}
) { paddingValues ->
NavHost(
navController = navController,
startDestination = Screen.Screen1.route,
modifier = Modifier.padding(paddingValues)
) {
composable(route = Screen.Screen1.route) {
Screen1()
}
...
}
}
}
@Composable
fun Screen1() {
var name by remember { mutableStateOf(TextFieldValue("") }
TextField(value = name, onValueChange = { name = it })
// Set FAB onClick to do some calculations on name without hoisting the variable
setFabOnClick { name.calculate() }
}