From 15c404c962d8afd4f36fb92dee7dfcf0d917d5d9 Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Wed, 8 Nov 2023 11:34:39 +0100 Subject: [PATCH] implemented way to add todo --- lib/models/todo.dart | 7 +++++-- lib/pages/create_todo_page.dart | 10 +++++++++- lib/pages/dashboard_page.dart | 2 ++ lib/services/dbhelper.dart | 9 ++++----- lib/widgets/todo_list.dart | 30 ++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 lib/widgets/todo_list.dart diff --git a/lib/models/todo.dart b/lib/models/todo.dart index 59b10fd..fe2ce5f 100644 --- a/lib/models/todo.dart +++ b/lib/models/todo.dart @@ -1,5 +1,7 @@ import 'package:isar/isar.dart'; +import '../services/dbhelper.dart'; + part 'todo.g.dart'; @collection @@ -10,9 +12,10 @@ class Todo { final DateTime createdAt; Todo({ - required this.id, + int? id, required this.title, this.description = '', DateTime? createdAt, - }) : createdAt = createdAt ?? DateTime.now(); + }) : createdAt = createdAt ?? DateTime.now(), + id = id ?? DbHelper.nextTodoId; } diff --git a/lib/pages/create_todo_page.dart b/lib/pages/create_todo_page.dart index 14e493b..7a85dab 100644 --- a/lib/pages/create_todo_page.dart +++ b/lib/pages/create_todo_page.dart @@ -1,5 +1,8 @@ import 'package:flutter/material.dart'; +import '../models/todo.dart'; +import '../services/dbhelper.dart'; + class CreateTodoPage extends StatefulWidget { const CreateTodoPage({super.key}); static const routeName = '/create'; @@ -17,7 +20,7 @@ class _CreateTodoPageState extends State { return Scaffold( floatingActionButton: _title.isNotEmpty ? FloatingActionButton( - onPressed: () {}, + onPressed: _onSubmitted, child: const Icon(Icons.save), ) : null, @@ -51,4 +54,9 @@ class _CreateTodoPageState extends State { ), ); } + + void _onSubmitted() { + DbHelper.addTodo(Todo(title: _title, description: _description)); + Navigator.of(context).pop(); + } } diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart index 8a5c636..7a61c59 100644 --- a/lib/pages/dashboard_page.dart +++ b/lib/pages/dashboard_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; +import '../widgets/todo_list.dart'; import 'create_todo_page.dart'; class DashboardPage extends StatelessWidget { @@ -14,6 +15,7 @@ class DashboardPage extends StatelessWidget { Navigator.of(context).pushNamed(CreateTodoPage.routeName), child: const Icon(Icons.add), ), + body: const TodoList(), ); } } diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index 77301ee..4aeb1c4 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -13,10 +13,9 @@ class DbHelper { schemas: [TodoSchema], directory: dir.path, ); - var todos = [ - Todo(id: nextTodoId, title: 'Get Stuff Done'), - Todo(id: nextTodoId, title: 'Some shit'), - ]; - _isar.write((isar) => isar.todos.putAll(todos)); } + + static List fetchTodos() => _isar.todos.where().findAll(); + + static void addTodo(Todo todo) => _isar.write((isar) => isar.todos.put(todo)); } diff --git a/lib/widgets/todo_list.dart b/lib/widgets/todo_list.dart new file mode 100644 index 0000000..a868242 --- /dev/null +++ b/lib/widgets/todo_list.dart @@ -0,0 +1,30 @@ +import 'package:flutter/material.dart'; + +import '../services/dbhelper.dart'; + +class TodoList extends StatefulWidget { + const TodoList({super.key}); + + @override + State createState() => _TodoListState(); +} + +class _TodoListState extends State { + final _todos = DbHelper.fetchTodos(); + + @override + Widget build(BuildContext context) { + return ListView.builder( + padding: const EdgeInsets.only(bottom: 60), + itemBuilder: _listBuilder, + itemCount: _todos.length, + ); + } + + Widget _listBuilder(BuildContext context, int index) { + final todo = _todos.elementAt(index); + return ListTile( + title: Text(todo.title), + ); + } +}