Changed styling, bit of refactoring
This commit is contained in:
@@ -7,15 +7,17 @@ part 'todo.g.dart';
|
|||||||
@collection
|
@collection
|
||||||
class Todo {
|
class Todo {
|
||||||
final int id;
|
final int id;
|
||||||
final String title;
|
String title;
|
||||||
final String description;
|
String description;
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
|
bool done;
|
||||||
|
|
||||||
Todo({
|
Todo({
|
||||||
int? id,
|
int? index,
|
||||||
required this.title,
|
required this.title,
|
||||||
this.description = '',
|
this.description = '',
|
||||||
DateTime? createdAt,
|
DateTime? createdTime,
|
||||||
}) : createdAt = createdAt ?? DateTime.now(),
|
this.done = false,
|
||||||
id = id ?? DbHelper.nextTodoId;
|
}) : createdAt = createdTime ?? DateTime.now(),
|
||||||
|
id = index ?? DbHelper.nextTodoId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,18 @@ class DashboardPage extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
appBar: AppBar(),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () =>
|
onPressed: () =>
|
||||||
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(),
|
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() {
|
void _onSubmitted() {
|
||||||
DbHelper.addTodo(Todo(title: _title, description: _description));
|
DbHelper.addOrUpdateTodo(Todo(title: _title, description: _description));
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,12 +13,21 @@ class DbHelper {
|
|||||||
schemas: [TodoSchema],
|
schemas: [TodoSchema],
|
||||||
directory: dir.path,
|
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);
|
_isar.todos.watchLazy(fireImmediately: true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class _TodoListState extends State<TodoList> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
DbHelper.watchTodos().listen((event) {
|
DbHelper.todosChangedStream.listen((event) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_todos = DbHelper.fetchTodos();
|
_todos = DbHelper.fetchTodos();
|
||||||
});
|
});
|
||||||
@@ -35,7 +35,16 @@ class _TodoListState extends State<TodoList> {
|
|||||||
Widget _listBuilder(BuildContext context, int index) {
|
Widget _listBuilder(BuildContext context, int index) {
|
||||||
final todo = _todos.elementAt(index);
|
final todo = _todos.elementAt(index);
|
||||||
return ListTile(
|
return ListTile(
|
||||||
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||||
|
tileColor: Theme.of(context).colorScheme.primaryContainer,
|
||||||
title: Text(todo.title),
|
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