-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
358 lines (331 loc) · 10.5 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import 'package:censo_aplicacion/censo.dart';
import 'package:censo_aplicacion/db.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/material/colors.dart';
//Programa nuevo, a editar y subir
void main() {
//var registro = new RegisterPage();
runApp(const MyApp());
//runApp(registro);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Censo Vacunas',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Censo Vacunas'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
backgroundColor: Colors.blue[400],
),
backgroundColor: Colors.blue[100], //Color de fondo
body: Container(
padding: EdgeInsets.all(30.0),
child: GridView.count(
crossAxisCount: 2,
children: <Widget>[
Botones(
title1: "Registrar Datos",
icon1: Icons.article_outlined,
color: Colors.blue,
),
Botones(
title1: "Visualizar Datos",
icon1: Icons.assessment,
color: Colors.blue,
),
],
),
));
}
}
class Botones extends StatelessWidget {
final String title1;
final IconData? icon1;
final MaterialColor? color;
Botones({this.title1 = "", this.icon1, this.color});
@override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
switch (title1) {
case "Registrar Datos":
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RegisterPage()),
);
break;
case "Visualizar Datos":
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RegisterPage()),
//MaterialPageRoute(builder: (context) => const Consultar()),
);
break;
}
}, //acción del "Onclick" del widget Card
splashColor: Colors.grey[10],
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(icon1, size: 70.0, color: color),
Text(title1, style: new TextStyle(fontSize: 12.0))
],
),
),
),
);
}
}
class RegisterPage extends StatefulWidget {
@override
State<RegisterPage> createState() => _RegisterPageState();
}
class _RegisterPageState extends State<RegisterPage> {
//Variables
GlobalKey<FormState> keyForm = new GlobalKey();
TextEditingController nombreCtrl = new TextEditingController();
TextEditingController correoCtrl = new TextEditingController();
TextEditingController numHabCtrl = new TextEditingController();
TextEditingController numVacCtrl = new TextEditingController();
TextEditingController direcionCtrl = new TextEditingController();
TextEditingController postalCtrl = new TextEditingController();
cargarCensos() async {
List<Censo> auxCenso = await DB.censos();
for (var i = 0; i < auxCenso.length; i++) {
print(auxCenso[i]);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: new Scaffold(
appBar: new AppBar(
actions: [
Container(
child: new Text("Registrar Datos Vacunas",
style: TextStyle(fontSize: 20)),
width: 250.0,
height: 30.0,
margin: EdgeInsets.only(top: 16.0, right: 5.0)),
],
title: Container(
child: atraS(),
width: 50.0,
),
),
body: new SingleChildScrollView(
child: new Container(
margin: new EdgeInsets.all(25.0),
child: new Form(
key: keyForm,
child: formUI(),
),
),
),
),
);
}
formItemsDesign(icon, item) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 7),
child: Card(child: ListTile(leading: Icon(icon), title: item)),
);
}
//Boton atras dek formulario
Widget atraS() {
return Column(
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyApp()),
);
},
child: Icon(
Icons.arrow_back,
color: Colors.white,
size: 25,
),
color: Colors.blue,
),
],
);
}
//Todo nuestro formulario
Widget formUI() {
return Column(
children: <Widget>[
formItemsDesign(
Icons.person,
TextFormField(
controller: nombreCtrl,
decoration: new InputDecoration(
labelText: 'Nombre completo:',
),
validator: validateName,
)),
formItemsDesign(
Icons.email,
TextFormField(
controller: correoCtrl,
decoration: new InputDecoration(
labelText: 'Correo electrónico:',
),
validator: validateEmail,
)),
formItemsDesign(
Icons.people,
TextFormField(
controller: numHabCtrl,
decoration: new InputDecoration(
labelText: 'Total de habitantes:',
),
keyboardType: TextInputType.phone,
validator: validateNumeroVacio,
)),
formItemsDesign(
Icons.coronavirus_sharp,
TextFormField(
controller: numVacCtrl,
decoration: new InputDecoration(
labelText: 'Personas vacunadas:',
),
keyboardType: TextInputType.phone,
validator: validateNumeroVacio,
)),
formItemsDesign(
Icons.directions,
TextFormField(
controller: direcionCtrl,
decoration: new InputDecoration(
labelText: 'Dirección:',
),
validator: validateCadenaVacia,
)),
formItemsDesign(
Icons.markunread_mailbox,
TextFormField(
controller: postalCtrl,
decoration: new InputDecoration(
labelText: 'Código Postal:',
),
validator: validateNumeroVacio,
)),
GestureDetector(
onTap: () {
save();
},
child: Container(
margin: new EdgeInsets.all(30.0),
alignment: Alignment.center,
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0)),
color: Colors.green,
),
child: Text("Guardar",
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w500)),
padding: EdgeInsets.only(top: 10, bottom: 13),
))
],
);
}
String? validateName(String? value) {
String pattern = r'(^[a-zA-Z ]*[a-zA-Z]$)';
RegExp regExp = new RegExp(pattern);
if (value!.length == 0) {
return "Ingrese un nombre.";
} else if ((value[value.length - 1] == " ") & (value.trim().length > 1)) {
return "Sin espacios al final.";
} else if (!regExp.hasMatch(value)) {
return "Ingrese un nombre válido.";
}
}
String? validateCadenaVacia(String? value) {
String patttern = r'(^[a-zA-Z][\w\W]*[a-zA-Z0-9]$)';
RegExp regExp = new RegExp(patttern);
if (value!.length == 0) {
return "No puede dejar el campo vacío.";
} else if ((value[value.length - 1] == " ") & (value.trim().length > 1)) {
return "Sin espacios al final.";
} else if (!regExp.hasMatch(value)) {
return "Ingrese datos válidos.";
}
}
String? validateNumeroVacio(String? value) {
String patttern = r'(^[0-9]*$)';
RegExp regExp = new RegExp(patttern);
if (value!.length == 0) {
return "No puede dejar el campo vacío.";
} else if (!regExp.hasMatch(value)) {
return "Sólo se aceptan números.";
}
}
String? validateEmail(String? value) {
String pattern =
r'(^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$)';
RegExp regExp = new RegExp(pattern);
if (value!.length == 0) {
return "Ingrese un correo electrónico.";
} else if (!regExp.hasMatch(value)) {
return "Correo inválido";
}
}
//Funciones y metodos
save() {
if (keyForm.currentState!.validate()) {
String nombre = nombreCtrl.text;
String correo = correoCtrl.text;
int habitantes = int.parse(numHabCtrl.text);
int vacunados = int.parse(numVacCtrl.text);
String direccion = direcionCtrl.text;
int cp = int.parse(postalCtrl.text);
Censo censo = Censo(nombre, correo, habitantes, vacunados, direccion, cp);
DB.insert(censo);
cargarCensos();
print("Nombre: ${nombreCtrl.text}");
print("Correo: ${correoCtrl.text}");
print("Numero de habitantes: ${numHabCtrl.text}");
print("Vacunados: ${numVacCtrl.text}");
print("Direccion: ${direcionCtrl.text}");
print("CP: ${postalCtrl.text}");
keyForm.currentState!.reset();
}
}
}
@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
}