diff --git a/lib/models/todo.dart b/lib/models/todo.dart index fe2ce5f..7ea97cc 100644 --- a/lib/models/todo.dart +++ b/lib/models/todo.dart @@ -7,15 +7,17 @@ part 'todo.g.dart'; @collection class Todo { final int id; - final String title; - final String description; + String title; + String description; final DateTime createdAt; + bool done; Todo({ - int? id, + int? index, required this.title, this.description = '', - DateTime? createdAt, - }) : createdAt = createdAt ?? DateTime.now(), - id = id ?? DbHelper.nextTodoId; + DateTime? createdTime, + this.done = false, + }) : createdAt = createdTime ?? DateTime.now(), + id = index ?? DbHelper.nextTodoId; } diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart index fc84717..6016017 100644 --- a/lib/pages/dashboard_page.dart +++ b/lib/pages/dashboard_page.dart @@ -10,12 +10,18 @@ class DashboardPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( + appBar: AppBar(), floatingActionButton: FloatingActionButton( onPressed: () => Navigator.of(context).pushNamed(CreateTodoPage.routeName), child: const Icon(Icons.add), ), - body: const TodoList(), + body: Center( + child: SizedBox( + width: MediaQuery.of(context).size.width * 0.9, + child: const TodoList(), + ), + ), ); } } diff --git a/lib/pages/todo_create_page.dart b/lib/pages/todo_create_page.dart index 7a85dab..9518647 100644 --- a/lib/pages/todo_create_page.dart +++ b/lib/pages/todo_create_page.dart @@ -56,7 +56,7 @@ class _CreateTodoPageState extends State { } void _onSubmitted() { - DbHelper.addTodo(Todo(title: _title, description: _description)); + DbHelper.addOrUpdateTodo(Todo(title: _title, description: _description)); Navigator.of(context).pop(); } } diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index a3eaf75..22846c7 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -13,12 +13,21 @@ class DbHelper { schemas: [TodoSchema], directory: dir.path, ); + _isar.write((isar) => isar.todos.clear()); } - static List fetchTodos() => _isar.todos.where().findAll(); + static List fetchTodos() => + _isar.todos.where().doneEqualTo(false).findAll(); - static void addTodo(Todo todo) => _isar.write((isar) => isar.todos.put(todo)); + static void addOrUpdateTodo(Todo todo) => + _isar.write((isar) => isar.todos.put(todo)); - static Stream watchTodos() => + static void deleteTodoAt(int index) => + _isar.write((isar) => isar.todos.delete(index)); + + static void deleteTodo(Todo todo) => + _isar.write((isar) => isar.todos.delete(todo.id)); + + static Stream get todosChangedStream => _isar.todos.watchLazy(fireImmediately: true); } diff --git a/lib/widgets/todo_list.dart b/lib/widgets/todo_list.dart index 216676d..9915bb5 100644 --- a/lib/widgets/todo_list.dart +++ b/lib/widgets/todo_list.dart @@ -16,7 +16,7 @@ class _TodoListState extends State { @override void initState() { super.initState(); - DbHelper.watchTodos().listen((event) { + DbHelper.todosChangedStream.listen((event) { setState(() { _todos = DbHelper.fetchTodos(); }); @@ -35,7 +35,16 @@ class _TodoListState extends State { Widget _listBuilder(BuildContext context, int index) { final todo = _todos.elementAt(index); return ListTile( + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + tileColor: Theme.of(context).colorScheme.primaryContainer, title: Text(todo.title), + trailing: IconButton( + onPressed: () {}, + icon: Icon( + Icons.check_box, + color: Theme.of(context).colorScheme.primary, + ), + ), ); } }