Hello, everyone! :wave: I’m having some difficult...
# compose
e
Hello, everyone! 👋 I’m having some difficulties with
SnackBar
. I have a list of items with
Checkbox
and when the user clicks, it “completes” the item and removes from the list. My implementation tries to shown an “Undo Snackbar” when the user performs this action, which brings the item back to the list. The problem is that the
Snackbar
disappears in less than one second due to the recompostion when the item disappears from the list.
Copy code
LazyColumn() {
    items(
        items = taskList,
        itemContent = { task ->
            TaskItem(
                task = task,
                onItemClick = onItemClick,
                onCheckedChange = {
                    onCheckedChange(it)
                    coroutineScope.launch { snackbarHostState.showSnackbar("Task Completed", "Undo") }
                }
            )
        }
    )
}
I tried using
LaunchedEffect
but AFAIK, this will associate the effect to a state, not the user click, right? Do you have any idea on how to make the
Snackbar
keep visible while the screen is recomposing? Thanks a lot in advance! ❤️
a
The message will be removed from the queue if the call to showSnackbar is cancelled. Where is
coroutineScope
declared in that snippet?
e
It was in the same composable function that is composing the list. 😅 I moved it to before the
Scaffold
definition and it works now. For some reason I thought it was “bad” to pass a CoroutineScope as a parameter in a composable function. Thanks a lot, Adam. 😊
a
You might consider making that launch call part of the caller's
onCheckedChanged(it)
from the snippet, so that you're not passing the scope down directly, only as part of the changed lambda
e
That makes a lot of sense. Thanks again! 🙂
👍 1