implemented way to add todo

This commit is contained in:
SomnusVeritas
2023-11-08 11:34:39 +01:00
parent d2f3eb109d
commit 15c404c962
5 changed files with 50 additions and 8 deletions

View File

@@ -1,5 +1,7 @@
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import '../services/dbhelper.dart';
part 'todo.g.dart'; part 'todo.g.dart';
@collection @collection
@@ -10,9 +12,10 @@ class Todo {
final DateTime createdAt; final DateTime createdAt;
Todo({ Todo({
required this.id, int? id,
required this.title, required this.title,
this.description = '', this.description = '',
DateTime? createdAt, DateTime? createdAt,
}) : createdAt = createdAt ?? DateTime.now(); }) : createdAt = createdAt ?? DateTime.now(),
id = id ?? DbHelper.nextTodoId;
} }

View File

@@ -1,5 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../models/todo.dart';
import '../services/dbhelper.dart';
class CreateTodoPage extends StatefulWidget { class CreateTodoPage extends StatefulWidget {
const CreateTodoPage({super.key}); const CreateTodoPage({super.key});
static const routeName = '/create'; static const routeName = '/create';
@@ -17,7 +20,7 @@ class _CreateTodoPageState extends State<CreateTodoPage> {
return Scaffold( return Scaffold(
floatingActionButton: _title.isNotEmpty floatingActionButton: _title.isNotEmpty
? FloatingActionButton( ? FloatingActionButton(
onPressed: () {}, onPressed: _onSubmitted,
child: const Icon(Icons.save), child: const Icon(Icons.save),
) )
: null, : null,
@@ -51,4 +54,9 @@ class _CreateTodoPageState extends State<CreateTodoPage> {
), ),
); );
} }
void _onSubmitted() {
DbHelper.addTodo(Todo(title: _title, description: _description));
Navigator.of(context).pop();
}
} }

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../widgets/todo_list.dart';
import 'create_todo_page.dart'; import 'create_todo_page.dart';
class DashboardPage extends StatelessWidget { class DashboardPage extends StatelessWidget {
@@ -14,6 +15,7 @@ class DashboardPage extends StatelessWidget {
Navigator.of(context).pushNamed(CreateTodoPage.routeName), Navigator.of(context).pushNamed(CreateTodoPage.routeName),
child: const Icon(Icons.add), child: const Icon(Icons.add),
), ),
body: const TodoList(),
); );
} }
} }

View File

@@ -13,10 +13,9 @@ class DbHelper {
schemas: [TodoSchema], schemas: [TodoSchema],
directory: dir.path, 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<Todo> fetchTodos() => _isar.todos.where().findAll();
static void addTodo(Todo todo) => _isar.write((isar) => isar.todos.put(todo));
} }

View File

@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
import '../services/dbhelper.dart';
class TodoList extends StatefulWidget {
const TodoList({super.key});
@override
State<TodoList> createState() => _TodoListState();
}
class _TodoListState extends State<TodoList> {
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),
);
}
}