Changed styling, bit of refactoring
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class _CreateTodoPageState extends State<CreateTodoPage> {
|
||||
}
|
||||
|
||||
void _onSubmitted() {
|
||||
DbHelper.addTodo(Todo(title: _title, description: _description));
|
||||
DbHelper.addOrUpdateTodo(Todo(title: _title, description: _description));
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,12 +13,21 @@ class DbHelper {
|
||||
schemas: [TodoSchema],
|
||||
directory: dir.path,
|
||||
);
|
||||
_isar.write((isar) => isar.todos.clear());
|
||||
}
|
||||
|
||||
static List<Todo> fetchTodos() => _isar.todos.where().findAll();
|
||||
static List<Todo> 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<void> 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<void> get todosChangedStream =>
|
||||
_isar.todos.watchLazy(fireImmediately: true);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class _TodoListState extends State<TodoList> {
|
||||
@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<TodoList> {
|
||||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user