· 6 years ago · Feb 24, 2020, 03:24 AM
1import 'package:flutter/material.dart';
2import 'package:http/http.dart' as http;
3import 'dart:async';
4import 'dart:convert';
5
6class MyApp extends StatelessWidget {
7 @override
8 Widget build(BuildContext context) {
9 return MaterialApp(
10 home: Scaffold(
11 appBar: AppBar(title: Text('Insert TextField Data to MySQL Database')),
12 backgroundColor: Colors.greenAccent,
13 body: Center(
14 child: TransfterData()
15 )
16 )
17 );
18 }
19}
20
21class TransfterData extends StatefulWidget {
22 TransfterData({Key key, this.title}) : super(key: key);
23
24 final String title;
25
26
27TransfterDataWidget createState() => TransfterDataWidget();
28
29}
30
31class TransfterDataWidget extends State {
32 final _formKey = GlobalKey<FormState>();
33
34 // Getting value from TextField widget.
35
36 String name, student, subject, year, q1, q2, q3, q4, q5;
37 // final nameController = TextEditingController();
38 // final studentNumberController = TextEditingController();
39 // final subjectController = TextEditingController();
40 // final yearController = TextEditingController();
41
42
43 TextEditingController txtNota = new TextEditingController();
44 TextEditingController txtNota1 = new TextEditingController();
45 TextEditingController txtNota2 = new TextEditingController();
46 TextEditingController txtNota3 = new TextEditingController();
47 TextEditingController txtNota4 = new TextEditingController();
48 TextEditingController txtNota5 = new TextEditingController();
49
50 @override
51 void initState()
52 {
53super.initState();
54listarClientes();
55 }
56
57 List listaCliente;
58 Future<Null> listarClientes() async {
59 var respuesta;
60 final response = await http.post(
61 "http://192.168.1.83/LAB-System/api/ajax/cliente.php?op=ListarCliente",
62 body: {});
63 setState(() {
64 respuesta = json.decode(response.body);
65 listaCliente = respuesta['data'];
66
67 });
68 pintarClientes();
69 pintarClientes1();
70 pintarClientes2();
71 pintarClientes3();
72 pintarClientes4();
73
74 }
75String _dropdownValue = null;
76String _dropdownValue1 = null;
77String _dropdownValue2 = null;
78String _dropdownValue3 = null;
79String _dropdownValue4 = null;
80
81Map<String ,String>listarClienteM=Map();
82
83
84void pintarClientes()
85{
86 for(var i=0; i<listaCliente.length;i++)
87 {
88 listarClienteM[listaCliente[i]['codCli']]=listaCliente[i]['nombre'];
89 }
90_dropdownValue=listarClienteM[listaCliente[0]['codCli']];
91}
92String mostrarcodigoCliente()
93{
94var usdKey=listarClienteM.keys.firstWhere((K)=> listarClienteM[K]== _dropdownValue,
95orElse: ()=>null
96);
97return usdKey;
98}
99
100// second dropdown
101void pintarClientes1()
102{
103 for(var i=0; i<listaCliente.length;i++)
104 {
105 listarClienteM[listaCliente[i]['codCli']]=listaCliente[i]['nombre'];
106 }
107_dropdownValue1=listarClienteM[listaCliente[0]['codCli']];
108}
109String mostrarcodigoCliente1()
110{
111var usdKey=listarClienteM.keys.firstWhere((K)=> listarClienteM[K]== _dropdownValue1,
112orElse: ()=>null
113);
114return usdKey;
115}
116
117// 3rd dropdown
118void pintarClientes2()
119{
120 for(var i=0; i<listaCliente.length;i++)
121 {
122 listarClienteM[listaCliente[i]['codCli']]=listaCliente[i]['nombre'];
123 }
124_dropdownValue2=listarClienteM[listaCliente[0]['codCli']];
125}
126String mostrarcodigoCliente2()
127{
128var usdKey=listarClienteM.keys.firstWhere((K)=> listarClienteM[K]== _dropdownValue2,
129orElse: ()=>null
130);
131return usdKey;
132}
133
134// 4th dropdown
135void pintarClientes3()
136{
137 for(var i=0; i<listaCliente.length;i++)
138 {
139 listarClienteM[listaCliente[i]['codCli']]=listaCliente[i]['nombre'];
140 }
141_dropdownValue3=listarClienteM[listaCliente[0]['codCli']];
142}
143String mostrarcodigoCliente3()
144{
145var usdKey=listarClienteM.keys.firstWhere((K)=> listarClienteM[K]== _dropdownValue3,
146orElse: ()=>null
147);
148return usdKey;
149}
150
151// 5th dropdown
152void pintarClientes4()
153{
154 for(var i=0; i<listaCliente.length;i++)
155 {
156 listarClienteM[listaCliente[i]['codCli']]=listaCliente[i]['subject'];
157 }
158_dropdownValue4=listarClienteM[listaCliente[0]['codCli']];
159}
160String mostrarcodigoCliente4()
161{
162var usdKey=listarClienteM.keys.firstWhere((K)=> listarClienteM[K]== _dropdownValue4,
163orElse: ()=>null
164);
165return usdKey;
166}
167
168
169
170///////////////////////////////////////////////////////
171 String res = '';
172 Future<String> guardarReserva() async {
173 var respuesta;
174 final response = await http.post(
175 "http://192.168.1.83/LAB-System/api/ajax/cliente.php?op=guardarReserva",
176 body: {
177"nombre":_dropdownValue,
178"motivo":txtNota.text,
179"nombre1":_dropdownValue1,
180"motivo1":txtNota1.text,
181"nombre2":_dropdownValue2,
182"motivo2":txtNota2.text,
183"nombre3":_dropdownValue3,
184"motivo3":txtNota3.text,
185"motivo4":txtNota4.text,
186"motivo5":txtNota5.text,
187"subject":_dropdownValue4,
188
189 });
190 setState(() {
191 respuesta = json.decode(response.body);
192
193 });
194print (respuesta);
195 if (respuesta == true) {
196 res = '';
197 } else {
198 res = 'error ';
199 }
200 }
201
202 // // Boolean variable for CircularProgressIndicator.
203 // bool visible = false ;
204
205 // Future webCall() async{
206
207 // // Showing CircularProgressIndicator using State.
208 // setState(() {
209 // visible = true ;
210 // });
211
212 // // Getting value from Controller
213 // String name = nameController.text;
214 // String studentnumber = studentNumberController.text;
215 // String subject = subjectController.text;
216 // String yearcourse = yearController.text;
217
218 // // API URL
219 // var url = 'http://192.168.1.83/LAB-System/api/submit_data.php';
220
221 // // Store all data with Param Name.
222 // var data = {'name': name, 'student_number' : studentnumber, 'subject': subject, 'course_year': yearcourse,};
223
224 // // Starting Web Call with data.
225 // var response = await http.post(url, body: json.encode(data));
226
227 // // Getting Server response into variable.
228 // var message = jsonDecode(response.body);
229
230 // // If Web call Success than Hide the CircularProgressIndicator.
231 // if(response.statusCode == 200){
232 // setState(() {
233 // visible = false;
234 // });
235 // }
236
237 // // Showing Alert Dialog with Response JSON.
238 // showDialog(
239 // context: context,
240 // builder: (BuildContext context) {
241 // return AlertDialog(
242 // title: new Text(message),
243 // actions: <Widget>[
244 // FlatButton(
245 // child: new Text("OK"),
246 // onPressed: () {
247 // Navigator.of(context).pop();
248 // },
249 // ),
250 // ],
251 // );
252 // },
253 // );
254
255 // }
256
257 @override
258 Widget build(BuildContext context) {
259 return Scaffold(
260 appBar: AppBar(
261 title: Text("Borrowers"),
262 backgroundColor: Colors.greenAccent,
263
264 ),
265 body: Form(
266 key: _formKey,
267 child: ListView(
268 children: <Widget>[
269 // Padding(padding: EdgeInsets.only(top: 20.0),),
270 // Padding(
271 // padding: const EdgeInsets.all(8.0),
272 // child: TextFormField(
273 // decoration: InputDecoration(
274 // hintText: 'Enter your Name',
275 // border: OutlineInputBorder(),
276 // labelText: 'Name',
277 // ),
278 // keyboardType: TextInputType.text,
279 // controller: nameController,
280 // validator: (value) {
281 // if (value.isEmpty) {
282 // return 'Please enter your Name';
283 // }
284 // //Regex for alphabets(name)
285 // if (!RegExp(r'^[a-zA-Z ]*$').hasMatch(value.trim())) {
286 // return 'Enter a valid Name';
287 // } else {
288 // name = value.trim();
289 // }
290
291 // return null;
292 // },
293 // ),
294 // ),
295 // Padding(
296 // padding: const EdgeInsets.all(8.0),
297 // child: TextFormField(
298 // decoration: InputDecoration(
299 // hintText: 'Enter your Student Number',
300 // border: OutlineInputBorder(),
301 // labelText: 'Student Number',
302 // ),
303 // keyboardType: TextInputType.number,
304 // controller: studentNumberController,
305 // validator: (value) {
306 // if (value.isEmpty) {
307 // return 'Please enter your Student Number';
308 // }
309 // if (!RegExp(r'^[0-9]+$').hasMatch(value.trim()) &&
310 // value.trim().length != 10) {
311 // return 'Student number should be in 10 digit';
312 // } else {
313 // student = value.trim();
314 // }
315
316 // return null;
317 // //Regex for email
318 // // if (!RegExp(
319 // // r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
320 // // .hasMatch(value.trim())) {
321 // // return 'Enter a valid Email';
322 // // } else {
323 // // email = value.trim();
324 // // }
325
326 // // return null;
327 // },
328 // ),
329 // ),
330 // Padding(
331 // padding: const EdgeInsets.all(8.0),
332 // child: TextFormField(
333 // decoration: InputDecoration(
334 // hintText: 'Enter your Subject',
335 // border: OutlineInputBorder(),
336 // labelText: 'Subject',
337 // ),
338 // keyboardType: TextInputType.text,
339 // controller: subjectController,
340 // validator: (value) {
341 // if (value.isEmpty) {
342 // return 'Please enter your Subject';
343 // }
344 // //Regex for alphabets(name)
345 // if (!RegExp(r'^[a-zA-Z ]*$').hasMatch(value.trim())) {
346 // return 'Enter a valid Subject';
347 // } else {
348 // subject = value.trim();
349 // }
350
351 // return null;
352 // },
353 // ),
354 // ),
355
356 // Padding(
357 // padding: const EdgeInsets.all(8.0),
358 // child: TextFormField(
359 // decoration: InputDecoration(
360 // hintText: 'Course & Year',
361 // border: OutlineInputBorder(),
362 // labelText: 'Course & Year',
363 // ),
364 // keyboardType: TextInputType.text,
365 // controller: yearController,
366 // validator: (value) {
367 // if (value.isEmpty) {
368 // return 'Please enter your Course & Year';
369 // }
370 // //Regex for alphabets(name)
371 // if (!RegExp(r'^[a-zA-Z ]*$').hasMatch(value.trim())) {
372 // return 'Enter a valid Course & Year';
373 // } else {
374 // year = value.trim();
375 // }
376
377 // return null;
378 // },
379 // ),
380 // ),
381
382
383 // RaisedButton(
384 // onPressed: webCall,
385 // color: Colors.greenAccent,
386 // textColor: Colors.white,
387 // padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
388 // child: Text('Send Borrowers'),
389 // ),
390
391
392
393Padding(padding: EdgeInsets.only(top: 20, left: 20.0),),
394
395 TextField(
396 controller: txtNota4,
397 keyboardType: TextInputType.text,
398 decoration: new InputDecoration(
399 hintText: 'FULL NAME',
400 labelText: "FULL NAME",
401 border: new OutlineInputBorder(
402 borderRadius: new BorderRadius.circular(10.0))),
403
404),
405Padding(padding: EdgeInsets.only(top: 20, left: 20.0),),
406
407TextField(
408 controller: txtNota5,
409 keyboardType: TextInputType.number,
410 decoration: new InputDecoration(
411 hintText: 'Student Number',
412 labelText: "Student Number",
413 border: new OutlineInputBorder(
414 borderRadius: new BorderRadius.circular(10.0))),
415
416),
417 Padding(padding: EdgeInsets.only(top: 20, left: 20.0),),
418
419DropdownButton<String>(
420 value: _dropdownValue4,
421 onChanged: (String newValue) {
422 setState(() {
423 _dropdownValue4 = newValue;
424 });
425 },
426 items: listarClienteM.values
427 .map<DropdownMenuItem<String>>((String value) {
428 return DropdownMenuItem<String>(
429 value:value,
430 child: Text(value),
431 );
432 })
433
434 .toList(),
435 ),
436
437Padding(padding: EdgeInsets.only(top: 20, left: 20.0),),
438
439Row(
440 // mainAxisAlignment: MainAxisAlignment.center,
441 children: <Widget>[
442 SizedBox(width: 50.0,),
443 Expanded(child: DropdownButton<String>(
444 value: _dropdownValue,
445 onChanged: (String newValue) {
446 setState(() {
447 _dropdownValue = newValue;
448 });
449 },
450 items: listarClienteM.values
451 .map<DropdownMenuItem<String>>((String value) {
452 return DropdownMenuItem<String>(
453 value: value,
454 child: Text(value),
455 );
456 })
457
458 .toList(),
459 )),
460
461 Padding(padding: EdgeInsets.only(left: 25.0),),
462
463 Expanded(child: TextField(
464 controller: txtNota,
465 keyboardType: TextInputType.number,
466 decoration: new InputDecoration(
467 hintText: 'Number of Apparatus',
468 labelText: "Quantity",
469 border: new OutlineInputBorder(
470 borderRadius: new BorderRadius.circular(10.0))),
471
472)),
473 ],
474),
475
476
477// Padding(padding: EdgeInsets.only(top: 30),),
478// new TextField(
479// controller: txtNota,
480// keyboardType: TextInputType.number,
481// decoration: new InputDecoration(
482// hintText: 'Number of Apparatus',
483// labelText: "Quantity",
484// border: new OutlineInputBorder(
485// borderRadius: new BorderRadius.circular(20.0))),
486// ),
487
488
489 // second dropdown
490Padding(padding: EdgeInsets.only(top: 20, left: 20.0),),
491Row(
492 // mainAxisAlignment: MainAxisAlignment.center,
493 children: <Widget>[
494 SizedBox(width: 50.0,),
495 Expanded(child: DropdownButton<String>(
496 value: _dropdownValue1,
497 onChanged: (String newValue) {
498 setState(() {
499 _dropdownValue1 = newValue;
500 });
501 },
502
503 items: listarClienteM.values
504 .map<DropdownMenuItem<String>>((String value) {
505 return DropdownMenuItem<String>(
506 value: value,
507 child: Text(value),
508 );
509 })
510 .toList(),
511 )),
512 Padding(padding: EdgeInsets.only(left: 25.0),),
513 Expanded(child: new TextField(
514 controller: txtNota1,
515 keyboardType: TextInputType.number,
516 decoration: new InputDecoration(
517 hintText: 'Number of Apparatus',
518 labelText: "Quantity",
519 border: new OutlineInputBorder(
520 borderRadius: new BorderRadius.circular(10.0))),
521
522
523
524)),
525 ],
526),
527 // 3rd dropdown
528
529 Padding(padding: EdgeInsets.only(top: 20, left: 20.0),),
530Row(
531 // mainAxisAlignment: MainAxisAlignment.center,
532 children: <Widget>[
533 SizedBox(width: 50.0,),
534 Expanded(child: DropdownButton<String>(
535 value: _dropdownValue2,
536 onChanged: (String newValue) {
537 setState(() {
538 _dropdownValue2 = newValue;
539 });
540 },
541 items: listarClienteM.values
542 .map<DropdownMenuItem<String>>((String value) {
543 return DropdownMenuItem<String>(
544 value: value,
545 child: Text(value),
546 );
547 })
548 .toList(),
549 )),
550Padding(padding: EdgeInsets.only(left: 25.0),),
551 Expanded(child:new TextField(
552 controller: txtNota2,
553 keyboardType: TextInputType.number,
554 decoration: new InputDecoration(
555 hintText: 'Number of Apparatus',
556 labelText: "Quantity",
557 border: new OutlineInputBorder(
558 borderRadius: new BorderRadius.circular(10.0))),
559
560
561)),
562 ],
563),
564 // 4th dropdown
565Padding(padding: EdgeInsets.only(top: 20, left: 20.0),),
566Row(
567 // mainAxisAlignment: MainAxisAlignment.center,
568 children: <Widget>[
569 SizedBox(width: 50.0,),
570 Expanded(child: DropdownButton<String>(
571 value: _dropdownValue3,
572 onChanged: (String newValue) {
573 setState(() {
574 _dropdownValue3 = newValue;
575 });
576 },
577 items: listarClienteM.values
578 .map<DropdownMenuItem<String>>((String value) {
579 return DropdownMenuItem<String>(
580 value: value,
581 child: Text(value),
582 );
583 })
584 .toList(),
585 )),
586Padding(padding: EdgeInsets.only(left: 25.0),),
587 Expanded(child: new TextField(
588 controller: txtNota3,
589 keyboardType: TextInputType.number,
590 decoration: new InputDecoration(
591 hintText: 'Number of Apparatus',
592 labelText: "Quantity",
593 border: new OutlineInputBorder(
594 borderRadius: new BorderRadius.circular(10.0))),
595
596)),
597 ],
598),
599 Padding(padding: EdgeInsets.only(top: 25.0),),
600 RaisedButton(
601 shape: RoundedRectangleBorder(
602 borderRadius:
603 BorderRadius.circular(30.0)),
604 onPressed: () {
605 //this validate() validates your whole form and return a boolean if all validates then true if not false
606 // if u wont call validate() your form wont validate.
607 if (_formKey.currentState.validate()) {
608 // this executes after everything is validated
609 print('form validated');
610 // webCall();
611 guardarReserva();
612 } else {
613 print('form not validated');
614 }
615 },
616 child: Text('Send Borrowers'),
617 textColor: Colors.white,
618 color: Colors.greenAccent,
619 ),
620
621 Text(res),
622
623
624 ],
625 ),
626 ),
627 );
628 }
629}