Back
Psychological EngineeringIntermediate4 min

Undo Snackbar in Flutter (Optimistic UI)

Learn how to build optimistic UI in Flutter using AnimatedList and SnackBar. Replace annoying confirm dialogs with a modern Undo action.

Undo vs Confirm ⏪

Interrupting the user’s workflow with an “Are you sure you want to delete this?” dialog is a relic of old desktop software. The modern standard (popularized by Gmail) is Optimistic UI with Undo.

When the user taps delete, we assume they mean it. The item instantly vanishes from the screen, making the app feel lightning fast. However, we immediately slide in a SnackBar offering them a window of forgiveness: the “Undo” button.

How it works in Flutter

We implement this premium UX using two native Flutter tools:

  1. AnimatedList: Instead of a regular ListView, we use an AnimatedList. When we call removeItem, the item gracefully slides and fades away.
  2. ScaffoldMessenger: We immediately summon a SnackBar with an action button.

If the user taps Undo, we simply insert the data back into the list array and call insertItem on the AnimatedList key. It slides right back into place!

Backend Strategy

In a real production app, how do you handle the API call? You have two choices:

  1. The Delayed Request: Wait 4 seconds (the duration of the SnackBar). If they don’t tap Undo, then send the HTTP DELETE request to the server.
  2. The Instant Request: Send the DELETE request instantly. If they tap Undo, send a new HTTP POST request to re-create or restore the item on the server.