From 1cd0b967de10bb55b6a76e28787ccdbddef03a84 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 10 Feb 2020 17:10:54 +0100 Subject: [PATCH] Checklist: Let items be re-ordered --- lib/core/checklist.dart | 26 ++++++++++++++++++++++++++ lib/editors/checklist_editor.dart | 8 +++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/core/checklist.dart b/lib/core/checklist.dart index 3f41d6b5..dd950263 100644 --- a/lib/core/checklist.dart +++ b/lib/core/checklist.dart @@ -82,6 +82,32 @@ class Checklist { nodes.remove(item.element); items.remove(item); } + + ChecklistItem removeAt(int index) { + assert(index >= 0 && index <= items.length); + + var item = items[index]; + assert(nodes.contains(item.element)); + + nodes.remove(item.element); + items.removeAt(index); + + return item; + } + + void insertItem(int index, ChecklistItem item) { + if (index == 0) { + items.insert(0, item); + nodes.insert(0, item.element); + return; + } + + var prevItem = items[index]; + var nodeIndex = nodes.indexOf(prevItem.element); + + nodes.insert(nodeIndex + 1, item.element); + items.insert(index, item); + } } /// Copied from flutter-markdown - cannot be merged as we added xUpperCase and changed the regexp diff --git a/lib/editors/checklist_editor.dart b/lib/editors/checklist_editor.dart index b84b91ba..7223ed04 100644 --- a/lib/editors/checklist_editor.dart +++ b/lib/editors/checklist_editor.dart @@ -73,15 +73,13 @@ class ChecklistEditorState extends State children: itemTiles, onReorder: (int oldIndex, int newIndex) { setState(() { - /* - var item = todos.removeAt(oldIndex); + var item = checklist.removeAt(oldIndex); if (newIndex > oldIndex) { - todos.insert(newIndex - 1, item); + checklist.insertItem(newIndex - 1, item); } else { - todos.insert(newIndex, item); + checklist.insertItem(newIndex, item); } - */ }); }, );