Sub topic 12
Swipe to Dismiss and Drag and Drop
This chapter assumes you can build stateful widgets, use setState, and understand ListView, Row, and Stack. A dismissible interaction removes an item with a swipe. A drag-and-drop interaction moves data from a draggable source to a drag target.
Choosing the right widget
| Widget | Purpose | Important requirement |
|---|---|---|
| `Dismissible` | Swipe an item away | Remove the item from your data in `onDismissed` |
| `Draggable<T>` | Drag typed data | Provide `data` and a visual `feedback` widget |
| `DragTarget<T>` | Receive dragged data | Handle the accepted data in a callback |
Dismissible does not remove anything from your list automatically. It animates the item and calls onDismissed. Your callback must update the list, or Flutter will report that the dismissed widget still exists.
Draggable<T> uses a type parameter, such as String, to describe the data being dragged. DragTarget<T> should use the same type.
A working example
The following screen supports both interactions. Swipe a task horizontally to delete it. Long-press a task and drag it to the archive area.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: TaskBoard()));
}
class TaskBoard extends StatefulWidget {
const TaskBoard({super.key});
@override
State<TaskBoard> createState() => _TaskBoardState();
}
class _TaskBoardState extends State<TaskBoard> {
final List<String> _tasks = ['Write tests', 'Review pull request', 'Update docs'];
final List<String> _archived = [];
void _archive(String task) {
if (_tasks.remove(task)) {
_archived.add(task);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Task board')),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _tasks.length,
itemBuilder: (context, index) {
final task = _tasks[index];
return Dismissible(
key: ValueKey(task),
background: Container(color: Colors.red),
direction: DismissDirection.horizontal,
onDismissed: (_) {
setState(() => _tasks.remove(task));
},
child: LongPressDraggable<String>(
data: task,
feedback: Material(
elevation: 4,
child: SizedBox(
width: 240,
child: ListTile(
tileColor: Colors.white,
title: Text(task),
),
),
),
childWhenDragging: ListTile(
title: Text(task),
textColor: Colors.grey,
),
child: ListTile(
title: Text(task),
leading: const Icon(Icons.drag_handle),
),
),
);
},
),
),
DragTarget<String>(
onAcceptWithDetails: (details) {
setState(() => _archive(details.data));
},
builder: (context, candidates, rejected) {
final hovering = candidates.isNotEmpty;
return Container(
height: 100,
width: double.infinity,
color: hovering ? Colors.teal.shade100 : Colors.teal.shade50,
child: const Center(child: Text('Drop here to archive')),
);
},
),
],
),
);
}
}LongPressDraggable starts dragging after a long press, which leaves ordinary taps available for other controls. feedback is the widget shown under the pointer. childWhenDragging replaces the original child while the drag is active.
The DragTarget builder receives candidate data while a draggable hovers over it. The example changes its color to give visual feedback. onAcceptWithDetails runs only after the drop is accepted.
Keys and data consistency
Each dismissible item needs a stable, unique key. ValueKey(task) works here because task names are unique. In a real application, use a permanent database or model ID instead.
Avoid relying on the original index inside onDismissed. Another animation or list update can change that index before the callback runs. Capturing the item and removing it by identity is safer.
Interaction flow
Exercise
Why must a Dismissible have a unique key and update the list in onDismissed?
Show answer
The key lets Flutter identify the item during its removal animation, and updating the list removes the dismissed widget from the tree.