I’m trying to create a list of ingredients that can be re-ordered using Flutter/Dart. I’m using ReorderableListView
and ReorderableDragStartListener
. I can drag the items around, but they won’t re-ordered. They just snapped back to their original places. Any help? The data (ingredients) comes from an sqlite query (I’m using sqflite library).
This is the code that I have:
@override
Widget build(BuildContext context) {
List<Map<String, dynamic>> ingredientsOrderable = List<Map<String, dynamic>>.from(ingredients);
return Scaffold(
body: SafeArea(
child: ReorderableListView(
onReorder: (int oldIndex, int newIndex) {
setState(() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final ingredient = ingredientsOrderable.removeAt(oldIndex);
ingredientsOrderable.insert(newIndex, ingredient);
});
},
children: ingredientsOrderable.asMap().entries.map((entry) {
final index = entry.key;
final ingredient = entry.value;
return ReorderableDragStartListener(
index: index,
key: Key(ingredient["ID"].toString()),
child: ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
IngredientDetailPage(widget.db, ingredient["ID"]),
),
);
},
title: Text(ingredient["Name"]),
),
);
}).toList(),
),
),
);
}
2
Answers
Nevermind. I forgot to copy the list back to the original. There are other things, but that's basically it.
Your list needs to be in State not in build(). Every time your page refreshes (which it must, when you reorder) you are reinitializing the list to the original order. Doh!