implemented way to add todo
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<CreateTodoPage> {
|
||||
return Scaffold(
|
||||
floatingActionButton: _title.isNotEmpty
|
||||
? FloatingActionButton(
|
||||
onPressed: () {},
|
||||
onPressed: _onSubmitted,
|
||||
child: const Icon(Icons.save),
|
||||
)
|
||||
: null,
|
||||
@@ -51,4 +54,9 @@ class _CreateTodoPageState extends State<CreateTodoPage> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onSubmitted() {
|
||||
DbHelper.addTodo(Todo(title: _title, description: _description));
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Todo> fetchTodos() => _isar.todos.where().findAll();
|
||||
|
||||
static void addTodo(Todo todo) => _isar.write((isar) => isar.todos.put(todo));
|
||||
}
|
||||
|
||||
30
lib/widgets/todo_list.dart
Normal file
30
lib/widgets/todo_list.dart
Normal 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),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user