gpt4 book ai didi

android - 在 Jetpack Compose 中管理状态

转载 作者:行者123 更新时间:2023-12-04 17:18:45 25 4
gpt4 key购买 nike

在我的 viewModel 中,每个屏幕都有“状态”。例如

class MainState(val commonState: CommonState) {
val text = MutableStateFlow("text")
}

我将 viewModel 传递到我的 JetpackCompose 屏幕。

@Composable
fun MainScreen(text: String, viewModel: HomeViewModel) {
val textState = remember { mutableStateOf(TextFieldValue()) }
Column(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = viewModel.state.mainState.text.value,
color = Color.Blue,
fontSize = 40.sp
)

Button(
onClick = { viewModel.state.mainState.text.value = "New text" },
colors = ButtonDefaults.buttonColors(
backgroundColor = Color.Green
),
modifier = Modifier.padding(16.dp)

) {
Text(text)
}

TextField(
value = textState.value,
onValueChange = { textState.value = it },
label = { Text("Input text") }
)

}
}

当我单击按钮时,我更改了状态值,我希望 UI 会更新,但它没有。我必须单击 TextField,然后单击 TextView 更新中的文本。

对于 UI 不自动更新有什么建议吗?

这就是我在 startActivity 中传递组件并启动整个屏幕的方式;

class HomeActivity : ComponentActivity() {

private val viewModel by viewModel<HomeViewModel>()
private val homeState: HomeState get() = viewModel.state

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
RateMeAppTheme {
ContentScreen(viewModel, homeState)
}
}

}

}

最佳答案

在这种简单的情况下,您应该在 MainState 类中使用 mutableStateOf("text") 而不是 mutableStateFlow

class MainState(val commonState: CommonState) {
val text = mutableStateOf("text")
}

使用 MutableStateFlow

要使用MutableStateFlow(当前场景不需要),我们需要收集

像下面这样:-

val state = viewModel.mainState.text.collectAsState() // we can collect a stateflow as state in a composable function

然后我们可以在 Text 中使用观察到的状态值:-

Text(text = state.value, ..)

最后,您的可组合函数应如下所示:-

@Composable
fun MainScreen(text: String, viewModel: HomeViewModel) {
val textState = remember { mutableStateOf(TextFieldValue()) }
val state = viewModel.mainState.text.collectAsState()
Column(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = state.value,
color = Color.Blue,
fontSize = 40.sp
)

Button(
onClick = { viewModel.mainState.text.value = "New text" },
colors = ButtonDefaults.buttonColors(
backgroundColor = Color.Green
),
modifier = Modifier.padding(16.dp)

) {
Text(text)
}

TextField(
value = textState.value,
onValueChange = { textState.value = it },
label = { Text("Input text") }
)

}
}

关于android - 在 Jetpack Compose 中管理状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67519783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com