前の記事でJetpackComposeでCheckboxの色を変更する方法
を書いたときにONの状態とOFFの状態で色を変更する方法を記載していただけなので、サンプルも残しておこうと思います。
CheckboxSample.kt
/**
* チェックボックスの色指定
*/
@Composable
private fun getCheckboxColors(): CheckboxColors {
return object: CheckboxColors {
@SuppressLint("UnrememberedMutableState")
@Composable
override fun borderColor(enabled: Boolean, state: ToggleableState): State<Color> {
return if(state == ToggleableState.On) {
mutableStateOf(colorResource(id = R.color.purple_200))
} else {
mutableStateOf(colorResource(id = R.color.purple_700))
}
}
@SuppressLint("UnrememberedMutableState")
@Composable
override fun boxColor(enabled: Boolean, state: ToggleableState): State<Color> {
return mutableStateOf(colorResource(id = R.color.white))
}
@SuppressLint("UnrememberedMutableState")
@Composable
override fun checkmarkColor(state: ToggleableState): State<Color> {
return if(state == ToggleableState.On) {
mutableStateOf(colorResource(id = R.color.purple_200))
} else {
mutableStateOf(colorResource(id = R.color.purple_700))
}
}
}
}
state == ToggleableState.On
でCheckboxの状態が見れるので、こちらで判定できます。