formulario_articulos.dart 3.69 KB
Newer Older
yenisleydi committed
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
import 'package:flutter/material.dart';
import 'package:primer_practica/src/http_api/articles_api.dart'; // Asegúrate de importar la clase correcta

class FormularioArticulos extends StatefulWidget {
  @override
  _FormularioArticulosState createState() => _FormularioArticulosState();
}

class _FormularioArticulosState extends State<FormularioArticulos> {
  final _formKey = GlobalKey<FormState>();
  String _clave = '';
  int _categoria = 1;
  String _nombre = '';
  List<double> _precios = [0.0, 0.0];
  bool _activo = true;
  final ArticlesApi _articlesApi = ArticlesApi();

  Future<void> _guardarArticulo() async {
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();

      final result = await _articlesApi.addArticle(
        clave: _clave,
        categoria: _categoria,
        nombre: _nombre,
        precios: _precios,
        activo: _activo,
      );

      if (result['statusCode'] == 200) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Artículo guardado exitosamente')),
        );
        Navigator.pop(context); // Regresa a la lista de artículos
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Error al guardar el artículo')),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Formulario de Artículo'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: ListView(
            children: [
              _buildTextField(
                label: 'Clave',
                onSaved: (value) => _clave = value!,
                inputType: TextInputType.text,
              ),
              _buildTextField(
                label: 'Categoría',
                onSaved: (value) => _categoria = int.parse(value!),
                inputType: TextInputType.number,
              ),
              _buildTextField(
                label: 'Nombre',
                onSaved: (value) => _nombre = value!,
                inputType: TextInputType.text,
              ),
              _buildTextField(
                label: 'Precio 1',
                onSaved: (value) => _precios[0] = double.parse(value!),
                inputType: TextInputType.number,
              ),
              _buildTextField(
                label: 'Precio 2',
                onSaved: (value) => _precios[1] = double.parse(value!),
                inputType: TextInputType.number,
              ),
              SizedBox(height: 30),
              Center(
                child: ElevatedButton(
                  onPressed: _guardarArticulo,
                  style: ElevatedButton.styleFrom(
                    padding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
                    textStyle: TextStyle(fontSize: 16),
                  ),
                  child: Text('Guardar'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTextField({
    required String label,
    required FormFieldSetter<String> onSaved,
    required TextInputType inputType,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 12.0),
      child: TextFormField(
        decoration: InputDecoration(
          labelText: label,
          border: OutlineInputBorder(),
          contentPadding: EdgeInsets.symmetric(vertical: 15, horizontal: 10),
        ),
        keyboardType: inputType,
        onSaved: onSaved,
        validator: (value) {
          if (value == null || value.isEmpty) {
            return 'Este campo es obligatorio';
          }
          return null;
        },
      ),
    );
  }
}