如何在更改TextField时更改Text()。

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

how to change Text() when changing TextField

问题

MyComposable:

  1. class Cycle(val title: String)
  2. ViewModel 接收到 Flow<Cycle>
  3. @Composable
  4. fun MyColumn() {
  5. Column() {
  6. val cycleVM: CycleVM = viewmodel()
  7. val cycle by cycleVM.cycle.collectAsState()
  8. MyText(cycle)
  9. MyTextField(cycle)
  10. }
  11. }

MyTextField:

  1. @Composable
  2. fun MyTextField(cycle: Cycle) {
  3. var title by remember {
  4. mutableStateOf(cycle.title!!)
  5. }
  6. TextField(
  7. modifier = modifierValue.fillMaxWidth(),
  8. colors = TextFieldDefaults.textFieldColors(
  9. backgroundColor = colorResource(R.color.colorBackgroundCardView)
  10. ),
  11. value = title,
  12. onValueChange = {
  13. title = it
  14. cycle.title = it
  15. }
  16. )
  17. }

MyText:

  1. @Composable
  2. fun MyTextField(cycle: Cycle) {
  3. Text(
  4. modifier = modifier1,
  5. style = MyTextTitleLabel20StrokeText,
  6. text = if (cycle.title.isBlank()) {
  7. stringResource(id = R.string.title_cycle_hint)
  8. } else {
  9. cycle.title
  10. }
  11. )
  12. }
英文:

how to change Text() when changing TextField
MyComposable:

class Cycle(val title: String)

from ViewModel recived Flow<Cycle>

  1. @Composable
  2. fun MyColumn(){
  3. Column(){
  4. val cycleVM : CycleVM = viewmodel()
  5. val cycle by cycleVM.cycle.collectAsState()
  6. MyText(cycle)
  7. MyTextField(cycle)
  8. }

MyTextField :

  1. @Composable
  2. fun MyTextField(cycle: Cycle) {
  3. var title by remember {
  4. mutableStateOf(cycle.title!!)
  5. }
  6. TextField(modifier = modifierValue.fillMaxWidth(),
  7. colors = TextFieldDefaults.textFieldColors(
  8. backgroundColor = colorResource(R.color.colorBackgroundCardView)
  9. ),
  10. value = title,
  11. onValueChange = {
  12. title = it
  13. cycle.title = it
  14. }
  15. }
  16. )
  17. }

MyText:

@Composable
fun MyTextField(cycle: Cycle) {

  1. Text(
  2. modifier = modifier1,
  3. style = MyTextTitleLabel20StrokeText,
  4. text = if (cycle.title.isBlank()) {
  5. stringResource(id = R.string.title_cycle_hint)
  6. } else {
  7. cycle.title
  8. }
  9. )

}

答案1

得分: 0

你的Cycle类中的title属性不是一个StateFlow,因此Jetpack Compose不知道该属性何时更改,无法"重新组合"文本。

与其将整个类作为参数传递给组合内容,你应该只分享它们内部需要的信息:

  1. @Composable
  2. fun MyColumn(){
  3. Column(){
  4. // val cycle from viemodel
  5. var title by remember {
  6. mutableStateOf(cycle.title)
  7. }
  8. MyText(title)
  9. MyTextField(title)
  10. }
  11. }
  12. @Composable
  13. MyText(
  14. modifier: Modifier = Modifier(),
  15. title: MutableState<String>
  16. ){
  17. Text(
  18. modifier = modifier,
  19. style = MyTextTitleLabel20StrokeText,
  20. text = if (title.isBlank()) {
  21. stringResource(id = R.string.title_cycle_hint)
  22. } else {
  23. title.value
  24. }
  25. )
  26. }

或者,你可以将Cycle类中的title属性改为StateFlow(但这是不正确的方法)。

英文:

Your title property inside the Cycle class is not a StateFlow, so Jetpack Compose doesn't know when that property changes and can't "recompose" the text.

Instead of passing the entire class to the composables as a parameter, you should share only the informations needed inside them:

  1. @Composable
  2. fun MyColumn(){
  3. Column(){
  4. // val cycle from viemodel
  5. var title by remember {
  6. mutableStateOf(cycle.title)
  7. }
  8. MyText(title)
  9. MyTextField(title)
  10. }
  11. }
  12. @Composable
  13. MyText(
  14. modifier: Modifier = Modifier(),
  15. title: MutableState&lt;String&gt;
  16. ){
  17. Text(
  18. modifier = modifier,
  19. style = MyTextTitleLabel20StrokeText,
  20. text = if (title.isBlank()) {
  21. stringResource(id = R.string.title_cycle_hint)
  22. } else {
  23. title
  24. }
  25. )
  26. }

Or, you could make the title property a StateFlow inside the Cycle class. (But that's an incorrect approach)

huangapple
  • 本文由 发表于 2023年8月10日 20:50:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875891.html
匿名

发表评论

匿名网友

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

确定