@Stable
class YourCheckboxColors(
private val checkedCheckmarkColor: Color,
private val uncheckedCheckmarkColor: Color,
private val checkedBoxColor: Color,
private val uncheckedBoxColor: Color,
private val disabledCheckedBoxColor: Color,
private val disabledUncheckedBoxColor: Color,
private val disabledIndeterminateBoxColor: Color,
private val checkedBorderColor: Color,
private val uncheckedBorderColor: Color,
private val disabledBorderColor: Color,
private val disabledIndeterminateBorderColor: Color
) : CheckboxColors {
private val BoxInDuration = 50
private val BoxOutDuration = 100
private val CheckAnimationDuration = 100
private val CheckboxRippleRadius = 24.dp
private val CheckboxDefaultPadding = 2.dp
private val CheckboxSize = 20.dp
private val StrokeWidth = 2.dp
private val RadiusSize = 2.dp
@Composable
override fun checkmarkColor(state: ToggleableState): State<Color> {
val target = if (state == ToggleableState.Off) {
uncheckedCheckmarkColor
} else {
checkedCheckmarkColor
}
val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
return animateColorAsState(target, tween(durationMillis = duration))
}
@Composable
override fun boxColor(enabled: Boolean, state: ToggleableState): State<Color> {
val target = if (enabled) {
when (state) {
ToggleableState.On, ToggleableState.Indeterminate -> checkedBoxColor
ToggleableState.Off -> uncheckedBoxColor
}
} else {
when (state) {
ToggleableState.On -> disabledCheckedBoxColor
ToggleableState.Indeterminate -> disabledIndeterminateBoxColor
ToggleableState.Off -> disabledUncheckedBoxColor
}
}
// If not enabled 'snap' to the disabled state, as there should be no animations between
// enabled / disabled.
return if (enabled) {
val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
animateColorAsState(target, tween(durationMillis = duration))
} else {
rememberUpdatedState(target)
}
}
@Composable
override fun borderColor(enabled: Boolean, state: ToggleableState): State<Color> {
val target = if (enabled) {
when (state) {
ToggleableState.On, ToggleableState.Indeterminate -> checkedBorderColor
ToggleableState.Off -> uncheckedBorderColor
}
} else {
when (state) {
ToggleableState.Indeterminate -> disabledIndeterminateBorderColor
ToggleableState.On, ToggleableState.Off -> disabledBorderColor
}
}
// If not enabled 'snap' to the disabled state, as there should be no animations between
// enabled / disabled.
return if (enabled) {
val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
animateColorAsState(target, tween(durationMillis = duration))
} else {
rememberUpdatedState(target)
}
}
}
3条答案
按热度按时间f45qwnt81#
您需要实现
CheckboxColors
接口并创建您的配色方案。然后,您只需通过以下方式设置颜色:
dsekswqp2#
有默认颜色函数CheckboxDefaults.colors()。你可以改变具体的颜色。
yzckvree3#
您可以尝试以下代码并使用Box自定义复选框边框。