我正在检查一个标记值,该值由一个更高级别的组件传递给我的可组合组件,但返回的值始终是初始值...
这是我的组件:
@Composable
fun Test(enabled: Boolean) {
var expanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
if (enabled) expanded = !expanded
println("enabled is $enabled") //always prints the initial value of enabled
}
) {
OutlinedTextField(
value = "value",
onValueChange = { },
modifier = Modifier.fillMaxWidth(),
enabled = enabled,
readOnly = true,
label = { Text("Label") },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
},
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
DropdownMenuItem(
onClick = {
expanded = false
},
text = {
Text("Content")
}
)
}
}
}
onExpandedChange
中的程式码会忽略enabled
的目前值。
例如,如果我运行下面的代码,我可以看到OutlinedTextField
在2秒后更改为启用状态,但onExpandedChange
将使用初始(旧)值enabled。
Surface(
color = MaterialTheme.colorScheme.background
) {
var flag by remember { mutableStateOf(false) }
println("flag is $flag")
LaunchedEffect(Unit) {
delay(2000)
flag = !flag
println("flag is now $flag")
}
Test(flag)
}
我做错了什么?
编辑:如果我这样做的话,它会工作得很好:
@Composable
fun Test(enabled: Boolean) {
var expanded by remember { mutableStateOf(false) }
var enabledState by remember {
mutableStateOf(enabled)
}
LaunchedEffect(enabled) {
enabledState = enabled
}
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
if (enabledState) expanded = !expanded
}
) {
OutlinedTextField(
value = "value",
onValueChange = { },
modifier = Modifier.fillMaxWidth(),
enabled = enabled,
readOnly = true,
label = { Text("Label") },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
},
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
DropdownMenuItem(
onClick = {
expanded = false
},
text = {
Text("Content")
}
)
}
}
}
但如果我这么做的话就不管用了
@Composable
fun Test(enabled: Boolean) {
var expanded by remember { mutableStateOf(false) }
val enabledState by remember(enabled) {
mutableStateOf(enabled)
}
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
if (enabledState) expanded = !expanded
}
) {
OutlinedTextField(
value = "value",
onValueChange = { },
modifier = Modifier.fillMaxWidth(),
enabled = enabled,
readOnly = true,
label = { Text("Label") },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
},
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
DropdownMenuItem(
onClick = {
expanded = false
},
text = {
Text("Content")
}
)
}
}
}
我也不知道为什么。
1条答案
按热度按时间hi3rlvi21#
重新组合不是忽略已更新的函数参数,而是忽略
onExpandedChange
的闭包。即使您通过更改remember
键重新示例化MutableState
,它也会保留MutableState
的上一个示例。因此,您应该做的是更新MutableState
的当前示例的值,而不是您应该按照Bruno建议使用rememberUpdatedState,或者使用
这就是
rememberUpdatedState
实现Difference between remember and rememberUpdatedState in Jetpack Compose?