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

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

how to change Text() when changing TextField

问题

MyComposable:

class Cycle(val title: String)

从 ViewModel 接收到 Flow<Cycle>

@Composable
fun MyColumn() {
    Column() {
        val cycleVM: CycleVM = viewmodel()
        val cycle by cycleVM.cycle.collectAsState()

        MyText(cycle)
        MyTextField(cycle)
    }
}

MyTextField:

@Composable
fun MyTextField(cycle: Cycle) {
    var title by remember {
        mutableStateOf(cycle.title!!)
    }

    TextField(
        modifier = modifierValue.fillMaxWidth(),
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = colorResource(R.color.colorBackgroundCardView)
        ),
        value = title,
        onValueChange = {
            title = it
            cycle.title = it
        }
    )
}

MyText:

@Composable
fun MyTextField(cycle: Cycle) {
    Text(
        modifier = modifier1,
        style = MyTextTitleLabel20StrokeText,
        text = if (cycle.title.isBlank()) {
            stringResource(id = R.string.title_cycle_hint)
        } else {
            cycle.title
        }
    )
}
英文:

how to change Text() when changing TextField
MyComposable:

class Cycle(val title: String)

from ViewModel recived Flow<Cycle>

@Composable
    fun MyColumn(){
    Column(){
      val cycleVM : CycleVM  = viewmodel()  
      val cycle by cycleVM.cycle.collectAsState()

    MyText(cycle)
    MyTextField(cycle)
    
    }

MyTextField :

@Composable
fun MyTextField(cycle: Cycle) {
            var title by remember {
                mutableStateOf(cycle.title!!)
            }

            TextField(modifier = modifierValue.fillMaxWidth(),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = colorResource(R.color.colorBackgroundCardView)
                ),
                value = title,
                onValueChange = {
                    title = it
                    cycle.title = it
                }
               }
            )
}

MyText:

@Composable
fun MyTextField(cycle: Cycle) {

 Text(
        modifier = modifier1,
        style = MyTextTitleLabel20StrokeText,
        text = if (cycle.title.isBlank()) {
            stringResource(id = R.string.title_cycle_hint)
        } else {
            cycle.title
        }
    )

}

答案1

得分: 0

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

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

@Composable
fun MyColumn(){
    Column(){
        // val cycle  from viemodel

        var title by remember {
            mutableStateOf(cycle.title)
        }

        MyText(title)
        MyTextField(title)
    }
}

@Composable
MyText(
    modifier: Modifier = Modifier(),
    title: MutableState<String>
){
    Text(
        modifier = modifier,
        style = MyTextTitleLabel20StrokeText,
        text = if (title.isBlank()) {
            stringResource(id = R.string.title_cycle_hint)
        } else {
            title.value
        }
    )
}

或者,你可以将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:

@Composable
fun MyColumn(){
    Column(){
        // val cycle  from viemodel

        var title by remember {
            mutableStateOf(cycle.title)
        }

        MyText(title)
        MyTextField(title)
    }
}

@Composable
MyText(
    modifier: Modifier = Modifier(),
    title: MutableState&lt;String&gt;
){
    Text(
        modifier = modifier,
        style = MyTextTitleLabel20StrokeText,
        text = if (title.isBlank()) {
            stringResource(id = R.string.title_cycle_hint)
        } else {
            title
        }
    )
}

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:

确定