Rainmaker
Rainmaker

Reputation: 11100

Compose swipe-to-dismiss selects wrong items

I have column with items where with "swipe-to-dismiss" functionality. The problem is: "swipe-to-delete" removes correct item only first time. Next time it sends wrong id to the "removeTask" function. I fixed it when using lazyColumn by adding item key to the lazyLayout. But for some reason I need to use column.

Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            items.forEach {
                SwipeableCompletedTask(task, removeTask)
            }
        }



@Composable
private fun SwipeableCompletedTask(task: UnboxingTask, removeTask: (String) -> Unit) {
    val dismissState = rememberDismissState(
        confirmStateChange = {
            if (it == DismissValue.DismissedToEnd || it == DismissValue.DismissedToStart) {
                removeTask(task.id ?: "") // same id here every time
            }
            false
        }
    )
    SwipeToDismiss(
        state = dismissState,
 ..........
    )
}

How can I fix this?

Upvotes: 3

Views: 1081

Answers (1)

FuHao
FuHao

Reputation: 56

You can use androidx.compose.runtime.key

Column(
    modifier = Modifier
        .fillMaxWidth()
) {
    items.forEach {
        key(task.id){ SwipeableCompletedTask(task, removeTask) }
    }
}

The task.id can use other value. You should make each value is unique in this composition.

Official Document: key

Upvotes: 1

Related Questions