Is there any way to have public static variable with remember state in jetpack compose (kotlin)

huangapple go评论68阅读模式
英文:

Is there any way to have public static variable with remember state in jetpack compose (kotlin)

问题

@Composable
fun Main() {
    var cardsList: List<String> by remember { mutableStateOf(listOf("Test1", "Test2")) }

    Column(
        Cards(cardsList)
    )
}

@Composable
fun Cards(cardsList: List<String>) {
    for (item in cardsList) {
        Button(
            onClick = {
                // Modify the list as needed
            }
        )
    }
}

这是您的代码,我已经对其进行了一些修改,以便在Cards函数中接受cardsList作为参数。这样,当您修改cardsList时,Cards函数中的UI将自动刷新。在Main函数中,我使用remember来管理cardsList的状态,以确保在重新组合时保留其状态。您可以在Cards函数中的onClick处理程序中修改cardsList以执行所需的操作。

英文:
@Composable
fun Main() {
    var cardsList: List&lt;String&gt; by remember { mutableStateOf( listOf(&quot;Test1&quot;,&quot;Test2&quot;) ) }

    Column(
        Cards()
    )
}

@Composable
fun Cards() {
    for(item in cardsList) {
        Button(
            onClick = {cardsList -= item}
        )
    }
}

This is my code, I need to re-assign the 'cardsList' out side of main function, also if 'cardsList' changed i want UI and all functions that use 'cardsList' as parameter refresh too

can a class help me? maybe i put my list in a class

I tried code below but didnt work

var cardsList: List&lt;String&gt; = listOf(&quot;Test1&quot;,&quot;Test2&quot;)

@Composable
fun Main() {
    Column(
        Cards()
    )
}

@Composable
fun Cards() {
    for(item in cardsList) {
        Button(
            onClick = {cardsList -= item}
        )
    }
}

答案1

得分: 2

这是使用state hoisting解决的:

@Composable fun Main() {
    var cardsList: List<String> by remember { mutableStateOf(listOf("Test1", "Test2")) }
    Cards(
        cards = cardsList,
        onCardClick = { card -> cardsList -= card }
    )
}

@Composable fun Cards(
    cards: List<String>,
    onCardClick: (String) -> Unit,
) {
    for (item in cards) {
        Button(
            onClick = { onCardClick(item) }
        ) { Text(text = item) }
    }
}

如果您有更多类似的变量或更喜欢这种方式,您可以创建一个状态类:

class MainState {
   var cardsList by mutableStateOf(listOf("Test1", "Test2"))
       private set

   fun removeCard(card: String) {
       cardsList -= card
   }
}

@Composable fun Main() {
    val state = remember { MainState() }
    Cards(state = state)
}

@Composable fun Cards(state: MainState) {
    for (item in state.cardsList) {
        Button(
            onClick = { state.removeCard(item) }
        ) { Text(text = item) }
    }
}
英文:

This is solved with state hoisting:

@Composable fun Main() {
    var cardsList: List&lt;String&gt; by remember { mutableStateOf(listOf(&quot;Test1&quot;, &quot;Test2&quot;)) }
    Cards(
        cards = cardsList,
        onCardClick = { card -&gt; cardsList -= card }
    )
}

@Composable fun Cards(
    cards: List&lt;String&gt;
    onCardClick: (String) -&gt; Unit,
) {
    for(item in cards) {
        Button(
            onClick = { onCardClick(item) }
        ) { Text(text = item) }
    }
}

If you have more such variables or you just prefer it that way, you can create a state class:

class MainState {
   var cardsList by mutableStateOf(listOf(&quot;Test1&quot;, &quot;Test2&quot;))
       private set

   fun removeCard(card: String) {
       cardsList -= card
   }
}

@Composable fun Main() {
    val state = remember { MainState() }
    Cards(state = state)
}

@Composable fun Cards(state: MainState) {
    for(item in state.cardsList) {
        Button(
            onClick = { state.removeCard(item) }
        ) { Text(text = item) }
    }
}

huangapple
  • 本文由 发表于 2023年6月29日 18:26:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定