Is it possible to set the value of a FloatingActionButton
's onClick
inside of a Scaffold
from a screen composable from a NavHost
, which is also inside the Scaffold
? Here in my example, I want to be able to change the FAB's onClick to do some calculations from a value (name
) inside the screen composable, without hoisting the value to the composable where the Scaffold
is located (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() }
}