Created
May 9, 2022 19:55
-
-
Save Wottrich/d1008c1192f14cfc3ba61a97c81d0956 to your computer and use it in GitHub Desktop.
SwipeToDeleteItem -> Compose 1.2.0-alpha8
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
SwipeToDeleteItem( | |
onDeleteItem = { onDeleteTask(task) }, | |
onBackground = { state -> BackgroundSwipeToDeleteItem(state) }, | |
onDismissContent = { | |
TaskItem( | |
task = task, | |
showDeleteItem = showDeleteItem, | |
onCheckChange = onCheckChange, | |
onDeleteTask = onDeleteTask | |
) | |
} | |
) | |
@ExperimentalMaterialApi | |
@Composable | |
private fun SwipeToDeleteItem( | |
onDeleteItem: () -> Unit, | |
onBackground: @Composable RowScope.(DismissState) -> Unit, | |
onDismissContent: @Composable RowScope.() -> Unit | |
) { | |
val state = rememberDismissState( | |
confirmStateChange = { | |
val isDismissedToStart = it == DismissedToStart | |
if (isDismissedToStart) { onDeleteItem() } | |
true | |
} | |
) | |
SwipeToDismiss( | |
state = state, | |
background = { onBackground(state) }, | |
dismissContent = onDismissContent, | |
directions = setOf(EndToStart) | |
) | |
} | |
@ExperimentalMaterialApi | |
@Composable | |
private fun BackgroundSwipeToDeleteItem(state: DismissState) { | |
val color = when (state.dismissDirection) { | |
StartToEnd -> Color.Transparent | |
EndToStart -> Color.Red // change background color here | |
else -> Color.Transparent | |
} | |
Box( | |
modifier = Modifier | |
.padding(top = 8.dp) | |
.padding(horizontal = 12.dp) | |
) { | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.clip(RoundedCornerShape(8.dp)) | |
.background(color = color) | |
.padding(8.dp) | |
) { | |
if (color != Color.Transparent) { | |
DeleteIcon( | |
modifier = Modifier.align(Alignment.CenterEnd) | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment