Divider is not showing in the custom Jetpack Compose TextField - android

I have a custom TextField which has no problem in the single TextField but when I create a Row() and put two custom TextField inside that, the divider is gone.
My custom text field: I created a BasicTextField and set a column BorderColumnState() to draw border when the text field is focus
#Composable
fun InputTextField(
modifier: Modifier = Modifier,
textFieldValue: TextFieldValue = TextFieldValue(""),
labelText: String,
withBorderModifier: Modifier = Modifier,
enabled: Boolean = true,
dividerColor: Color,
dividerThickness: Dp = 0.5.dp,
spacer: Dp,
textStyle: TextStyle,
keyboardOptions: KeyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
isDropDown: Boolean = false,
valueChange: (String) -> Unit
) {
var value by remember { mutableStateOf(textFieldValue) }
//This is used if we want to use it in DropDown
if (textFieldValue.text.isNotEmpty() && isDropDown)
value = textFieldValue
///////////////////////////////////////////
val dividerState = remember {
mutableStateOf(true)
}
BasicTextField(
value = value,
onValueChange = {
value = it
valueChange.invoke(it.text)
},
modifier = Modifier
.onFocusChanged {
dividerState.value = !it.isFocused
},
decorationBox = { innerTextField ->
val mainModifier = if (!dividerState.value) {
withBorderModifier
} else {
modifier
}
BorderColumnState(
mainModifier,
labelText,
textStyle,
spacer,
innerTextField,
dividerState,
dividerThickness,
dividerColor
)
}, keyboardOptions = keyboardOptions, enabled = enabled
)
}
#Composable
private fun BorderColumnState(
modifier: Modifier,
labelText: String,
textStyle: TextStyle,
spacer: Dp,
innerTextField: #Composable () -> Unit,
dividerState: MutableState<Boolean>,
dividerThickness: Dp,
dividerColor: Color
) {
Column(
modifier = modifier.wrapContentHeight(),
horizontalAlignment = Alignment.Start
) {
Text(text = labelText, style = textStyle)
Spacer(modifier = Modifier.size(spacer))
innerTextField()
if (dividerState.value) {
Spacer(
modifier = modifier
.size(dividerThickness)
.background(dividerColor)
)
}
}
}
Usage custom text field: When I want to use two custom text field inside a Row, the divider is gone
#Composable
fun EditProfileScreen() {
val disableButtonState = remember {
mutableStateOf(false)
}
Box(modifier = Modifier.fillMaxSize()) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(start = 24.dp, end = 24.dp, top = 32.5.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
CountryCodeAndMobileNumber(
countryCodeList = listOf("+98"),
clientInfo,
disableButtonState,
viewModel
)
Spacer(modifier = Modifier.size(16.dp))
InputTextField(
modifier = Modifier.fillMaxWidth(),
textFieldValue = TextFieldValue("sasasasas#gmail.com"),
labelText = stringResource(R.string.email_address),
withBorderModifier = Modifier
.fillMaxWidth()
.border(
width = 1.dp,
shape = AppShapes.small,
color = AppColor.brandColor.BLUE_DE_FRANCE
)
.padding(4.dp),
textStyle = AppFont.PoppinsTypography.caption,
dividerColor = AppColor.neutralColor.SILVER_CHALICE,
spacer = 8.dp,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
) {}
}
}
}
#Composable
private fun CountryCodeAndMobileNumber(
countryCodeList: List<String>,
clientInfo: Client,
) {
var mobileClientInfo = clientInfo
Row(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
) {
CountryCodeDropDown(
list = countryCodeList,
label = stringResource(
id = R.string.country
),
dividerColor = AppColor.neutralColor.SILVER_CHALICE,
enabled = false
)
Spacer(modifier = Modifier.size(4.dp))
InputTextField(
textFieldValue = TextFieldValue("111111"),
labelText = stringResource(R.string.mobile_number),
withBorderModifier = Modifier
.wrapContentWidth()
.border(
width = 1.dp,
shape = AppShapes.small,
color = AppColor.brandColor.BLUE_DE_FRANCE
)
.padding(4.dp),
enabled = false,
textStyle = AppFont.PoppinsTypography.caption,
dividerColor = AppColor.neutralColor.SILVER_CHALICE,
spacer = 8.dp,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone)
) {
}
}
}
How can I solve this problem?

