当属性在Jetpack Compose中更改时,更改remember中的值。

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

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 })
    }
}

huangapple
  • 本文由 发表于 2023年7月13日 20:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定