Advertisement
danielbrito1987

modal-add-categoria.dart

Apr 3rd, 2020
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.79 KB | None | 0 0
  1. import 'package:PhotoReport/blocs/categoria_bloc.dart';
  2. import 'package:PhotoReport/helpers/categoria_helper.dart';
  3. import 'package:PhotoReport/helpers/obra_helper.dart';
  4. import 'package:PhotoReport/validators/categoria_validatores.dart';
  5. import 'package:flutter/material.dart';
  6.  
  7. class AddCategoriaPage extends StatefulWidget {
  8.   final String action;
  9.   final Obra obra;
  10.   final Categoria categoria;
  11.  
  12.   AddCategoriaPage(this.action, {this.obra, this.categoria});
  13.  
  14.   @override
  15.   _AddCategoriaPageState createState() => _AddCategoriaPageState();
  16. }
  17.  
  18. class _AddCategoriaPageState extends State<AddCategoriaPage>
  19.     with CategoriaValidator {
  20.   final _nomeCtrl = TextEditingController();
  21.   Obra _obra;
  22.   bool _load = false;
  23.   Categoria _editedCategoria;
  24.   final _formKey = GlobalKey<FormState>();
  25.  
  26.   @override
  27.   void initState() {
  28.     super.initState();
  29.  
  30.     _obra = widget.obra;
  31.  
  32.     if (widget.categoria == null) {
  33.       _editedCategoria = Categoria();
  34.       _editedCategoria.obraId = _obra.id;
  35.     } else {
  36.       _editedCategoria = widget.categoria;
  37.       _editedCategoria.obraId = _obra.id;
  38.       _nomeCtrl.text = _editedCategoria.nome;
  39.     }
  40.   }
  41.  
  42.   @override
  43.   Widget build(BuildContext context) {
  44.     return Scaffold(
  45.       appBar: AppBar(
  46.         title: Text(
  47.             widget.action == 'add' ? 'Criar Categoria' : 'Editar Categoria'),
  48.       ),
  49.       body: _load
  50.           ? Column(
  51.               mainAxisAlignment: MainAxisAlignment.center,
  52.               crossAxisAlignment: CrossAxisAlignment.center,
  53.               children: <Widget>[
  54.                 Center(
  55.                   child: CircularProgressIndicator(),
  56.                 ),
  57.                 Text("Carregando...")
  58.               ],
  59.             )
  60.           : Form(
  61.               key: _formKey,
  62.               child: ListView(
  63.                 padding: EdgeInsets.all(10.0),
  64.                 children: <Widget>[
  65.                   TextFormField(
  66.                     controller: _nomeCtrl,
  67.                     textCapitalization: TextCapitalization.words,
  68.                     decoration:
  69.                         InputDecoration(labelText: "Nome", filled: true),
  70.                     validator: validateNome,
  71.                     onChanged: (value) {
  72.                       setState(() {
  73.                         _editedCategoria.nome = value;
  74.                       });
  75.                     },
  76.                   ),
  77.                   const SizedBox(height: 12.0),
  78.                   SizedBox(
  79.                     height: 44.0,
  80.                     child: RaisedButton(
  81.                       color: Colors.blue,
  82.                       textColor: Colors.white,
  83.                       child: Center(
  84.                         child: Row(
  85.                           mainAxisAlignment: MainAxisAlignment.center,
  86.                           children: <Widget>[
  87.                             Text(
  88.                               widget.action == 'add' ? "CADASTRAR" : "ALTERAR",
  89.                               style: TextStyle(fontSize: 18.0),
  90.                             )
  91.                           ],
  92.                         ),
  93.                       ),
  94.                       onPressed: () {
  95.                         if (widget.action == 'add') {
  96.                           setState(() {
  97.                             categoriaBloc.salvar(_editedCategoria);
  98.                             Navigator.of(context).pop(true);
  99.                           });
  100.                         } else {
  101.                           setState(() {
  102.                             categoriaBloc.alterar(_editedCategoria);
  103.                             Navigator.of(context).pop(true);
  104.                           });
  105.                         }
  106.                       },
  107.                     ),
  108.                   )
  109.                 ],
  110.               ),
  111.             ),
  112.     );
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement