英文:
Change value in remember when property change in Jetpack Compose
问题
@Composable
fun TypeRadioGroup(
type: Type,
onTypeChange: (type: Type) -> Unit
) {
val options = listOf(Type.Oney, Type.Round)
var selectedOption by rememberSaveable {
mutableStateOf(type)
}
LaunchedEffect(type) {
selectedOption = type
}
val cornerRadius = 4.dp
Row(modifier = Modifier.fillMaxWidth()) {
options.forEachIndexed { index, type ->
OutlinedButton(
modifier = Modifier
.weight(1f)
.offset(x = if (index == 0) 0.dp else (-1).dp),
onClick = {
selectedOption = options[index]
onTypeChange(selectedOption)
},
shape = when (index) {
//this is for future multi trip option
0 -> RoundedCornerShape(topStart = cornerRadius, topEnd = 0.dp, bottomStart = cornerRadius, bottomEnd = 0.dp)
options.size - 1 -> RoundedCornerShape(
topStart = 0.dp,
topEnd = cornerRadius,
bottomStart = 0.dp,
bottomEnd = cornerRadius
)
else -> RoundedCornerShape(topStart = 0.dp, topEnd = 0.dp, bottomStart = 0.dp, bottomEnd = 0.dp)
},
border = BorderStroke(1.dp, color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled)),
colors = ButtonDefaults.outlinedButtonColors(
backgroundColor = if (selectedOption == flightType) DesignSystemColors.Primary05 else DesignSystemColors.White
),
) {
Text(
text = "TEXT",
modifier = Modifier.padding(horizontal = 8.dp)
)
}
}
}
}
英文:
I have a code:
@Composable
fun TypeRadioGroup(
type: Type,
onTypeChange: (type: Type) -> Unit
) {
val options = listOf(Type.Oney, Type.Round)
var selectedOption by rememberSaveable {
mutableStateOf(type)
}
LaunchedEffect(type) {
selectedOption = type
}
val cornerRadius = 4.dp
Row(modifier = Modifier.fillMaxWidth()) {
options.forEachIndexed { index, type ->
OutlinedButton(
modifier = Modifier
.weight(1f)
.offset(x = if (index == 0) 0.dp else (-1).dp),
onClick = {
selectedOption = options[index]
onTypeChange(selectedOption)
},
shape = when (index) {
//this is for future multi trip option
0 -> RoundedCornerShape(topStart = cornerRadius, topEnd = 0.dp, bottomStart = cornerRadius, bottomEnd = 0.dp)
options.size - 1 -> RoundedCornerShape(
topStart = 0.dp,
topEnd = cornerRadius,
bottomStart = 0.dp,
bottomEnd = cornerRadius
)
else -> RoundedCornerShape(topStart = 0.dp, topEnd = 0.dp, bottomStart = 0.dp, bottomEnd = 0.dp)
},
border = BorderStroke(1.dp, color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled)),
colors = ButtonDefaults.outlinedButtonColors(
backgroundColor = if (selectedOption == flightType) DesignSystemColors.Primary05 else DesignSystemColors.White
),
) {
Text(
text = "TEXT",
modifier = Modifier.padding(horizontal = 8.dp)
)
}
}
}
}
now sometimes when type was changed I got new type, but in LaunchedEffect selectedOption is still the same as last, even if type is different. Any ideas how change selectedOptions every time when type is changed?
答案1
得分: 1
我认为应该可以选择一个选项。
@Composable
fun TypeRadioGroup(
selectedType: Type, // 由于名称遮蔽而更改了参数名称
onTypeChange: (type: Type) -> Unit
) {
val options = listOf(Type.Oney, Type.Round)
val cornerRadius = 4.dp
Row(modifier = Modifier.fillMaxWidth()) {
options.forEachIndexed { index, type ->
OutlinedButton(
modifier = Modifier
.weight(1f)
.offset(x = if (index == 0) 0.dp else (-1).dp),
onClick = { onTypeChange(type) },
shape = when (index) {
// 这是为了未来的多次行程选项
0 -> RoundedCornerShape(topStart = cornerRadius, topEnd = 0.dp, bottomStart = cornerRadius, bottomEnd = 0.dp)
options.size - 1 -> RoundedCornerShape(
topStart = 0.dp,
topEnd = cornerRadius,
bottomStart = 0.dp,
bottomEnd = cornerRadius
)
else -> RoundedCornerShape(topStart = 0.dp, topEnd = 0.dp, bottomStart = 0.dp, bottomEnd = 0.dp)
},
border = BorderStroke(
1.dp,
color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled),
),
colors = ButtonDefaults.outlinedButtonColors(
backgroundColor = if (selectedType == flightType) DesignSystemColors.Primary05 else DesignSystemColors.White
),
) {
Text(
text = "TEXT",
modifier = Modifier.padding(horizontal = 8.dp)
)
}
}
}
}
由于状态被提升,您需要将其提供给可组合函数,并在更改事件上进行如下操作。
@Preview(showBackground = true)
@Composable
private fun TypeRadioGroupPreview() {
YourAppsTheme {
var selectedType by rememberSaveable { mutableStateOf(Type.Oney) }
TypeRadioGroup(selectedType = selectedType, onTypeChange = { selectedType = it })
}
}
英文:
I think should work to select an option.
@Composable
fun TypeRadioGroup(
selectedType: Type, // changed the param name because of name shadowing
onTypeChange: (type: Type) -> Unit
) {
val options = listOf(Type.Oney, Type.Round)
val cornerRadius = 4.dp
Row(modifier = Modifier.fillMaxWidth()) {
options.forEachIndexed { index, type ->
OutlinedButton(
modifier = Modifier
.weight(1f)
.offset(x = if (index == 0) 0.dp else (-1).dp),
onClick = { onTypeChange(type) },
shape = when (index) {
//this is for future multi trip option
0 -> RoundedCornerShape(topStart = cornerRadius, topEnd = 0.dp, bottomStart = cornerRadius, bottomEnd = 0.dp)
options.size - 1 -> RoundedCornerShape(
topStart = 0.dp,
topEnd = cornerRadius,
bottomStart = 0.dp,
bottomEnd = cornerRadius
)
else -> RoundedCornerShape(topStart = 0.dp, topEnd = 0.dp, bottomStart = 0.dp, bottomEnd = 0.dp)
},
border = BorderStroke(
1.dp,
color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled),
),
colors = ButtonDefaults.outlinedButtonColors(
backgroundColor = if (selectedType == flightType) DesignSystemColors.Primary05 else DesignSystemColors.White
),
) {
Text(
text = "TEXT",
modifier = Modifier.padding(horizontal = 8.dp)
)
}
}
}
}
As the state is hoisted, you need to provide it and on change event to the composable function like this.
@Preview(showBackground = true)
@Composable
private fun TypeRadioGroupPreview() {
YourAppsTheme {
var selectedType by rememberSaveable { mutableStateOf(Type.Oney) }
TypeRadioGroup(selectedType = selectedType, onTypeChange = { selectedType = it })
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论