英文:
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<String>
){
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论