【发布时间】:2022-12-02 21:54:36
【问题描述】:
出于可读性目的,我想在另一个函数中提取 NavigationBar 可组合项。与 PreviousButton 相同。因此我想将索引的 mutableState 传递给这些函数。但是将索引作为参数传递是行不通的,因为我无法更新状态。我能做些什么?
@Composable
fun MyChickensScreen(){
val art: List<Art> = Datasource().loadArt()
var index: Int by remember { mutableStateOf(0) }
// IDE suggests making index a val,
// but I want to update the state in another composable.
//...
NavigationBar(index = index)
}
}
//NavigationBar passes index to the PreviousButton Composable
@Composable
private fun PreviousButton(index: Int) {
Button(
onClick = { index = handlePrevClick(index) }, //Error: Val cannot be reassigned for index
) {
//...
}
}
【问题讨论】:
标签: kotlin android-jetpack-compose