gpt4 book ai didi

android - 我可以为可组合函数切换状态变量吗?

转载 作者:行者123 更新时间:2023-12-05 00:13:19 25 4
gpt4 key购买 nike

我有一个简单的可组合函数,如下所示

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var isGrayScale by remember { mutableStateOf(false) }
val colorChoice by remember(isGrayScale) {
mutableStateOf(
if (isGrayScale)
ColorChoice(onColor = Color.White, offColor = Color.Black)
else
ColorChoice(onColor = Color.Green, offColor = Color.Red)
)
}
Box(modifier = Modifier.fillMaxSize().background(colorChoice.color))
Button(
modifier = Modifier.padding(64.dp).fillMaxSize(),
onClick = { colorChoice.toggle() }) {
Text(
text = if (colorChoice.isOn) "On" else "Off",
color = colorChoice.color,
fontSize = 48.sp
)
}
Switch(
modifier = Modifier.padding(64.dp).fillMaxWidth(),
checked = isGrayScale,
onCheckedChange = {isGrayScale = !isGrayScale}
)
}
}

class ColorChoice(
private val onColor: Color,
private val offColor: Color
) {
var isOn by mutableStateOf(false)
var color by mutableStateOf(offColor)
fun toggle() {
isOn = !isOn
color = if(isOn) {
onColor
} else {
offColor
}
}
}
}

开关会将可组合函数引用更改为不同的状态变量。切换后,当状态变量发生变化时,可组合函数不再重新组合。

是否不支持更改可组合函数的状态变量?或者我做错了什么。

最佳答案

经过一些调查,显然,问题出在 onClick = { colorChoice.toggle() } 中,当 colorChoice 更改时,lambda 仍然存储旧的 `colorChoice。

要更正它,请将 onClick = { colorChoice.toggle() } 替换为 onClick = colorChoice::toggle。这确保 onClick 将获得最新的 colorChoice

完整代码如下

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var isGrayScale by remember { mutableStateOf(false) }
val colorChoice by remember(isGrayScale) {
mutableStateOf(
if (isGrayScale)
ColorChoice(onColor = Color.White, offColor = Color.Black)
else
ColorChoice(onColor = Color.Green, offColor = Color.Red)
)
}
Box(modifier = Modifier.fillMaxSize().background(colorChoice.color))
Button(
modifier = Modifier.padding(64.dp).fillMaxSize(),
onClick = colorChoice::toggle) {
Text(
text = if (colorChoice.isOn) "On" else "Off",
color = colorChoice.color,
fontSize = 48.sp
)
}
Switch(
modifier = Modifier.padding(64.dp).fillMaxWidth(),
checked = isGrayScale,
onCheckedChange = {isGrayScale = !isGrayScale}
)
}
}

class ColorChoice(
private val onColor: Color,
private val offColor: Color
) {
var isOn by mutableStateOf(false)
var color by mutableStateOf(offColor)
fun toggle() {
isOn = !isOn
color = if(isOn) {
onColor
} else {
offColor
}
}
}
}

关于android - 我可以为可组合函数切换状态变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65058766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com