我一直在玩Jetpack Compose,并尝试了一些方法。其中之一是呈现矢量资产(svgs)。
我创建了一个自定义复选框组件。这是代码:
@Composable
fun CustomCheckBox(
checked: Boolean = false,
onClick: (() -> Unit)?,
children: (@Composable() () -> Unit)? = null
) {
val imgRes = if (checked) R.drawable.ic_checkbox_on else R.drawable.ic_checkbox_off
val vectorAsset = +vectorResource(imgRes)
Clickable(onClick = onClick) {
Row(modifier = Height(100.dp) wraps Expanded) {
Container(
width = vectorAsset.defaultWidth.value.dp,
height = vectorAsset.defaultHeight.value.dp
) {
DrawVector(vectorImage = vectorAsset)
}
Container(modifier = Spacing(top = 10.dp)) {
children?.invoke()
}
}
}
}
还有更多代码,这样我们就可以看到完整的图片:
@Composable
fun TermsConditions(state: TermsAndConditionsState) {
CustomCheckBox(
onClick = {
state.accepted = !state.accepted
},
checked = state.accepted,
children = {
Text(text = "I accept the Terms of Service\" or \"I accept the Privacy Statement\" \"Click here to indicate that you have read and agree to the terms presented in the Terms and Conditions agreement.")
}
)
}
我的模型班:
@Model
data class TermsAndConditionsState(var accepted: Boolean)
您遇到了一个问题:每当我单击复选框组件时,都会触发状态更改(accept
标志被反转)。但是,它没有反映在UI中。令人惊讶的是,它可以与PNG一起使用。
有人知道我可能做错了什么吗?还是仅仅是Compose中的错误?我知道它处于开发的早期阶段,因此可以预期会有错误的行为。