Advertisement
danielbrito1987

dados-produtor-page

Dec 18th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 8.48 KB | None | 0 0
  1. import 'package:coocafe_tecnico_app/helpers/produtor_helper.dart';
  2. import 'package:coocafe_tecnico_app/widgets/customShowDialog.dart';
  3. import 'package:flutter/material.dart';
  4.  
  5. class DadosProdutorPage extends StatefulWidget {
  6.   final Produtor produtor;
  7.  
  8.   DadosProdutorPage({this.produtor});
  9.  
  10.   @override
  11.   _DadosProdutorPageState createState() => _DadosProdutorPageState();
  12. }
  13.  
  14. class _DadosProdutorPageState extends State<DadosProdutorPage> {
  15.   Produtor _produtor = Produtor();
  16.   ProdutorHelper prodHelper = ProdutorHelper();
  17.   final _nomeCtrl = TextEditingController();
  18.   final _enderecoCtrl = TextEditingController();
  19.   final _complCtrl = TextEditingController();
  20.   final _bairroCtrl = TextEditingController();
  21.   final _cidadeCtrl = TextEditingController();
  22.   final _ufCtrl = TextEditingController();
  23.   final _cepCtrl = TextEditingController();
  24.   final _telCtrl = TextEditingController();
  25.   final _celCtrl = TextEditingController();
  26.   String telefone = "";
  27.   String celular = "";
  28.   bool _produtorEdited;
  29.  
  30.   @override
  31.   void initState() {
  32.     _produtorEdited = false;
  33.     _produtor = widget.produtor;
  34.  
  35.     _nomeCtrl.text = _produtor.nome;
  36.     _enderecoCtrl.text = _produtor.endereco;
  37.     _complCtrl.text = _produtor.complemento;
  38.     _bairroCtrl.text = _produtor.bairro;
  39.     _cidadeCtrl.text = _produtor.cidade;
  40.     _ufCtrl.text = _produtor.uf;
  41.     _cepCtrl.text = _produtor.cep;
  42.     _telCtrl.text = _produtor.telefone;
  43.     _celCtrl.text = _produtor.celular;
  44.  
  45.     super.initState();
  46.   }
  47.  
  48.   @override
  49.   Widget build(BuildContext context) {
  50.     return WillPopScope(
  51.       onWillPop: _requestPop,
  52.       child: Scaffold(
  53.         appBar: AppBar(
  54.           title: Text("Alterar Produtor"),
  55.           centerTitle: true,
  56.         ),
  57.         body: Column(
  58.           mainAxisSize: MainAxisSize.max,
  59.           children: <Widget>[
  60.             _createFormProdutor(context),
  61.           ],
  62.         ),
  63.       ),
  64.     );
  65.   }
  66.  
  67.   Widget _createFormProdutor(BuildContext context) {
  68.     return Expanded(
  69.       child: ListView(
  70.         padding: EdgeInsets.all(10.0),
  71.         children: <Widget>[
  72.           TextField(
  73.             enabled: false,
  74.             controller: _nomeCtrl,
  75.             keyboardType: TextInputType.text,
  76.             decoration: InputDecoration(
  77.               labelText: "Nome",
  78.             ),
  79.           ),
  80.           TextField(
  81.             enabled: false,
  82.             controller: _enderecoCtrl,
  83.             keyboardType: TextInputType.text,
  84.             decoration: InputDecoration(
  85.               labelText: "Endereço",
  86.             ),
  87.           ),
  88.           TextField(
  89.             enabled: false,
  90.             controller: _complCtrl,
  91.             keyboardType: TextInputType.text,
  92.             decoration: InputDecoration(
  93.               labelText: "Complemento",
  94.             ),
  95.           ),
  96.           TextField(
  97.             enabled: false,
  98.             controller: _bairroCtrl,
  99.             keyboardType: TextInputType.text,
  100.             decoration: InputDecoration(
  101.               labelText: "Bairro",
  102.             ),
  103.           ),
  104.           TextField(
  105.             enabled: false,
  106.             controller: _cidadeCtrl,
  107.             keyboardType: TextInputType.text,
  108.             decoration: InputDecoration(
  109.               labelText: "Cidade",
  110.             ),
  111.           ),
  112.           TextField(
  113.             enabled: false,
  114.             controller: _ufCtrl,
  115.             keyboardType: TextInputType.text,
  116.             decoration: InputDecoration(
  117.               labelText: "UF",
  118.             ),
  119.           ),
  120.           TextField(
  121.             enabled: false,
  122.             controller: _cepCtrl,
  123.             keyboardType: TextInputType.text,
  124.             decoration: InputDecoration(
  125.               labelText: "CEP",
  126.             ),
  127.           ),
  128.           TextField(
  129.             controller: _telCtrl,
  130.             keyboardType: TextInputType.number,
  131.             decoration: InputDecoration(
  132.               labelText: "Telefone",
  133.             ),
  134.             onChanged: (value) {
  135.               setState(() {
  136.                 _produtorEdited = true;
  137.                 _produtor.telefone = value;
  138.               });
  139.             },
  140.           ),
  141.           TextField(
  142.             controller: _celCtrl,
  143.             keyboardType: TextInputType.number,
  144.             decoration: InputDecoration(
  145.               labelText: "Celular",
  146.             ),
  147.             onChanged: (value) {
  148.               setState(() {
  149.                 _produtorEdited = true;
  150.                 _produtor.celular = value;
  151.               });
  152.             },
  153.           ),
  154.           Padding(
  155.             padding: EdgeInsets.only(top: 10.0),
  156.             child: SizedBox(
  157.               height: 44.0,
  158.               child: RaisedButton(
  159.                 color: Colors.blue,
  160.                 textColor: Colors.white,
  161.                 child: Center(
  162.                   child: Row(
  163.                     mainAxisAlignment: MainAxisAlignment.center,
  164.                     children: <Widget>[
  165.                       Icon(Icons.save),
  166.                       Text(
  167.                         "Salvar",
  168.                         style: TextStyle(fontSize: 18.0),
  169.                       )
  170.                     ],
  171.                   ),
  172.                 ),
  173.                 onPressed: () {
  174.                   _produtor.situacao = "A";
  175.                   alterarProdutor(context, _produtor);
  176.                 },
  177.               ),
  178.             ),
  179.           )
  180.         ],
  181.       ),
  182.     );
  183.   }
  184.  
  185.   void alterarProdutor(BuildContext context, Produtor produtor) {
  186.     try{
  187.       prodHelper.update(produtor).then((data) {
  188.         if(data > 0) {
  189.           showDialog(
  190.             barrierDismissible: false,
  191.             context: context,
  192.             builder: (BuildContext context) {
  193.               return CustomAlertDialog(
  194.                 title: Text("Sucesso"),
  195.                 content: Text("Produtor alterado com sucesso."),
  196.                 actions: <Widget>[
  197.                   FlatButton(
  198.                     child: Text("OK"),
  199.                     onPressed: () {
  200.                       Navigator.of(context).pop();
  201.                       Navigator.of(context).pop();
  202.                     },
  203.                   )
  204.                 ],
  205.               );
  206.             }
  207.           );
  208.         } else {
  209.           showDialog(
  210.               barrierDismissible: false,
  211.               context: context,
  212.               builder: (BuildContext context) {
  213.                 return CustomAlertDialog(
  214.                   title: Text("Atenção"),
  215.                   content: Text("Ocorreu um erro ao atualizar o produtor."),
  216.                   actions: <Widget>[
  217.                     FlatButton(
  218.                       child: Text("OK"),
  219.                       onPressed: () {
  220.                         Navigator.of(context).pop();
  221.                       },
  222.                     )
  223.                   ],
  224.                 );
  225.               }
  226.           );
  227.         }
  228.       });
  229.     } catch(e) {
  230.       _showDialog(context, e);
  231.     }
  232.   }
  233.  
  234.   Future<bool> _requestPop() {
  235.     if (_produtorEdited) {
  236.       showDialog(
  237.         context: context,
  238.         barrierDismissible: false,
  239.         builder: (context) {
  240.           return CustomAlertDialog(
  241.             title: Text("Descartar Alterações?"),
  242.             content: Text("Ao sair as alterações serão perdidas."),
  243.             actions: <Widget>[
  244.               FlatButton(
  245.                 child: Text("Sim"),
  246.                 onPressed: () {
  247.                   Navigator.pop(context);
  248.                   Navigator.pop(context);
  249.                 },
  250.               ),
  251.               FlatButton(
  252.                 child: Text("Não"),
  253.                 onPressed: () {
  254.                   Navigator.pop(context);
  255.                 },
  256.               ),
  257.             ],
  258.           );
  259.         }
  260.       );
  261.       return Future.value(false);
  262.     } else {
  263.       return Future.value(true);
  264.     }
  265.   }
  266.  
  267.   void _showDialog(BuildContext context, String texto) {
  268.     // flutter defined function
  269.     showDialog(
  270.       barrierDismissible: false,
  271.       context: context,
  272.       builder: (BuildContext context) {
  273.         // return object of type Dialog
  274.         return CustomAlertDialog(
  275.           title: new Text("Atenção"),
  276.           content: new Text(texto),
  277.           actions: <Widget>[
  278.             // usually buttons at the bottom of the dialog
  279.             new FlatButton(
  280.               child: new Text("OK"),
  281.               onPressed: () {
  282.                 Navigator.of(context).pop();
  283.               },
  284.             ),
  285.           ],
  286.         );
  287.       },
  288.     );
  289.   }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement