我试图创建一些动态视图时,用户点击添加按钮,它开始一组新的文本字段,并填写所需的详细信息,并将所有设置添加到数组的最终列表中,我面临着如何处理每个文本字段的可变状态的问题,即值和onValueChange文本字段共享相同的viewModel可变状态。
如何处理动态视图,使只有所需的文本字段值发生变化,而不是所有相似的文本字段发生变化
Ui代码
@Composable
fun CombineFields(viewModel: MainContentUploadViewModel = hiltViewModel()) {
val containerTitle = viewModel.containerTitle.value
val containerAbout = viewModel.containerAbout.value
OutlinedTextField(value = containerTitle.innerStateTitle,
onValueChange = { viewModel.onEvent(MainContentEvent.ContainerTitle(it)) },
label = { Text(text = "Title") })
Spacer(modifier = Modifier.height(8.dp))
TextField(value = containerAbout.innerStateAbout,
onValueChange = { viewModel.onEvent(MainContentEvent.ContainerAbout(it)) },
modifier = Modifier.height(100.dp))
Spacer(modifier = Modifier.height(8.dp))
val options = listOf("Products", "Banners", "Categories")
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(options[0]) }
// We want to react on tap/press on TextField to show menu
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
readOnly = true,
value = selectedOptionText,
onValueChange = { },
label = { Text("Label") },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(
expanded = expanded
)
},
colors = ExposedDropdownMenuDefaults.textFieldColors()
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
options.forEach { selectionOption ->
DropdownMenuItem(
onClick = {
selectedOptionText = selectionOption
expanded = false
}
) {
Text(text = selectionOption)
}
}
}
}
}
视图模型
@HiltViewModel
class MainContentUploadViewModel @Inject constructor(private val useCases: UseCases) : ViewModel() {
private val _containerTitle = mutableStateOf(MainContentFieldState())
val containerTitle : State<MainContentFieldState> = _containerTitle
private val _containerAbout = mutableStateOf(MainContentFieldState())
val containerAbout : State<MainContentFieldState> = _containerAbout
private val _containerPriority = mutableStateOf(MainContentFieldState())
val containerPriority : State<MainContentFieldState> = _containerPriority
private val _selectedContent = mutableStateOf(MainContentFieldState())
val selectedContent : State<MainContentFieldState> = _selectedContent
private val _selectedTags = mutableStateOf(MainContentFieldState())
val selectedTags : State<MainContentFieldState> = _selectedTags
private val _allInnerContent = mutableStateOf(MainContentFieldState())
val allInnerContent : State<MainContentFieldState> = _allInnerContent
数据类别
data class InnerContainerItems(
val containerName:String? = null,
val containerAbout:String? = null,
val containerTags:List<String>? = null,
val containerType:String? = null,
val containerPriority:Int? = null,
)
data class MainScreenContainer(
val ScreenContainer:List<InnerContainerItems>? = null
)
任何人都可以帮助我如何进一步移动的动态领域谢谢。
1条答案
按热度按时间3mpgtkmj1#
正如我所看到的,你正在使用
var selectedOptionText by remember { mutableStateOf(options[0]) }
,我发现了这个https://stackoverflow.com/a/68887484/20839582。检查它是否有用,因为它提到使用mutableStateListOf和rememberSaveable。