1

我有以下问题,计数器变量的状态更改不会触发 Text 组件的重新组合。关于可能是什么问题的任何想法???

@Model
class CounterState(var counter:Int = 0)

@Composable
fun CounterView(counterState: CounterState){
     Column {
         Text(text= "${counterState.counter}", 
              style=(+typography()).body1)

         Button(text ="Increment", 
                onClick = {counterState.counter++},
                style = ContainedButtonStyle())
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        app {
            CounterView(CounterState())
        }
    }
}
4

1 回答 1

-1

var counter by +state { 0 } Column { Text( text = "$counter", style = (+typography()).body1 ) Button( text = "Increment", onClick = { counter++ }, style = ContainedButtonStyle() ) }

于 2020-01-14T13:51:42.490 回答