Last active
August 31, 2021 02:19
-
-
Save widiarifki/c0ad3a42c8d903ce14557ceeac0f3ac1 to your computer and use it in GitHub Desktop.
This gist is part of https://github.com/widiarifki/android-playground/tree/master/compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalMaterialApi::class) | |
@Composable | |
fun ShoppingItemCard( | |
shoppingItem: ShoppingItem, | |
onToggleTickItem: (ShoppingItem) -> Unit, | |
onDeleteItem: (ShoppingItem) -> Unit | |
) { | |
val swiperState = rememberDismissState( | |
confirmStateChange = { dismissValue -> | |
val isItemDismissed = dismissValue == DismissValue.DismissedToEnd | |
// event delete hanya ketika item digeser sampai ujung kanan (DismissedToEnd) | |
if (isItemDismissed) onDeleteItem(shoppingItem) | |
// definisikan Boolean value, dimana kita menganggap swipe akan merubah state | |
isItemDismissed | |
} | |
) | |
var textStyle = MaterialTheme.typography.body1 | |
if (shoppingItem.isTicked) { | |
textStyle = textStyle.copy(textDecoration = TextDecoration.LineThrough) | |
} | |
SwipeToDismiss( | |
state = swiperState, | |
background = { | |
// UI yg tampil saat item digeser | |
Box( | |
modifier = Modifier.fillMaxHeight(), | |
contentAlignment = Alignment.CenterStart | |
) { | |
Text( | |
text = "Swipe left to cancel delete", | |
style = MaterialTheme.typography.body2 | |
) | |
} | |
} | |
) { | |
Card { | |
Row( | |
modifier = Modifier | |
.fillMaxWidth(1f) | |
.padding(10.dp) | |
) { | |
// Teks item belanja | |
Text( | |
text = shoppingItem.name, | |
style = textStyle, | |
modifier = Modifier.weight(1f) | |
) | |
// Done checkbox | |
Checkbox( | |
checked = shoppingItem.isTicked, | |
onCheckedChange = { | |
onToggleTickItem(shoppingItem) | |
} | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment