Skip to content

Commit 0c5444c

Browse files
committed
Adicionado funcionalidades de salvar permanetemente os dados
1 parent 80e2b6b commit 0c5444c

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

lib/main.dart

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'dart:async';
77
void main() {
88
runApp(
99
MaterialApp(
10-
title: "Todo List",
10+
title: "ToDo List",
1111
home: Home(),
1212
),
1313
);
@@ -20,39 +20,32 @@ class Home extends StatefulWidget {
2020

2121
class _HomeState extends State<Home> {
2222
List<Map> _todoList = [];
23-
@override
24-
Widget build(BuildContext context) {
25-
final _tarefaController = TextEditingController();
2623

27-
void _addTarefa() {
24+
final _tarefaController = TextEditingController();
25+
26+
@override
27+
void initState() {
28+
super.initState();
29+
_getDados().then((dados) {
2830
setState(() {
29-
Map<String, dynamic> novaTarefa = Map();
30-
novaTarefa["tittle"] = _tarefaController.text;
31-
novaTarefa["estado"] = false;
32-
_todoList.add(novaTarefa);
31+
_todoList = json.decode(dados);
3332
});
34-
}
35-
36-
Future<File> _getArquivo() async {
37-
final diretorio = await getApplicationDocumentsDirectory();
38-
return File("${diretorio.path}/tarefas.json");
39-
}
40-
41-
Future<File> _saveTarefas() async {
42-
String dado = json.encode(_todoList);
43-
final arquivo = await _getArquivo();
44-
return arquivo.writeAsString(dado);
45-
}
33+
});
34+
}
4635

47-
Future<String> _getDados() async {
48-
try {
49-
final arquivo = await _getArquivo();
50-
return arquivo.readAsString();
51-
} catch (error) {
52-
return null;
53-
}
54-
}
36+
void _addTarefa() {
37+
setState(() {
38+
Map<String, dynamic> novaTarefa = Map();
39+
novaTarefa["tittle"] = _tarefaController.text;
40+
novaTarefa["estado"] = false;
41+
_tarefaController.text = "";
42+
_todoList.add(novaTarefa);
43+
_saveTarefas();
44+
});
45+
}
5546

47+
@override
48+
Widget build(BuildContext context) {
5649
return Scaffold(
5750
appBar: AppBar(
5851
centerTitle: true,
@@ -117,6 +110,7 @@ class _HomeState extends State<Home> {
117110
onChanged: (ok) {
118111
setState(() {
119112
_todoList[index]["estado"] = ok;
113+
_saveTarefas();
120114
});
121115
},
122116
);
@@ -127,4 +121,24 @@ class _HomeState extends State<Home> {
127121
),
128122
);
129123
}
124+
125+
Future<File> _getArquivo() async {
126+
final diretorio = await getApplicationDocumentsDirectory();
127+
return File("${diretorio.path}/tarefas.json");
128+
}
129+
130+
Future<File> _saveTarefas() async {
131+
String dado = json.encode(_todoList);
132+
final arquivo = await _getArquivo();
133+
return arquivo.writeAsString(dado);
134+
}
135+
136+
Future<String> _getDados() async {
137+
try {
138+
final arquivo = await _getArquivo();
139+
return arquivo.readAsString();
140+
} catch (error) {
141+
return null;
142+
}
143+
}
130144
}

0 commit comments

Comments
 (0)