Skip to content

Instantly share code, notes, and snippets.

@wilinz
Last active February 29, 2024 13:25
Show Gist options
  • Save wilinz/3f06dce48fee4620522caaae7708caf8 to your computer and use it in GitHub Desktop.
Save wilinz/3f06dce48fee4620522caaae7708caf8 to your computer and use it in GitHub Desktop.
ComposeCheckboxWithLabel
package com.wilinz.xxx
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Checkbox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
@Composable
fun CheckboxWithLabel(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
label: @Composable () -> Unit,
) {
val isChecked = remember { mutableStateOf(checked) }
Row(
modifier = modifier
.padding(8.dp)
.clip(RoundedCornerShape(8.dp))
.clickable(
) {
isChecked.value = !isChecked.value
onCheckedChange(isChecked.value)
},
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = isChecked.value,
onCheckedChange = {
isChecked.value = it
onCheckedChange(it)
}
)
Column(Modifier.padding(end = 12.dp)) {
label()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment