Hi.
@Composable
fun MainView() {
var random1 by remember { mutableStateOf((0..10).random()) }
var random2 by remember { mutableStateOf((0..10).random()) }
var text by remember { mutableStateOf("") }
var checkResult by remember { mutableStateOf("") }
var question by remember { mutableStateOf(0) }
var points by remember { mutableStateOf(0) }
val sumRandom = random1 + random2
Column(Modifier.padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally) {
Spacer(modifier = Modifier.height(16.dp))
Text(modifier = Modifier.padding(16.dp), text = "Rozwiąrz zadanie")
Text(modifier = Modifier.padding(16.dp), text = "$random1 + $random2")
OutlinedTextField(
value = text,
onValueChange = { text = it },
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
), keyboardActions = KeyboardActions(onDone = { val answer = text.toIntOrNull()
if (answer != null) {
if (sumRandom == answer) {
checkResult = "Correct, $random1 + $random2 = $sumRandom"
points++
question++
} else {
checkResult = "Fail, $random1 + $random2 = $sumRandom"
question++
}
random1 = (0..10).random()
random2 = (0..10).random()
} else {
checkResult = "Its not a number"
question++
}
text = ""})
)
Button(modifier = Modifier.padding(16.dp), onClick = {
val answer = text.toIntOrNull()
if (answer != null) {
if (sumRandom == answer) {
checkResult = "Correct, $random1 + $random2 = $sumRandom"
points++
question++
} else {
checkResult = "Fail, $random1 + $random2 = $sumRandom"
question++
}
random1 = (0..10).random()
random2 = (0..10).random()
} else {
checkResult = "Its not a number"
question++
}
text = ""
}
// End Button
) {
Text(text = "Check")
}
Text(text = "Question nr: $question")
Text(text = "Points: $points")
Text(text = checkResult)
}
}
I have code like that, but i want to make same function from keyboard and from button.
I try to make something like that:
// ...
Button(modifier = Modifier.padding(16.dp), onClick = {
CountThat(nr1 = 1, nr2 = 2)
// ...
@Composable
fun CountThat(nr1: Int, nr2: Int): Int{
return nr1+nr2
}
But i have an error in Button: @Composable invocations can only happen from the context of a @Composable function
But IT IS composable.
Wher i am wrong?