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.
This commit is contained in:
Vishesh Handa
2020-02-11 18:07:23 +01:00
parent e9742b249c
commit 6627f9c38a
2 changed files with 30 additions and 12 deletions

View File

@ -61,15 +61,17 @@ class Checklist {
].join(' '); ].join(' ');
} }
void addItem(bool value, String text) { ChecklistItem addItem(bool value, String text) {
var elem = md.Element.withTag("input"); var elem = md.Element.withTag("input");
elem.attributes["type"] = "checkbox"; elem.attributes["type"] = "checkbox";
elem.attributes["checked"] = value.toString(); elem.attributes["checked"] = value.toString();
elem.attributes["xUpperCase"] = "false"; elem.attributes["xUpperCase"] = "false";
elem.attributes["text"] = text; elem.attributes["text"] = text;
items.add(ChecklistItem.fromMarkdownElement(elem)); var item = ChecklistItem.fromMarkdownElement(elem);
items.add(item);
nodes.add(elem); nodes.add(elem);
return item;
} }
void removeItem(ChecklistItem item) { void removeItem(ChecklistItem item) {

View File

@ -42,15 +42,14 @@ class ChecklistEditor extends StatefulWidget implements Editor {
class ChecklistEditorState extends State<ChecklistEditor> class ChecklistEditorState extends State<ChecklistEditor>
implements EditorState { implements EditorState {
Checklist checklist; Checklist checklist;
var focusNodes = <FocusNode>[]; var focusNodes = <ChecklistItem, FocusNode>{};
TextEditingController _titleTextController = TextEditingController(); TextEditingController _titleTextController = TextEditingController();
ChecklistEditorState(Note note) { ChecklistEditorState(Note note) {
_titleTextController = TextEditingController(text: note.title); _titleTextController = TextEditingController(text: note.title);
checklist = Checklist(note); checklist = Checklist(note);
for (var i = 0; i < checklist.items.length + 1; i++) { for (var item in checklist.items) {
// extra 1 for new item focusNodes[item] = FocusNode();
focusNodes.add(FocusNode());
} }
} }
@ -71,9 +70,9 @@ class ChecklistEditorState extends State<ChecklistEditor>
key: UniqueKey(), key: UniqueKey(),
onPressed: () { onPressed: () {
setState(() { setState(() {
var fn = focusNodes[checklist.items.length]; var fn = FocusNode();
focusNodes.add(FocusNode()); var item = checklist.addItem(false, "");
checklist.addItem(false, ""); focusNodes[item] = fn;
// FIXME: Make this happen on the next build // FIXME: Make this happen on the next build
Timer(const Duration(milliseconds: 50), () { Timer(const Duration(milliseconds: 50), () {
@ -123,7 +122,7 @@ class ChecklistEditorState extends State<ChecklistEditor>
return ChecklistItemTile( return ChecklistItemTile(
key: UniqueKey(), key: UniqueKey(),
item: item, item: item,
focusNode: focusNodes[index], focusNode: focusNodes[item],
statusChanged: (bool newVal) { statusChanged: (bool newVal) {
setState(() { setState(() {
item.checked = newVal; item.checked = newVal;
@ -134,7 +133,24 @@ class ChecklistEditorState extends State<ChecklistEditor>
}, },
itemRemoved: () { itemRemoved: () {
setState(() { 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); 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: 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: Fix padding issue on top
// FIXME: When removing an item the focus should jump to the next/prev in line // 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