val mainModifier = if (!dividerState.value) {
withBorderModifier
} else {
modifier
}
You can not assign modifier as a variable. that's why it's not working.
Edit your code like this:
BorderColumnState(
Modifier.then(if (!dividerState.value) withBorderModifier else modifier),
labelText,
textStyle,
spacer,
innerTextField,
dividerState,
dividerThickness,
dividerColor
)

I checked the code on my IDE. Why are you setting the size for the divider?
I changed the spacer to this and that's working fine.
Spacer(
modifier = Modifier
.fillMaxWidth()
.height(dividerThickness)
.background(dividerColor)
)

Related

Unable to align a text and a Switch in a Row lambda in jetpack compose

ISSUE
I am trying to align the text Enable Notifications and the switch on the same line. If I were to use XML layouts, then I would have gone with constraint layout, but I am trying to figure out how to do this with Compose.
SET UP
#Composable
fun Settings() {
val viewModel: SettingsViewModel = viewModel()
val uiState: SettingsState = viewModel.uiState.collectAsState().value
SettingsList(uiState = uiState, viewModel)
}
#Composable
fun SettingsList(uiState: SettingsState, viewModel: SettingsViewModel, modifier: Modifier = Modifier) {
val scaffoldState = rememberScaffoldState()
Scaffold(
modifier = Modifier, scaffoldState = scaffoldState,
backgroundColor = MaterialTheme.colors.background,
topBar = {
TopAppBar(
modifier = Modifier.semantics {
heading()
},
backgroundColor = MaterialTheme.colors.surface,
contentPadding = PaddingValues(start = 12.dp)
) {
Icon(
tint = MaterialTheme.colors.onSurface,
imageVector = Icons.Default.ArrowBack,
contentDescription = stringResource(id = R.string.header_settings_back)
)
Spacer(modifier = modifier.width(16.dp))
Text(
text = stringResource(id = R.string.header_settings),
fontSize = 18.sp,
color = MaterialTheme.colors.onSurface
)
}
}
) {
val scrollState = rememberScrollState()
Column(
modifier = Modifier
.verticalScroll(scrollState).padding(paddingValues = it)
) {
NotificationSettingsComposable(uiState.notificationsEnabled, {
viewModel.toggleNotificationSettings()
}, Modifier.fillMaxWidth())
}
}
}
#Composable
fun NotificationSettingsComposable(checked: Boolean, onCheckedChanged: (Boolean) -> Unit, modifier: Modifier = Modifier) {
Surface(modifier = modifier.padding(12.dp)) {
Row {
Text(
text = stringResource(id = R.string.body_enable_notifications), fontSize = 16.sp,
color = MaterialTheme.colors.onSurface,
)
Switch(checked = checked, onCheckedChange = onCheckedChanged, modifier = Modifier.align(Alignment.Top))
}
}
}
However the text and the switch are not being aligned on the same line.
DESIRED OUTCOME
The text and the switch should be aligned on the same line as shown in this picture.
This is what I am getting
What I want to achieve
Add verticalAlignment = Alignment.CenterVertically in the Row:
Something like:
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Enable Notification", fontSize = 16.sp,
)
Switch(
checked = checkedState.value,
onCheckedChange = { checkedState.value = it }
)
}

Autocomplete TextField move down other views in jetpack compose

I wrote an AutoCompletet TextField in Jetpack Compose. I used this view in a Row and between two others' views. The side views move down when autocomplete dropdown is opened. How can I fix this issue?
The Link of GIF file is end of page. in the GIF you can see side views of Autocomplete move to bottom when the suggest items is shows.
My AutoComplete codes:
#Composable
fun AutoCompleteTextField(
modifier: Modifier = Modifier,
list: List<Product>,
searchTerm: TextFieldValue,
updateSearchTerm: (TextFieldValue) -> Unit,
) {
var product by remember {
mutableStateOf("")
}
var heightTextFields by remember {
mutableStateOf(55.dp)
}
var textFieldSize by remember {
mutableStateOf(Size.Zero)
}
var expanded by remember {
mutableStateOf(false)
}
val interactionSource = remember {
MutableInteractionSource()
}
Column(
modifier = modifier
.width(200.dp)
// .padding(30.dp)
.clickable(
interactionSource = interactionSource,
indication = null,
onClick = {
expanded = false
}
)
) {
Column(modifier = modifier.fillMaxWidth()) {
Row(
modifier = modifier
.border(BorderStroke(width = 1.dp, color = Color.LightGray))
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
IconButton(onClick = {product = "" }) {
Icon(
imageVector = Icons.Filled.Add,
contentDescription = "Add",
modifier = modifier
.padding(4.dp)
.align(Alignment.CenterVertically)
)
}
TextField(
modifier = Modifier
.fillMaxWidth()
.height(heightTextFields)
.onGloballyPositioned { coordinates ->
textFieldSize = coordinates.size.toSize()
},
value = product,
onValueChange = {
product = it
expanded = true
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
cursorColor = Color.Black
),
textStyle = TextStyle(
fontFamily = shabnamFontFamily,
color = MaterialTheme.colorScheme.secondary,
fontWeight = FontWeight.Bold,
fontSize = 14.sp,
),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Done
),
singleLine = true,
)
}
AnimatedVisibility(visible = expanded) {
Card(
modifier = modifier
.padding(horizontal = 4.dp)
.width(textFieldSize.width.dp),
elevation = 10.dp,
shape = RoundedCornerShape(bottomStart = 4.dp, bottomEnd = 4.dp)
) {
LazyColumn(
modifier = Modifier.heightIn(max = 150.dp),
) {
if (product.isNotEmpty()) {
items(
list.filter {
it.productName.lowercase()
.contains(product.lowercase())
}
) { item ->
ProductItems(title = item.productName) { title ->
product = title
expanded = false
}
}
} else {
items(
items =
list
) { item ->
ProductItems(title = item.productName) { title ->
product = title
expanded = false
}
}
}
}
}
}
}
}
}
}
Screen GIF file

Jetpack Compose: Textfield and FAB not using full width

I am trying to put a TextField and a FAB inside a bottomBar using Jetpack Compose.
I wrapped the two with a box, which has the modifier "fillMaxWidth".
But the two controls dont use the full width.
Does anyone know, how to fix this issue?
Here is my Code:
#Composable
fun ChatView() {
Scaffold(
topBar= { ChannelButton() },
bottomBar = { ChatBox() },
modifier = Modifier
.padding(10.dp)
) {
ChatList()
}
}
#Composable
fun ChatBox() {
Box(modifier = Modifier
.background(DiscordDarkGray)
.fillMaxWidth()
){
Column(modifier = Modifier
.padding(10.dp)
.fillMaxWidth()) {
HorizontalCenteredRow(modifier = Modifier
.fillMaxWidth()) {
val textState = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = textState.value,
onValueChange = { textState.value = it }
)
Spacer(modifier = Modifier.width(10.dp))
FloatingIconActionButton (
icon = Icons.Default.Send,
onClick = { /*TODO*/ },
backgroundColor = DiscordBlue
)
}
Spacer(modifier = Modifier.height(60.dp))
}
}
}
Here is the Code of the HorizontalCenteredRow:
#Composable
fun HorizontalCenteredRow(
modifier: Modifier = Modifier,
content: #Composable RowScope.() -> Unit
) {
Row(
modifier = modifier
.wrapContentSize(Alignment.Center),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
content = content
)
}
Here is the code of the FAB:
#Composable
fun FloatingIconActionButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)),
backgroundColor: Color = MaterialTheme.colors.secondary,
contentColor: Color = contentColorFor(backgroundColor),
elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(),
icon: ImageVector = Icons.Default.QuestionAnswer,
iconContentDescription: String = "",
) {
Surface(
modifier = modifier.let {
if (enabled) {
it.clickable(
onClick = onClick,
role = Role.Button,
interactionSource = interactionSource,
indication = null
)
} else it
},
shape = shape,
color = backgroundColor,
contentColor = contentColor,
elevation = elevation.elevation(interactionSource).value
) {
CompositionLocalProvider(LocalContentAlpha provides contentColor.alpha) {
ProvideTextStyle(MaterialTheme.typography.button) {
Box(
modifier = Modifier
.defaultMinSize(minWidth = FabSize, minHeight = FabSize)
.indication(interactionSource, rememberRipple()),
contentAlignment = Alignment.Center
) {
Icon(
icon,
iconContentDescription,
tint = if (enabled) {
colors().onPrimary
} else {
colors().onPrimary.transparentize(.6f)
}
)
}
}
}
}
}
Using
HorizontalCenteredRow(
modifier = Modifier.fillMaxWidth()
the Row fills all the space, but it doesn't mean that the children occupy the entire space.
If you want to fill all the space with the children you have to change the default dimensions of them.
For example you can apply modifier = Modifier.weight(1f) to the TextField.
Something like:
HorizontalCenteredRow(modifier = Modifier
.fillMaxWidth()) {
val textState = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = textState.value,
onValueChange = { textState.value = it },
modifier = Modifier.weight(1f)
)

FillMaxHeight, padding and wrapping content not behaving as expected in JetpackCompose

I got into Jetpack compose and I am having a hard time finding examples of a normal screen. Everyone just uses scaffold and calls it a day. Using fillMaxSize is supposed to do exactly this, can someone explain how composable inherit their size? The topmost composable specifically (root composable)?
#Composable
fun MainContent() {
Surface(modifier = Modifier.fillMaxSize()) {
Column() {
SearchBox()
CurrentWeather()
WindAndHumidityBox()
}
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeweathertestTheme {
// A surface container using the 'background' color from the theme
Surface(
color = MaterialTheme.colors.background,
modifier = Modifier.fillMaxSize()
) {
MainContent()
}
}
}
}
}
Also the PURPLE portion is using this code.
Surface(
modifier = Modifier
.padding(40.dp).wrapContentHeight(), elevation = 3.dp, shape = RoundedCornerShape(8.dp),
color = MaterialTheme.colors.primarySurface
) {
In XML we could use wrap content and apply padding to it, this doesn't seem to be the case with compose? I am trying to do something like this
EDIT: Whole compose file
#Composable
private fun Content() {
Column(modifier = Modifier.fillMaxSize()) {
SearchBox()
CurrentWeather()
WindAndHumidityBox()
}
}
#Composable
fun WindAndHumidityBox() {
Surface(
modifier = Modifier
.padding(40.dp)
.wrapContentHeight(), elevation = 3.dp, shape = RoundedCornerShape(8.dp),
color = MaterialTheme.colors.primarySurface
) {
Row {
Row(modifier = Modifier.align(Alignment.CenterVertically)) {
Image(
painter = painterResource(id = R.drawable.day_clear),
contentDescription = "humidity"
)
Text(
text = "Precipitation \n 10%", fontWeight = FontWeight.Bold, fontSize = 20.sp,
modifier = Modifier.align(Alignment.CenterVertically)
)
}
Row(modifier = Modifier.align(Alignment.CenterVertically)) {
Image(
painter = painterResource(id = R.drawable.day_clear),
contentDescription = "humidity"
)
Text(
text = "Precipitation \n 10%", fontWeight = FontWeight.Bold, fontSize = 20.sp,
modifier = Modifier.align(Alignment.CenterVertically)
)
}
}
}
}
#Composable
private fun CurrentWeather() {
Box(modifier = Modifier.fillMaxWidth()) {
Column(Modifier.align(Alignment.Center)) {
Column(modifier = Modifier.padding(bottom = 20.dp)) { // location and time/date
Row(modifier = Modifier.align(Alignment.CenterHorizontally)) {
Icon(
painter = painterResource(id = R.drawable.ic_location),
contentDescription = "location"
)
Text(
text = "London", fontWeight = FontWeight.Bold, fontSize = 20.sp,
modifier = Modifier.align(Alignment.CenterVertically)
)
}
Text(text = "Thu 4 December 8:41 am")
}
Row(Modifier.align(Alignment.CenterHorizontally)) {
Image(
painterResource(id = R.drawable.day_clear),
modifier = Modifier.align(alignment = Alignment.CenterVertically),
contentDescription = "current weather image displaying the current weather"
)
Text(
text = "27", fontSize = 40.sp, modifier = Modifier
.padding(5.dp)
.align(Alignment.Top)
)
}
Text(
text = "Sunny",
modifier = Modifier
.align(Alignment.CenterHorizontally)
.padding(top = 20.dp),
fontWeight = FontWeight.Bold,
fontSize = 20.sp
)
}
}
}
#Composable
private fun SearchBox() {
Box {
var textValue by remember { mutableStateOf("") }
val context = LocalContext.current
OutlinedTextField(value = textValue,
leadingIcon = {
Icon(
painterResource(id = R.drawable.ic_baseline_search_24),
"search"
)
},
label = { Text("Search For A City") },
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Search,
keyboardType = KeyboardType.Text
),
keyboardActions = KeyboardActions(
onSearch = {
hideKeyboard(context = context)
}),
onValueChange = { text ->
textValue = text
}
)
}
}
You are using too many parent containers.
Just use:
Surface(
color = MaterialTheme.colors.background,
modifier = Modifier.fillMaxSize()
){
Column(){
WindAndHumidityBox()
}
}
Then you can achieve the PURPLE portion in many ways.
In particular check the Intrinsic measurements.
For example:
Surface(
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth(),
elevation = 3.dp,
shape = RoundedCornerShape(8.dp),
color = MaterialTheme.colors.primarySurface
) {
Row(
Modifier.padding(40.dp), //Padding inside the Row
horizontalArrangement = Arrangement.Center
) {
Row(modifier = Modifier
.align(Alignment.CenterVertically)
.height(IntrinsicSize.Min) //Intrinsic measurements to center the icon vertically
) {
Image(
modifier = Modifier.fillMaxHeight(),
//..
)
//...
}
//...
}
}

