From a3c2ebea3191c36f1233a1a996fa3a840c72fcda Mon Sep 17 00:00:00 2001 From: = <24mdavid25@gmail.com> Date: Fri, 2 Aug 2024 10:19:46 -0600 Subject: [PATCH] Subiendo actividad 8 Formulario categoria --- lib/src/controllers/category_service.dart | 28 ++++++++++++++++++++++++++++ lib/src/pages/category_form.dart | 51 ++++++++++++++++++++++++++++++++++++++------------- lib/src/pages/home_page.dart | 1 + 3 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 lib/src/controllers/category_service.dart diff --git a/lib/src/controllers/category_service.dart b/lib/src/controllers/category_service.dart new file mode 100644 index 0000000..a23015a --- /dev/null +++ b/lib/src/controllers/category_service.dart @@ -0,0 +1,28 @@ +import 'dart:convert'; +import 'package:http/http.dart' as http; +import 'package:practica1_flutter/src/models/category_model.dart'; + +class CategoryService { + static const String _baseUrl = 'https://basic2.visorus.com.mx'; + Future> addCategory(CategoryModel category) async { + final String url = '$_baseUrl/categoria'; + + final Map categoryData = category.toJson(); + + try { + final http.Response response = await http.post( + Uri.parse(url), + body: json.encode({"data": [categoryData]}), + headers: {'Content-Type': 'application/json'}, + ); + + if (response.statusCode == 200) { + return {"statusCode": response.statusCode, "body": json.decode(response.body)}; + } else { + return {"statusCode": response.statusCode, "body": response.body}; + } + } catch (e) { + return {"statusCode": 501, "body": '$e'}; + } + } +} diff --git a/lib/src/pages/category_form.dart b/lib/src/pages/category_form.dart index f5132a9..85c8fa7 100644 --- a/lib/src/pages/category_form.dart +++ b/lib/src/pages/category_form.dart @@ -1,21 +1,29 @@ import 'package:flutter/material.dart'; -class CategoryForm extends StatefulWidget { - @override - _NewCategoryFormState createState() => _NewCategoryFormState(); -} - -class _NewCategoryFormState extends State { - final _formKey = GlobalKey(); +class CategoryForm extends StatelessWidget { + final GlobalKey _formKey = GlobalKey(); final TextEditingController _claveController = TextEditingController(); final TextEditingController _fechaCreadoController = TextEditingController(); final TextEditingController _nombreController = TextEditingController(); + // Función para obtener la fecha en milisegundos + int _getFechaCreadoEnMilisegundos(String fecha) { + try { + // convertimos el String a DateTime y luego obtenemos el tiempo en milisegundos + DateTime fechaCreado = DateTime.parse(fecha); + return fechaCreado.millisecondsSinceEpoch; + } catch (e) { + // Si hay un error en la conversión, devolvemos 0 + return 0; + } + } + @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Agregar Categoría'), + backgroundColor: Colors.red ), body: Padding( padding: const EdgeInsets.all(16.0), @@ -25,7 +33,9 @@ class _NewCategoryFormState extends State { children: [ TextFormField( controller: _claveController, - decoration: InputDecoration(labelText: 'Clave'), + decoration: InputDecoration(labelText: 'Clave', + border: OutlineInputBorder() + ), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor ingrese la clave'; @@ -33,20 +43,35 @@ class _NewCategoryFormState extends State { return null; }, ), + SizedBox(height: 20), + TextFormField( controller: _fechaCreadoController, - decoration: InputDecoration(labelText: 'Fecha Creado (milisegundos)'), + decoration: InputDecoration( + labelText: 'Fecha Creado (YYYY-MM-DD)', + border: OutlineInputBorder()), validator: (value) { if (value == null || value.isEmpty) { - return 'Por favor ingrese la fecha en milisegundos'; + return 'Por favor ingrese la fecha en formato YYYY-MM-DD'; + } + // Validación de la fecha + try { + DateTime.parse(value); + } catch (e) { + return 'Por favor ingrese una fecha válida'; } return null; }, - keyboardType: TextInputType.number, ), + SizedBox(height: 20), + TextFormField( + //padding: const EdgeInsets.symmetric(horizontal: 80.0), // añado mi margen horizontal controller: _nombreController, - decoration: InputDecoration(labelText: 'Nombre'), + decoration: InputDecoration( + labelText: 'Nombre', + border: OutlineInputBorder(), + ), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor ingrese el nombre'; @@ -61,7 +86,7 @@ class _NewCategoryFormState extends State { // Guardar los valores y mostrar mensaje en consola print({ "clave": _claveController.text, - "fechaCreado": int.parse(_fechaCreadoController.text), + "fechaCreado": _getFechaCreadoEnMilisegundos(_fechaCreadoController.text), "nombre": _nombreController.text, }); } diff --git a/lib/src/pages/home_page.dart b/lib/src/pages/home_page.dart index 7ad551c..1cb7490 100644 --- a/lib/src/pages/home_page.dart +++ b/lib/src/pages/home_page.dart @@ -12,6 +12,7 @@ class HomeSellerPage extends StatelessWidget { child: Scaffold( appBar: AppBar( title: const Text("ShopMaster"), + centerTitle: true, foregroundColor: Colors.white, backgroundColor: Colors.indigoAccent, bottom: const TabBar( -- libgit2 0.27.1