当我在 foreach 中创建一个 mutableStateOf (true) 来隐藏一个 Row 并单击第一项会删除所有选项时,我只想删除里面有按钮的行
我试图创建一个 MutableState 数组 val visible = arrayOf <MutableState<Boolean>> ()
但它给出了错误,在 foreach 中执行此操作的最佳方法是什么
我该如何解决这个错误
我的代码:
@Composable
private fun Content(){
Column(modifier=Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Top){
//val visibles= arrayOf<MutableState<Boolean>>()
if(i.isNotEmpty()){
i.forEachIndexed { index, item ->
var quantidades by remember{ mutableStateOf(TextFieldValue(item.quantidade.toString())) }
var visible by remember {
mutableStateOf(true)
}
if(visible) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(bottom = 8.dp)
.fillMaxWidth(100f)
) {
CoilImage(
data = item.photo,
contentDescription = item.name,
fadeIn = true,
loading = {
Box(Modifier.fillMaxSize()) {
CircularProgressIndicator(Modifier.align(Alignment.Center))
}
},
modifier = Modifier
.size(100.dp),
error = {
Image(
painterResource(R.drawable.ic_launcher_background),
contentDescription = "Error"
)
}
)
Text(
text = item.name,
fontSize = 15.sp,
modifier = Modifier.padding(end = 20.dp)
)
// Text(text=item.count.toString())
// Text(text=item.id.toString())
TextField(
value = quantidades,
onValueChange = {
quantidades = it
//_addUserState.value = _addUserState.value?.copy(name = it)
},
shape = RoundedCornerShape(8.dp),
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent
),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
modifier = Modifier
.width(50.dp)
.height(50.dp)
)
Column(
modifier = Modifier
.fillMaxWidth(100f)
.padding(5.dp), horizontalAlignment = Alignment.End
) {
Button(
onClick = { visible=onVisible(visible,item) },
modifier = Modifier.padding(end = 20.dp)
) {
Icon(
Icons.Default.Delete,
stringResource(id = R.string.deletar)
)
}
}
}
}
}
}else{
Text(text= stringResource(id = R.string.sem_carrinho), fontSize = 15.sp)
}
}
}
private fun onVisible(visible: Boolean, item:ShoppingCart):Boolean{
counter.value--
i.remove(item)
return !visible
}
答案 0 :(得分:1)
我复制并粘贴了您的代码,但无法运行它。
但是您是否尝试过为每个 HttpServlet.doOptions
状态使用一个密钥?
visible