1
1

JetpackComposeでCheckboxの色をONの状態とOFFの状態で色を変更する方法

Posted at

前の記事で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の状態が見れるので、こちらで判定できます。

スクリーンショット 2023-11-26 19.36.50.png

スクリーンショット 2023-11-26 19.36.44.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1