Reorderable List
Loading preview
Starting the Flutter app
Drag handles with ReorderableListView
import 'package:flutter/material.dart';
class ReorderableListPage extends StatefulWidget {
const ReorderableListPage({super.key});
@override
State<ReorderableListPage> createState() => _ReorderableListPageState();
}
class _ReorderableListPageState extends State<ReorderableListPage> {
final _items = ['Task A', 'Task B', 'Task C', 'Task D', 'Task E'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Reorderable List')),
body: ReorderableListView.builder(
itemCount: _items.length,
onReorderItem: (oldIndex, newIndex) {
setState(() {
final item = _items.removeAt(oldIndex);
_items.insert(newIndex, item);
});
},
itemBuilder: (context, index) {
return Card(
key: ValueKey(_items[index]),
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: ListTile(
leading: const Icon(Icons.drag_handle),
title: Text(_items[index]),
),
);
},
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
_items holds five strings shown through ReorderableListView.builder, each Card carrying the required ValueKey. onReorderItem already adjusts newIndex for the removed item, so the callback only removes and inserts. Icons.drag_handle is decorative, since it isn’t wrapped in a drag listener: mobile users long-press a row, and desktop gets the default trailing handle.
Adapting it
- Wrap the icon in
ReorderableDragStartListenerand setbuildDefaultDragHandles: false. - Persist
_itemsorder after each drop, because it resets when the page is recreated. - Use unique IDs in
ValueKey, as duplicate titles would collide with strings as keys.