https://github.com/yanzm/ComposeToggleButton
こんな感じのやつ。
選択の処理は Modifier.toggleable() を使えば OK。
- @Composable
- fun IconToggleButton(
- imageVector: ImageVector,
- contentDescription: String?,
- checked: Boolean,
- onCheckedChange: (Boolean) -> Unit,
- enabled: Boolean = true
- ) {
- CompositionLocalProvider(
- LocalContentColor provides contentColor(enabled = enabled, checked = checked),
- ) {
- Box(
- contentAlignment = Alignment.Center,
- modifier = Modifier
- .toggleable(
- value = checked,
- onValueChange = onCheckedChange,
- role = Role.RadioButton,
- )
- .size(48.dp)
- ) {
- Icon(
- imageVector = imageVector,
- contentDescription = contentDescription,
- modifier = Modifier.size(24.dp)
- )
- }
- }
- }
枠線などの描画は Modifier.drawWithContent() でやったが、これが面倒だった〜(特にRTL対応)。
- private fun Modifier.drawToggleButtonFrame(
- ...
- ): Modifier = this.drawWithContent {
- ...
- // draw checked border
- drawPath(
- path = ...,
- color = checkedBorderColor,
- style = Stroke(strokeWidth),
- )
- drawContent()
- }
0 件のコメント:
コメントを投稿