Skip to content

Instantly share code, notes, and snippets.

@wilburx9
Last active February 18, 2021 17:30
Show Gist options
  • Save wilburx9/71961f5238a7af58fbdc5674a592772e to your computer and use it in GitHub Desktop.
Save wilburx9/71961f5238a7af58fbdc5674a592772e to your computer and use it in GitHub Desktop.
Handle dismiss action of Flutter Dismissible.
handleDismiss(DismissDirection direction, int index) {
// Get a reference to the swiped item
final swipedEmail = items[index];
// Remove it from the list
items.removeAt(index);
String action;
if (direction == DismissDirection.startToEnd) {
deleteItem();
action = "Deleted";
} else {
archiveItem();
action = "Archived";
}
_scaffoldKey.currentState
.showSnackBar(
SnackBar(
content: Text("$action. Do you want to undo?"),
duration: Duration(seconds: 5),
action: SnackBarAction(
label: "Undo",
textColor: Colors.yellow,
onPressed: () {
// Deep copy the email
final copiedEmail = Email.copy(swipedEmail);
// Insert it at swiped position and set state
setState(() => items.insert(index, copiedEmail));
}),
),
)
.closed
.then((reason) {
if (reason != SnackBarClosedReason.action) {
// The SnackBar was dismissed by some other means
// that's not clicking of action button
// Make API call to backend
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment