article_form.dart 4.61 KB
Newer Older
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
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../http_api/article_service.dart';

class ArticleFormPage extends StatefulWidget {
  @override
  _ArticleFormPageState createState() => _ArticleFormPageState();
}

class _ArticleFormPageState extends State<ArticleFormPage> {
  final _formKey = GlobalKey<FormState>();
  String clave = '';
  int categoria = 1;
  String nombre = '';
  List<double> precios = [0.0, 0.0];
  bool activo = true;

  Future<void> _submitForm() async {
    if (_formKey.currentState?.validate() ?? false) {
      _formKey.currentState?.save();

      final payload = {
        "clave": clave,
        "categoria": categoria,
        "nombre": nombre,
        "precios": precios.map((precio) => {"precio": precio}).toList(),
        "activo": activo,
      };

      final articleService = ArticleService();
      final success = await articleService.addArticle(payload);

      if (success) {
        Navigator.pop(context); // Regresa a la lista de artículos
      } else {
        // Maneja el error
        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'),
        centerTitle: true,
        foregroundColor: Colors.white,
        backgroundColor: Colors.teal,
      ),
      body: Padding(
        padding: EdgeInsets.all(60.0),
        child: Form(
          key: _formKey,
          child: ListView(
            children: [
              TextFormField(
                decoration: InputDecoration(labelText: 'Clave',
              border: OutlineInputBorder()),
                //
                onSaved: (value) => clave = value ?? '',
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Por favor ingresa la clave';
                  }
                  return null;
                },
              ),
              SizedBox(height: 20),

              TextFormField(
                decoration: InputDecoration(labelText: 'Categoría',
                    border: OutlineInputBorder()),
                keyboardType: TextInputType.number,
                //
                onSaved: (value) => categoria = int.parse(value ?? '1'),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Por favor ingresa la categoría';
                  }
                  return null;
                },
              ),
              SizedBox(height: 20),

              TextFormField(
                decoration: InputDecoration(labelText: 'Nombre', border: OutlineInputBorder()),
                onSaved: (value) => nombre = value ?? '',
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Por favor ingresa el nombre';
                  }
                  return null;
                },
              ),
              SizedBox(height: 20),

              TextFormField(
                decoration: InputDecoration(labelText: 'Precio 1', border: OutlineInputBorder()),
                keyboardType: TextInputType.number,
                onSaved: (value) => precios[0] = double.parse(value ?? '0.0'),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Por favor ingresa el precio';
                  }
                  return null;
                },
              ),
              SizedBox(height: 20),

              TextFormField(
                decoration: InputDecoration(labelText: 'Precio 2', border: OutlineInputBorder()),
                keyboardType: TextInputType.number,
                onSaved: (value) => precios[1] = double.parse(value ?? '0.0'),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Por favor ingresa el precio';
                  }
                  return null;
                },
              ),
              SizedBox(height: 20),

              SwitchListTile(
                title: Text('Activo'),
                value: activo,
                onChanged: (value) {
                  setState(() {
                    activo = value;
                  });
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: _submitForm,
                child: Text('Guardar'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}