how to make Jetpack compose checkbox rounded

How do I create a rounded checkbox in Jetpackcompose like this. I tried using a Shape composable on it but it doesn't work.
I was looking on how to do the same thing you were asking, your question helped me on my journey so it is only fair I share. Add some animations and you are set my friend.
Make a round looking icon by using a box and an icon
Box(
modifier = Modifier
.clip(CircleShape)
.size(40.dp)
.background(Color.Black)
.padding(3.dp)
.clip(CircleShape)
.background(Color.White),
contentAlignment = Alignment.Center
) {
Icon(imageVector = Icons.Default.Check, contentDescription = "")
}
2.Place the newly made rounded icon and some text next to each other by using a Row
Row(
verticalAlignment = Alignment.CenterVertically,
){
Box(
modifier = Modifier
.clip(CircleShape)
.size(40.dp)
.background(Color.Black)
.padding(3.dp)
.clip(CircleShape)
.background(Color.White),
contentAlignment = Alignment.Center
) {
Icon(imageVector = Icons.Default.Check, contentDescription = "")
}
Text(
text = checkedText.value,
color = color.value,
fontSize = 20.sp,
fontWeight = FontWeight.Medium,
modifier = Modifier.padding(start = 5.dp)
)
}
3.Replace whatever you want with variables so you can customize
it
#Composable
fun RoundedCheckView(
) {
val isChecked = remember { mutableStateOf(false) }
val checkedText = remember { mutableStateOf("unChecked") }
val circleSize = remember { mutableStateOf(20.dp) }
val circleThickness = remember { mutableStateOf(2.dp) }
val color = remember { mutableStateOf(Color.Gray) }
Row(
verticalAlignment = Alignment.CenterVertically,
{
Box(
modifier = Modifier
.clip(CircleShape)
.size(circleSize.value)
.background(color.value)
.padding(circleThickness.value)
.clip(CircleShape)
.background(Color.White) ,
contentAlignment = Alignment.Center
) {
Icon(imageVector = Icons.Default.Check, contentDescription = "")
}
Text(
text = checkedText.value,
color = color.value,
fontSize = 20.sp,
fontWeight = FontWeight.Medium,
modifier = Modifier.padding(start = 5.dp)
)
}
}
4.Finally add Modifier.toggleable to the row, basically making it a clickable item that toggles (between true and false) a variable in this case isChecked. Then just customize the variables according to what you need
#Composable
fun RoundedCheckView()
{
val isChecked = remember { mutableStateOf(false) }
val checkedText = remember { mutableStateOf("unChecked") }
val circleSize = remember { mutableStateOf(20.dp) }
val circleThickness = remember { mutableStateOf(2.dp) }
val color = remember { mutableStateOf(Color.Gray) }
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.toggleable(value = isChecked.value,role = Role.Checkbox) {
isChecked.value = it
if (isChecked.value) {
checkedText.value = "Checked"
circleSize.value = 40.dp
circleThickness.value = 3.dp
color.value = Color.Black
} else {
checkedText.value = "unChecked"
circleSize.value = 20.dp
circleThickness.value = 2.dp
color.value = Color.Gray
}
}) {
Box(
modifier = Modifier
.clip(CircleShape)
.size(circleSize.value)
.background(color.value)
.padding(circleThickness.value)
.clip(CircleShape)
.background(Color.White) ,
contentAlignment = Alignment.Center
) {
if(isChecked.value){
Icon(imageVector = Icons.Default.Check, contentDescription = "")
}
}
Text(
text = checkedText.value,
color = color.value,
fontSize = 20.sp,
fontWeight = FontWeight.Medium,
modifier = Modifier.padding(start = 5.dp)
)
}
}
This is how we can make a custom check box in jetpack compose
val isCheck = remember { mutableStateOf(false) }
Row {
Card(
modifier = Modifier.background(Color.White),
elevation = 0.dp,
shape = RoundedCornerShape(6.dp),
border = BorderStroke(1.5.dp, color = titleColor)
) {
Box(
modifier = Modifier
.size(25.dp)
.background(if (isCheck.value) titleColor else Color.White)
.clickable {
isCheck.value = !isCheck.value
},
contentAlignment = Center
) {
if(isCheck.value)
Icon(Icons.Default.Check, contentDescription = "", tint = Color.White)
}
}
Text(
modifier = Modifier
.align(CenterVertically)
.padding(start = 10.dp),
text = "I agree with the terms & condition",
)
}
You can try to make it use Box with modifier content alignment center. and put an icon on there.
#Preview
#Composable
fun Check() {
Box(
modifier = Modifier
.clip(CircleShape)
.size(50.dp)
.background(Color.Red)
.padding(5.dp)
.clip(CircleShape)
.background(Color.Blue),
contentAlignment = Alignment.Center
) {
Icon(imageVector = Icons.Default.Check, contentDescription = "")
}
}
We can use animations to make it behave similar to the default Checkbox. Using Modifier.toggleable on the top-level Row, the entire thing is clickable, including the label. This also creates the proper semantics for screen reader users. You can change the shape of the card to get a circular checkbox.
#Composable
fun PrimaryCheckbox(
label: String,
modifier: Modifier = Modifier,
size: Float = 24f,
checkedColor: Color = DarkGray,
uncheckedColor: Color = White,
checkmarkColor: Color = White,
onValueChange: () -> Unit
) {
var isChecked by remember { mutableStateOf(false) }
val checkboxColor: Color by animateColorAsState(if (isChecked) checkedColor else uncheckedColor)
val density = LocalDensity.current
val duration = 200
Row(
modifier = modifier
.toggleable(
value = isChecked,
role = Role.Checkbox,
onValueChange = {
isChecked = !isChecked
onValueChange.invoke()
}
)
) {
Card(
elevation = 0.dp,
shape = RoundedCornerShape(4.dp),
border = BorderStroke(1.5.dp, color = checkedColor),
) {
Box(
modifier = Modifier
.size(size.dp)
.background(checkboxColor),
contentAlignment = Alignment.Center
) {
androidx.compose.animation.AnimatedVisibility(
visible = isChecked,
enter = slideInHorizontally(
animationSpec = tween(duration)
) {
with(density) { (size * -0.5).dp.roundToPx() }
} + expandHorizontally(
expandFrom = Alignment.Start,
animationSpec = tween(duration)
),
exit = fadeOut()
) {
Icon(
Icons.Default.Check,
contentDescription = null,
tint = checkmarkColor
)
}
}
}
Text(
modifier = Modifier
.align(Alignment.CenterVertically)
.padding(start = 8.dp),
text = label,
)
}
}

Categories

Resources