From 6627f9c38a97bd0eebaf4412d4a3e8079db99ae1 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 11 Feb 2020 18:07:23 +0100 Subject: [PATCH] ChecklistEditor: Try to give focus on the next item on removing This doesn't seem to work reliably - it only kind-of gives the focus. I see the 'x' on the right, but the keyboard has not been shown. Maybe this is just an ios emulator issue. --- lib/core/checklist.dart | 6 ++++-- lib/editors/checklist_editor.dart | 36 ++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/core/checklist.dart b/lib/core/checklist.dart index ae416e37..22f08266 100644 --- a/lib/core/checklist.dart +++ b/lib/core/checklist.dart @@ -61,15 +61,17 @@ class Checklist { ].join(' '); } - void addItem(bool value, String text) { + ChecklistItem addItem(bool value, String text) { var elem = md.Element.withTag("input"); elem.attributes["type"] = "checkbox"; elem.attributes["checked"] = value.toString(); elem.attributes["xUpperCase"] = "false"; elem.attributes["text"] = text; - items.add(ChecklistItem.fromMarkdownElement(elem)); + var item = ChecklistItem.fromMarkdownElement(elem); + items.add(item); nodes.add(elem); + return item; } void removeItem(ChecklistItem item) { diff --git a/lib/editors/checklist_editor.dart b/lib/editors/checklist_editor.dart index 0c8cca12..48609161 100644 --- a/lib/editors/checklist_editor.dart +++ b/lib/editors/checklist_editor.dart @@ -42,15 +42,14 @@ class ChecklistEditor extends StatefulWidget implements Editor { class ChecklistEditorState extends State implements EditorState { Checklist checklist; - var focusNodes = []; + var focusNodes = {}; TextEditingController _titleTextController = TextEditingController(); ChecklistEditorState(Note note) { _titleTextController = TextEditingController(text: note.title); checklist = Checklist(note); - for (var i = 0; i < checklist.items.length + 1; i++) { - // extra 1 for new item - focusNodes.add(FocusNode()); + for (var item in checklist.items) { + focusNodes[item] = FocusNode(); } } @@ -71,9 +70,9 @@ class ChecklistEditorState extends State key: UniqueKey(), onPressed: () { setState(() { - var fn = focusNodes[checklist.items.length]; - focusNodes.add(FocusNode()); - checklist.addItem(false, ""); + var fn = FocusNode(); + var item = checklist.addItem(false, ""); + focusNodes[item] = fn; // FIXME: Make this happen on the next build Timer(const Duration(milliseconds: 50), () { @@ -123,7 +122,7 @@ class ChecklistEditorState extends State return ChecklistItemTile( key: UniqueKey(), item: item, - focusNode: focusNodes[index], + focusNode: focusNodes[item], statusChanged: (bool newVal) { setState(() { item.checked = newVal; @@ -134,7 +133,24 @@ class ChecklistEditorState extends State }, itemRemoved: () { setState(() { + // Give next item the focus + var nextIndex = index + 1; + if (index >= checklist.items.length - 1) { + nextIndex = index - 1; + } + print("Next focus index $nextIndex"); + var nextItemForFocus = checklist.items[nextIndex]; + var fn = focusNodes[nextItemForFocus]; + print("Giving focus to $nextItemForFocus"); + + focusNodes.remove(item); checklist.removeItem(item); + + // FIXME: Make this happen on the next build + Timer(const Duration(milliseconds: 300), () { + FocusScope.of(context).requestFocus(fn); + debugPrint("forced focus!"); + }); }); }, ); @@ -283,7 +299,7 @@ class AddItemButton extends StatelessWidget { } // FIXME: The body needs to be scrollable -// FIXME: New item button -> clicking on + should give the new one focus // FIXME: Fix padding issue on top // FIXME: When removing an item the focus should jump to the next/prev in line -// FIXME: Fix \n issue in the ChecklistEditor +// FIXME: Support pressing Enter to add a new item +// FIXME: Support removing an item when pressing backspace