Hookup the checklist editor properly

It's not perfect as one cannot remove items or add new ones. But it's a
start.
This commit is contained in:
Vishesh Handa
2020-02-10 16:16:11 +01:00
parent f9cc724c16
commit fd791a6597
2 changed files with 43 additions and 48 deletions

View File

@ -51,6 +51,15 @@ class Checklist {
return _note; return _note;
} }
@override
String toString() {
return [
'[',
items.map((x) => x.toString()).join(', '),
']',
].join(' ');
}
} }
/// Copied from flutter-markdown - cannot be merged as we added xUpperCase and changed the regexp /// Copied from flutter-markdown - cannot be merged as we added xUpperCase and changed the regexp

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gitjournal/core/checklist.dart';
import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/editors/common.dart'; import 'package:gitjournal/editors/common.dart';
@ -38,18 +39,12 @@ class ChecklistEditor extends StatefulWidget implements Editor {
class ChecklistEditorState extends State<ChecklistEditor> class ChecklistEditorState extends State<ChecklistEditor>
implements EditorState { implements EditorState {
Note note; Checklist checklist;
List<TodoItem> todos;
TextEditingController _titleTextController = TextEditingController(); TextEditingController _titleTextController = TextEditingController();
ChecklistEditorState(this.note) { ChecklistEditorState(Note note) {
_titleTextController = TextEditingController(text: note.title); _titleTextController = TextEditingController(text: note.title);
checklist = Checklist(note);
todos = [
TodoItem(false, "First Item"),
TodoItem(true, "Second Item"),
TodoItem(false, "Third Item"),
];
} }
@override @override
@ -60,20 +55,21 @@ class ChecklistEditorState extends State<ChecklistEditor>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var todoItemTiles = <Widget>[]; var itemTiles = <Widget>[];
todos.forEach((TodoItem todo) { checklist.items.forEach((ChecklistItem item) {
todoItemTiles.add(_buildTile(todo)); itemTiles.add(_buildTile(item));
}); });
todoItemTiles.add(AddTodoItemButton( itemTiles.add(AddItemButton(
key: UniqueKey(), key: UniqueKey(),
onPressed: () {}, onPressed: () {},
)); ));
print("Building " + todos.toString()); print("Building " + checklist.toString());
Widget todoList = ReorderableListView( Widget checklistWidget = ReorderableListView(
children: todoItemTiles, children: itemTiles,
onReorder: (int oldIndex, int newIndex) { onReorder: (int oldIndex, int newIndex) {
setState(() { setState(() {
/*
var item = todos.removeAt(oldIndex); var item = todos.removeAt(oldIndex);
if (newIndex > oldIndex) { if (newIndex > oldIndex) {
@ -81,6 +77,7 @@ class ChecklistEditorState extends State<ChecklistEditor>
} else { } else {
todos.insert(newIndex, item); todos.insert(newIndex, item);
} }
*/
}); });
}, },
); );
@ -94,30 +91,31 @@ 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: todoList)], children: <Widget>[titleEditor, Expanded(child: checklistWidget)],
), ),
); );
} }
@override @override
Note getNote() { Note getNote() {
var note = checklist.note;
note.title = _titleTextController.text.trim(); note.title = _titleTextController.text.trim();
return note; return note;
} }
TodoItemTile _buildTile(TodoItem todo) { ChecklistItemTile _buildTile(ChecklistItem item) {
return TodoItemTile( return ChecklistItemTile(
key: UniqueKey(), key: UniqueKey(),
todo: todo, item: item,
statusChanged: (val) { statusChanged: (val) {
setState(() { setState(() {
todo.checked = val; item.checked = val;
}); });
}, },
todoRemoved: () { itemRemoved: () {
setState(() { setState(() {
// FIXME: The body isn't a good indicator, there could be multiple with the same body! // FIXME: The body isn't a good indicator, there could be multiple with the same body!
todos.removeWhere((t) => t.body == todo.body); // todos.removeWhere((t) => t.body == todo.body);
}); });
}, },
); );
@ -148,42 +146,30 @@ class _NoteTitleEditor extends StatelessWidget {
} }
} }
class TodoItem { class ChecklistItemTile extends StatefulWidget {
bool checked; final ChecklistItem item;
String body;
TodoItem(this.checked, this.body);
@override
String toString() {
return 'TodoItem($checked, "$body")';
}
}
class TodoItemTile extends StatefulWidget {
final TodoItem todo;
final Function statusChanged; final Function statusChanged;
final Function todoRemoved; final Function itemRemoved;
TodoItemTile({ ChecklistItemTile({
Key key, Key key,
@required this.todo, @required this.item,
@required this.statusChanged, @required this.statusChanged,
@required this.todoRemoved, @required this.itemRemoved,
}) : super(key: key); }) : super(key: key);
@override @override
_TodoItemTileState createState() => _TodoItemTileState(); _ChecklistItemTileState createState() => _ChecklistItemTileState();
} }
class _TodoItemTileState extends State<TodoItemTile> { class _ChecklistItemTileState extends State<ChecklistItemTile> {
TextEditingController _textController; TextEditingController _textController;
FocusNode _focusNode = FocusNode(); FocusNode _focusNode = FocusNode();
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_textController = TextEditingController(text: widget.todo.body); _textController = TextEditingController(text: widget.item.text);
_focusNode.addListener(() { _focusNode.addListener(() {
setState(() {}); setState(() {});
}); });
@ -212,7 +198,7 @@ class _TodoItemTileState extends State<TodoItemTile> {
children: <Widget>[ children: <Widget>[
Container(height: 24.0, width: 24.0, child: Icon(Icons.reorder)), Container(height: 24.0, width: 24.0, child: Icon(Icons.reorder)),
Checkbox( Checkbox(
value: widget.todo.checked, value: widget.item.checked,
onChanged: widget.statusChanged, onChanged: widget.statusChanged,
), ),
], ],
@ -222,17 +208,17 @@ class _TodoItemTileState extends State<TodoItemTile> {
trailing: _focusNode.hasFocus trailing: _focusNode.hasFocus
? IconButton( ? IconButton(
icon: Icon(Icons.cancel), icon: Icon(Icons.cancel),
onPressed: widget.todoRemoved, onPressed: widget.itemRemoved,
) )
: null, : null,
); );
} }
} }
class AddTodoItemButton extends StatelessWidget { class AddItemButton extends StatelessWidget {
final Function onPressed; final Function onPressed;
AddTodoItemButton({Key key, @required this.onPressed}) : super(key: key); AddItemButton({Key key, @required this.onPressed}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {