1
1
import 'package:flutter/material.dart' ;
2
2
import 'package:provider/provider.dart' ;
3
- import 'package:todo/models/task_model.dart' ;
3
+ import 'package:todo/models/task.dart' ;
4
+ import 'package:todo/repositories/task_repository.dart' ;
5
+
6
+ import '../repositories/sqlite_task_repository.dart' ;
4
7
5
8
class AddTask extends StatefulWidget {
6
9
const AddTask ({
@@ -12,6 +15,15 @@ class AddTask extends StatefulWidget {
12
15
13
16
class _AddTaskState extends State <AddTask > {
14
17
final textController = TextEditingController ();
18
+ late SQLiteTaskRepository taskRepository;
19
+ late Future <List <Task >> taskListFuture;
20
+
21
+ @override
22
+ void initState () {
23
+ super .initState ();
24
+ taskRepository = SQLiteTaskRepository ();
25
+ taskListFuture = taskRepository.getAllTasks ();
26
+ }
15
27
16
28
@override
17
29
void dispose () {
@@ -28,6 +40,7 @@ class _AddTaskState extends State<AddTask> {
28
40
child: Padding (
29
41
padding: const EdgeInsets .only (left: 20.0 ),
30
42
child: TextField (
43
+ key: const Key ("task-input" ),
31
44
controller: textController,
32
45
decoration: const InputDecoration (
33
46
border: OutlineInputBorder (),
@@ -39,16 +52,24 @@ class _AddTaskState extends State<AddTask> {
39
52
flex: 1 ,
40
53
child: Padding (
41
54
padding: const EdgeInsets .all (8.0 ),
42
- child: Consumer (
43
- builder: (context, value, child) => ElevatedButton (
44
- onPressed: () {
45
- final widget = context.read <TaskModel >();
46
- if (textController.text.isNotEmpty) {
47
- widget.addTask (textController.text);
48
- textController.clear ();
49
- }
50
- },
51
- child: const Text ("ADD" )))),
55
+ child: ElevatedButton (
56
+ key: const Key ("task-add-button" ),
57
+ onPressed: () async {
58
+ if (textController.text.isNotEmpty) {
59
+ Task task = Task (
60
+ title: textController.text,
61
+ description: '' ,
62
+ createdAt: DateTime .now (),
63
+ isCompleted: false ,
64
+ rewardInSatoshis: 0 ,
65
+ );
66
+ await Provider .of <SQLiteTaskRepository >(context,
67
+ listen: false )
68
+ .addTask (task);
69
+ textController.clear ();
70
+ }
71
+ },
72
+ child: const Text ("ADD" ))),
52
73
),
53
74
],
54
75
);
0 commit comments