该行中的错误
inputs = inputs + name
@Composable
fun CreateBar(){
var name by remember { mutableStateOf("") }
val inputs = remember { mutableStateListOf<String>() }
Column {
Row( modifier = Modifier
.fillMaxWidth()
.padding(20.dp , 60.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically)
{
TextField(
value = name,
onValueChange = { text : String ->
name = text
}
)
Button(
onClick = {
inputs = inputs + name
}
) {
Icon(
imageVector = Icons.Default.Search,
contentDescription = null,
modifier = Modifier
.height(45.dp)
)
}
}
LazyColumn {
items(inputs){ input ->
Text("this is"+input)
}
}
}
}
检查MutableStateList的正确导入 - >
import androidx.compose.runtime.mutableStateListOf
如果问题持续存在,那么您可以使用此
var inputsMut by remember { mutableStateOf<List<String>>(emptyList()) }
inputsMut = inputsMut + name //Insert the name on button click
在现有代码中,您正在以错误的方式更新“ mutableStateListof”。 MutableStateList已经可以变形,因此您无需重新分配它,只需使用“ .ADD(name)”函数对其进行修改。
这是我方面的最终修改代码=>
@Composable
fun CreateBar(
modifier: Modifier = Modifier
){
var name by remember { mutableStateOf("") }
val inputs = remember { mutableStateListOf<String>() }
var inputsMut by remember { mutableStateOf<List<String>>(emptyList()) }
//Use lazy column as it will minimizes recomposition and also good for this type of scenarios
LazyColumn (
modifier = modifier
){
item {
Row( modifier = Modifier
.fillMaxWidth()
.padding(20.dp , 60.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically)
{
TextField(
value = name,
onValueChange = { text : String ->
name = text
}
)
Button(
onClick = {
inputs.add(name) // Use this to modify the input list
// inputsMut = inputsMut + name // Use this to modify the input list, if you are using mutableStateOf
name = "" // After adding change the name to empty
}
) {
androidx.compose.material3.Icon(
imageVector = Icons.Default.Search,
contentDescription = null,
modifier = Modifier
.height(45.dp)
)
}
}
}
//Use this to show the input list, if you are using mutableStateOf
// items(inputsMut){ input ->
// Text("this is$input")
// }
items(inputs){ input ->
Text("this is$input")
}
}
}
我希望我的答案对您有帮助。随时提出与之相关的新问题,或者就此问题进行任何进一步的澄清。 谢谢