0

Why does this not recompose, despite state changing.

@Composable
fun IngredientsScreen() {

    var numberOfIngredients by remember {mutableStateOf(1)}

    Box(

    ){
        Column(
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.Start,
            modifier = Modifier
                .verticalScroll(rememberScrollState())
        ){
            for (i in 0 until numberOfIngredients){
                IngredientInputBox(
                    onClick = {
                        numberOfIngredients++
                    }
                )
            }
        }
    }
}

IngredientInputBox() is just a composable that contains a text field and a "+" symbol that is clickable, ideally when the symbol is pressed, a new text field would appear beneath it

@Composable
fun IngredientInputBox(
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    var text by remember { mutableStateOf("") }

    Row(
        modifier = Modifier
            .padding(16.dp)
    ){
        TextField(
            value = text,
            onValueChange = { newText ->
                text = newText
            },
            label = {Text(text = stringResource(R.string.addNewIngredient))},
            modifier = Modifier
                .fillMaxWidth(0.3F)
                .weight(1f)
        )
        Text(
            text = "+",
            modifier = Modifier
                .clickable {onClick}
        )
    }
}
4
  • The issue is likely inside IngredientInputBox(). Can you post that code? By the way, for simpler code, you can replace for (i in 0 until numberOfIngredients) with repeat(numberOfIngredients).
    – Tenfour04
    Commented Mar 6, 2023 at 19:28
  • As @Tenfour04 points out, this works if, for example, IngredientInputBox is just a Text with a clickable event handler. Can provide the implementation of IngredientInputBox?
    – chuckj
    Commented Mar 7, 2023 at 1:12
  • @Tenfour04 I added the code you requested, however there seems to be an answer so I'll check that.
    – lvm12
    Commented Mar 7, 2023 at 18:32
  • @chuckj I added the code you requested, however there seems to be an answer so I'll check that.
    – lvm12
    Commented Mar 7, 2023 at 18:33

1 Answer 1

0

Here's what's wrong with your implementation. This code:

modifier = Modifier
    .clickable {onClick}

Makes it so your button, when clicked, does absolutely nothing. The code in your lambda is what is called when the button is clicked, and it just declares the name of the click listener without doing anything with it.

You could fix it using

modifier = Modifier
    .clickable {onClick()}

or

modifier = Modifier
    .clickable(onClick = onClick)
2
  • Oh yeah... I'm just stupid
    – lvm12
    Commented Mar 7, 2023 at 18:48
  • 1
    Small note: you need to specify the name of the parameter since the onClick is the last parameter Modifier.clickable(onClick = onClick).
    – gpunto
    Commented Mar 7, 2023 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.