androidkotlinandroid-compose-button

return a value from composable function after button click


I've a bit of code that's inside a compose function that returns a "PscEntity" which takes (as you can notice) a code (String type) and money (Double type), I've tried to make the function return a new instance of PscEntity (becuase this function will be used to update instances of those entities) but It doesn't work due to: enter image description here and adding "@Button" doesn't let me return "PscEntity"

                Row() {
                    Button(onClick = {
                        return PscEntity(code = newCode.value, money = newMoney.value)
                    })
                    {
                        Text("Apply changes")
                    }
                    Button(onClick = {
                        return PscEntity(code = code, money = money)
                    })
                    {
                        Text("Disregard changes")
                    }

I thought of adding a variable that would be changed and then check the value with if statement but that doesn't seem like a good way to solve it,

any help will be appreciated

thanks :)

edit:

this function is called from ".callable { } "

Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .shadow(40.dp)
                    .clickable {
                        showAlertDialog("11111", 12.32)
                    }
            )
@Composable
fun showAlertDialog(code: String, money: Double): PscEntity {
    var newCode by remember { mutableStateOf(code) }
    var newMoney by remember { mutableStateOf(money) }


    Box(contentAlignment = Alignment.Center) {
        Column() {
            TextField(value = newCode, onValueChange = {
                newCode = it
            })

            TextField(value = newMoney.toString(), onValueChange = {
                newMoney = it.toDouble()
            })
            Row()
            {
                Button(onClick = {
                    return PscEntity(code = newCode, money = newMoney)
                })
                {
                    Text("Apply changes")
                }

                Button(onClick = {
                    return PscEntity(code = code, money = money)
                })
                {
                    Text("Disregard changes")
                }
            }
        }
    }
}

Solution

  • Your code is incorrect. Compose functions do not return a result. You should use a lambda for it. Please, check sample on codeLab