Composable, public var

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?

The onClick method of a button is intended to signal that the button has been clicked so that the app can update some state. You can’t render any composables inside onClick, and hence it is not @Composable

Ok, so how I can do that without double my code?
New class?

Does this need to be a composable at all? It doesn’t render anything, so I think you can just have it be a normal function.

Ok, but still i dont know how to make it in one fun.
I cant make var public with remember mutableStateOf
But when i need to make fun with code like that:

            if (answer != null) {
                if (sumRandom == answer) {
                    checkResult = "Zgadza się, $random1 + $random2 = $sumRandom"
                    points++
                    question++
                } else {
                    checkResult = "Złe rozwiązanie, $random1 + $random2 = $sumRandom"
                    question++
                }
                random1 = (0..10).random()
                random2 = (0..10).random()
            } else {
                checkResult = "Niepoprawny format liczby"
                question++
            }
            text = ""

I need to call and send a lot of var, like

fun countThat(random1: Int, random2: Int, aanswer: Int, checkResult: String, points: Int, question: Int): Int{
}

I know, theres beater solution, but i dont know how to do that :wink:

I’m not very familiar with Compose and I can’t check this solution right now, but did you try creating a local function?

    ...
    var points by remember { mutableStateOf(0) }
    val sumRandom = random1 + random2

    fun verifyAnswer(...) {
        ...
    }

    Column(Modifier.padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally) {
        ...
        Button(modifier = Modifier.padding(16.dp), onClick = { verifyAnswer(...) }

P.S.
“Rozwiąż” is spelled like this :wink:

[Thank You meme]
Why didn’t I think of this idea?

And yes- iths ‘ż’ at end :slight_smile:

Las question about that code.
I use random numbers, but when i change it to ex.
var random1 by remember { mutableStateOf((2..5).random()) }
why still randomly selects 0, 9 and other numbers outside the range 2-5?

Edit: nvm, i found error :slight_smile: