mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-27 09:06:43 +08:00
ChecklistEditor: Implement proper focus handling
Now the checklist editor is finally usable.
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:gitjournal/core/checklist.dart';
|
import 'package:gitjournal/core/checklist.dart';
|
||||||
|
|
||||||
import 'package:gitjournal/core/note.dart';
|
import 'package:gitjournal/core/note.dart';
|
||||||
@ -42,7 +43,7 @@ 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 = <ChecklistItem, FocusNode>{};
|
var focusNodes = <ChecklistItem, FocusScopeNode>{};
|
||||||
TextEditingController _titleTextController = TextEditingController();
|
TextEditingController _titleTextController = TextEditingController();
|
||||||
|
|
||||||
ChecklistEditorState(Note note) {
|
ChecklistEditorState(Note note) {
|
||||||
@ -59,13 +60,15 @@ class ChecklistEditorState extends State<ChecklistEditor>
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (var item in checklist.items) {
|
for (var item in checklist.items) {
|
||||||
focusNodes[item] = FocusNode();
|
focusNodes[item] = FocusScopeNode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_titleTextController.dispose();
|
_titleTextController.dispose();
|
||||||
|
focusNodes.values.forEach((fn) => fn.dispose());
|
||||||
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +83,7 @@ class ChecklistEditorState extends State<ChecklistEditor>
|
|||||||
key: UniqueKey(),
|
key: UniqueKey(),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
var fn = FocusNode();
|
var fn = FocusScopeNode();
|
||||||
var item = checklist.buildItem(false, "");
|
var item = checklist.buildItem(false, "");
|
||||||
checklist.addItem(item);
|
checklist.addItem(item);
|
||||||
focusNodes[item] = fn;
|
focusNodes[item] = fn;
|
||||||
@ -117,7 +120,10 @@ class ChecklistEditorState extends State<ChecklistEditor>
|
|||||||
appBar: buildEditorAppBar(widget, this),
|
appBar: buildEditorAppBar(widget, this),
|
||||||
floatingActionButton: buildFAB(widget, this),
|
floatingActionButton: buildFAB(widget, this),
|
||||||
body: Column(
|
body: Column(
|
||||||
children: <Widget>[titleEditor, Expanded(child: checklistWidget)],
|
children: <Widget>[
|
||||||
|
titleEditor,
|
||||||
|
Expanded(child: FocusScope(child: checklistWidget)),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -160,19 +166,21 @@ class ChecklistEditorState extends State<ChecklistEditor>
|
|||||||
// FIXME: Make this happen on the next build
|
// FIXME: Make this happen on the next build
|
||||||
Timer(const Duration(milliseconds: 300), () {
|
Timer(const Duration(milliseconds: 300), () {
|
||||||
FocusScope.of(context).requestFocus(fn);
|
FocusScope.of(context).requestFocus(fn);
|
||||||
debugPrint("forced focus!");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
itemFinished: () {
|
itemFinished: () {
|
||||||
var fn = FocusNode();
|
setState(() {
|
||||||
var item = checklist.buildItem(false, "");
|
var fn = FocusScopeNode();
|
||||||
checklist.insertItem(index + 1, item);
|
var item = checklist.buildItem(false, "");
|
||||||
focusNodes[item] = fn;
|
checklist.insertItem(index + 1, item);
|
||||||
|
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), () {
|
||||||
FocusScope.of(context).requestFocus(fn);
|
print("Asking focus to ${index + 1}");
|
||||||
|
FocusScope.of(context).requestFocus(fn);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -251,6 +259,7 @@ class _ChecklistItemTileState extends State<ChecklistItemTile> {
|
|||||||
|
|
||||||
void _onFocus() {
|
void _onFocus() {
|
||||||
setState(() {}); // rebuild to hide close button
|
setState(() {}); // rebuild to hide close button
|
||||||
|
SystemChannels.textInput.invokeMethod('TextInput.show');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -275,7 +284,7 @@ class _ChecklistItemTileState extends State<ChecklistItemTile> {
|
|||||||
border: InputBorder.none,
|
border: InputBorder.none,
|
||||||
isDense: true,
|
isDense: true,
|
||||||
),
|
),
|
||||||
onSubmitted: (_) => widget.itemFinished(),
|
onEditingComplete: widget.itemFinished,
|
||||||
);
|
);
|
||||||
|
|
||||||
return ListTile(
|
return ListTile(
|
||||||
@ -342,6 +351,5 @@ class AddItemButton extends StatelessWidget {
|
|||||||
|
|
||||||
// FIXME: The body needs to be scrollable
|
// FIXME: The body needs to be scrollable
|
||||||
// FIXME: Support removing an item when pressing backspace
|
// FIXME: Support removing an item when pressing backspace
|
||||||
// FIXME: Focus on + doesn't work on device
|
|
||||||
// FIXME: Align the checkbox and close button on top
|
// FIXME: Align the checkbox and close button on top
|
||||||
// FIXME: Move checked items to the bottom?
|
// FIXME: Move checked items to the bottom?
|
||||||
|
Reference in New Issue
Block a user