This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lista_emprestimo.dart
206 lines (194 loc) · 6.68 KB
/
lista_emprestimo.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import 'package:flutter/material.dart';
import 'package:trabalho_emprestimo/model/Emprestimo.dart';
import 'package:trabalho_emprestimo/persistence/ManipulaArquivo.dart';
import 'dart:convert';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
ManipulaArquivo manipulaArquivo = ManipulaArquivo();
final _tipoObjetoController = TextEditingController();
final _nomePessoaController = TextEditingController();
final _descricaoObjetoController = TextEditingController();
Map<String, dynamic> _ultimoRemovido;
int _ultimoRemovidoPos;
List _emprestimoList = [];
DateTime _dataInfo = DateTime.now();
@override
void initState() {
super.initState();
manipulaArquivo.readEmprestimo().then((dado) {
setState(() {
_emprestimoList = json.decode(dado);
});
});
}
void _addEmprestimo() {
setState(() {
Map<String, dynamic> novoEmprestimo = Map();
Emprestimo emprestimo = Emprestimo(
_tipoObjetoController.text,
_nomePessoaController.text,
_descricaoObjetoController.text,
// _dataInfo,
false);
novoEmprestimo = emprestimo.getEmprestimo();
_tipoObjetoController.text = "";
_nomePessoaController.text = "";
_descricaoObjetoController.text = "";
_emprestimoList.add(novoEmprestimo);
manipulaArquivo.saveEmprestimo(_emprestimoList);
});
}
Future<Null> _refresh() async {
await Future.delayed(Duration(seconds: 1));
setState(() {
_emprestimoList.sort((a, b) {
if (a["concluido"] && !b["concluido"])
return 1;
else if (!a["concluido"] && b["concluido"])
return -1;
else
return 0;
});
manipulaArquivo.saveEmprestimo(_emprestimoList);
});
return null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Lista de Empréstimos"),
centerTitle: true,
),
body: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(18.0, 8.0, 18.0, 8.0),
child: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(labelText: "Tipo do objeto"),
controller: _tipoObjetoController,
),
TextField(
decoration: InputDecoration(labelText: "Nome da pessoa"),
controller: _nomePessoaController,
),
TextField(
decoration: InputDecoration(labelText: "Descrição do objeto"),
controller: _descricaoObjetoController,
),
FlatButton(
child: Row(
children: <Widget>[
Text(
"${_dataInfo.day}/${_dataInfo.month}/${_dataInfo.year}"),
Icon(Icons.calendar_today),
],
),
onPressed: () async {
final dataSelecionada = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1967),
lastDate: DateTime(_dataInfo.year + 1),
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.dark(),
child: child,
);
},
);
if (dataSelecionada != null &&
dataSelecionada != _dataInfo) {
setState(() {
_dataInfo = dataSelecionada as DateTime;
});
}
},
),
RaisedButton(
child: Text("+ Adicionar"),
textColor: Colors.white,
onPressed: () {
_addEmprestimo();
},
),
],
),
),
Expanded(
child: RefreshIndicator(
onRefresh: _refresh,
child: ListView.builder(
padding: EdgeInsets.only(top: 10.0),
itemCount: _emprestimoList.length,
itemBuilder: buildItem),
),
),
],
),
);
}
Widget buildItem(BuildContext context, int index) {
return Dismissible(
key: Key(DateTime.now().millisecondsSinceEpoch.toString()),
background: Container(
color: Colors.red,
child: Align(
alignment: Alignment(-0.9, 0.0),
child: Icon(
Icons.delete,
color: Colors.white,
),
),
),
direction: DismissDirection.startToEnd,
child: CheckboxListTile(
title: Text('''
${_emprestimoList[index]["tipoObjeto"]}
${_emprestimoList[index]["nomePessoa"]}
${_emprestimoList[index]["descricaoObjeto"]}
${_dataInfo.day}/${_dataInfo.month}/${_dataInfo.year}
'''),
value: _emprestimoList[index]["concluido"],
secondary: CircleAvatar(
child: Icon(
_emprestimoList[index]["concluido"] ? Icons.check : Icons.error),
),
onChanged: (c) {
setState(() {
_emprestimoList[index]["concluido"] = c;
manipulaArquivo.saveEmprestimo(_emprestimoList);
});
},
),
onDismissed: (direction) {
setState(() {
_ultimoRemovido = Map.from(_emprestimoList[index]);
_ultimoRemovidoPos = index;
_emprestimoList.removeAt(index);
manipulaArquivo.saveEmprestimo(_emprestimoList);
final snack = SnackBar(
content: Text(
"Empréstimo \"${_ultimoRemovido["tipoObjeto"]}\"removido!"),
action: SnackBarAction(
label: "Desfazer",
onPressed: () {
setState(() {
_emprestimoList.insert(_ultimoRemovidoPos, _ultimoRemovido);
manipulaArquivo.saveEmprestimo(_emprestimoList);
});
}),
duration: Duration(seconds: 2),
);
Scaffold.of(context).removeCurrentSnackBar();
Scaffold.of(context).showSnackBar(snack);
});
},
);
}
}