Home > Mobile >  Nested Column doesn't Recompose
Nested Column doesn't Recompose

Time:02-02

I have nested column, when I click add button the goal is add another text field and when I click delete button (which still hidden because first index) the goal is remove its text field. It seems doesn't recompose but the list size is changed.

I have tried using LazyColumn and foreach inside leads to force close, still no luck. Any help appreciated, thank you!

Preview of the screen

My current code :

@Composable
fun ProblemScreen() {
    val list = remember {
        mutableStateListOf<MutableList<String>>()
    }

    LaunchedEffect(key1 = Unit, block = {
        repeat(3) {
            val listDesc = mutableListOf<String>()
            repeat(1) {
                listDesc.add("")
            }
            list.add(listDesc)
        }
    })

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colors.background)
    ) {
        list.forEachIndexed { indexParent, parent ->
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp, horizontal = 16.dp)
            ) {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(text = "Parent ${indexParent   1}", fontSize = 18.sp)
                    Spacer(modifier = Modifier.weight(1f))
                    Button(onClick = {
                        parent.add("")
                        println("PARENT SIZE : ${parent.size}")
                    }) {
                        Icon(imageVector = Icons.Rounded.Add, contentDescription = "Add")
                    }
                }
                parent.forEachIndexed { indexChild, child ->
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 16.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        TextField(
                            value = "",
                            onValueChange = {

                            },
                            colors = TextFieldDefaults.textFieldColors(),
                            maxLines = 1,
                            modifier = Modifier.weight(1f)
                        )
                        Spacer(modifier = Modifier.width(16.dp))
                        Button(
                            onClick = {
                                parent.removeAt(indexChild)
                            },
                            modifier = Modifier.alpha(if (indexChild != 0) 1f else 0f)
                        ) {
                            Icon(
                                imageVector = Icons.Rounded.Delete,
                                contentDescription = "Delete"
                            )
                        }
                    }
                }
            }
        }
    }
}

CodePudding user response:

As said in docs, mutable objects that are not observable, such as mutableListOf(), are not observable by Compose and don't trigger a recomposition.

So instead of

val list = remember {
    mutableStateListOf<MutableList<String>>()
}

Use:

val list = remember {
    mutableStateListOf<List<String>>()
}

And when you need to update the List, create a new one:

//parent.add("")
list[indexParent] = parent   ""
  • Related