This post has been edited in order to provide more context to my question. The "final" version, which I can develop with my current knowledge and the solution that I can understand better, is this. I would appreciate it if you could analyze it and give me the OK. I am aware that there may be some better solutions, but this one is mine ;)
var myTextQuantityState by remember { mutableStateOf(1) }
Text(myTextQuantityState)
Button(
Modifier.constrainAs(buttonAdd) {
end.linkTo(parent.end)
bottom.linkTo(buttonOrder.top)
top.linkTo(imageIceCream.bottom)
},
myTextQuantityState
)
{ myTextQuantityState = it }
}
}
}
@Composable
fun Text(text: Int) {
Text(
text = text.toString(),
)
}
@Composable
fun Button(
modifier: Modifier,
currentTextQuantityState: Int,
increaseIceCreamQuantity: (Int) -> Unit
) {
Button(
onClick = {
increaseIceCreamQuantity(currentTextQuantityState + 1)
},
modifier = modifier
) {
}
}
I'm 2 months into Android so I can't tell you the "best" way. But here is how I would do it.
The reason I would do it this way is because I would prefer to have the lambda in a position to directly access the quantity without having to pass it as a variable like you are doing with the
increaseIceCreamQuantityfunctionHere is how it would work if the state and button were in different composables
Based on your current code. The solution is still similar to the function I have above. The
currentTextQuantityStatevalue you have as a parameter in your Button composable is actually a reference to the original one so you can still pass a lambda that doesn't need an input. Here is an edit of your code to clarify what I mean. I have put...to skip code I didn't change