tezvyn:

Dismissible: The Swipe-to-Remove Widget in Flutter

AI-drafted, machine-checkedSource: api.flutter.devintermediate

A `Dismissible` widget lets you swipe away list items, like clearing notifications. Use it for to-do lists or email inboxes. The key footgun: you MUST provide a unique `Key` and remove the item from your data source in `onDismissed`, or it will reappear on…

WHY IT EXISTS To solve the common UI problem of removing an item from a list with a swipe gesture. Building this from scratch requires complex gesture detection, animation controllers, and state management. Dismissible encapsulates this logic into a standard, reusable widget, saving you from reinventing the wheel.

THE MENTAL MODEL Think of Dismissible as a special frame you put around another widget. The frame listens for swipes. When you swipe, the frame animates its contents (the child widget) off-screen. After the animation, it tells you, "Okay, I'm done, now you can permanently delete the data."

HOW IT WORKS You wrap your list item widget (like a ListTile) with a Dismissible. You must provide a unique Key for each Dismissible so Flutter's rendering engine can track it correctly. The onDismissed callback is triggered after the swipe-away animation completes. Inside this callback, you must call setState to remove the item from your underlying data list. You can also provide a background widget (like a red Container with a trash icon) that is revealed during the swipe.

WHEN TO USE IT Use it in any ListView or Column where you want to provide a quick, gesture-based way to remove items. This is perfect for to-do apps, email clients, notification lists, and shopping carts. It provides immediate, tactile feedback for a common action.

WHEN NOT TO USE IT Avoid using it for actions that are not dismissals or for critical actions that shouldn't be easy to trigger accidentally (e.g., deleting an account). For those, use an explicit button or menu. While you can use the confirmDismiss property to show a confirmation dialog, consider if a simple swipe is the right user experience for a non-trivial action in the first place.

ONE CANONICAL EXAMPLE The most common mistake is assuming the widget handles data deletion. The Dismissible widget only handles the UI animation of swiping an item away. Your onDismissed callback MUST update the state to remove the item from the list that builds your ListView. If you don't, the item will visually disappear and then pop right back in on the next screen rebuild because the data backing it was never removed.

Read the original → api.flutter.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.