Created
December 1, 2023 17:30
-
-
Save carloswm85/1488903ae260f581edce198c0ace92ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
| import 'package:go_router/go_router.dart'; | |
| import 'package:miningtrackerapp/config/config.dart'; | |
| import 'package:miningtrackerapp/domain/entities/prueba.dart'; | |
| import 'package:miningtrackerapp/presentation/providers/pruebas/pruebas_api_provider.dart'; | |
| import 'package:miningtrackerapp/presentation/presentation_widgets.dart'; | |
| import 'package:miningtrackerapp/presentation/providers/pruebas/pruebas_repository_provider.dart'; | |
| class PruebasItemScreen extends ConsumerStatefulWidget { | |
| static const name = 'prueba-screen'; | |
| final String pruebaId; | |
| const PruebasItemScreen({super.key, required this.pruebaId}); | |
| @override | |
| PruebasItemScreenState createState() => PruebasItemScreenState(); | |
| } | |
| class PruebasItemScreenState extends ConsumerState<PruebasItemScreen> { | |
| GlobalKey<FormState> _formKey = GlobalKey<FormState>(); | |
| final _isVisible = false; | |
| final _controllerIdHidden = TextEditingController(); | |
| final _controllerFlotante = TextEditingController(); | |
| final _controllerEntero = TextEditingController(); | |
| final _controllerBooleano = SwitchController(); | |
| final _controllerNombre = TextEditingController(); | |
| final _controllerApellido = TextEditingController(); | |
| final _controllerTelefono = TextEditingController(); | |
| final _controllerEmail = TextEditingController(); | |
| final _controllerFecha = TextEditingController(); | |
| final _controllerTiempo = TextEditingController(); | |
| final _controllerArchivoNombre = TextEditingController(); | |
| final _controllerArchivoRuta = TextEditingController(); | |
| final _controllerContrasenia = TextEditingController(); | |
| final _controllerUsuario = TextEditingController(); | |
| Future<void> _submitFormSave() async { | |
| final isValid = _formKey.currentState!.validate(); | |
| if (!isValid) return; | |
| // Collect values | |
| final id = _controllerIdHidden.text; | |
| final flotante = _controllerFlotante.text; | |
| final entero = _controllerEntero.text; | |
| final booleano = _controllerBooleano.isSwitchOn; | |
| final nombre = _controllerNombre.text; | |
| final apellido = _controllerApellido.text; | |
| final telefono = _controllerTelefono.text; | |
| final email = _controllerEmail.text; | |
| final fecha = _controllerFecha.text; | |
| final tiempo = _controllerTiempo.text; | |
| final archivoNombre = _controllerArchivoNombre.text; | |
| final archivoRuta = _controllerArchivoRuta.text; | |
| final contrasenia = _controllerContrasenia.text; | |
| final usuario = _controllerUsuario.text; | |
| final pruebaNueva = Prueba( | |
| pruebaId: {}, | |
| id: int.parse(id), | |
| flotante: double.parse(flotante), | |
| entero: int.parse(entero), | |
| booleano: booleano, | |
| nombre: nombre, | |
| apellido: apellido, | |
| telefono: telefono, | |
| email: email, | |
| fecha: HelperConverter.getDateTime(fecha), | |
| tiempo: tiempo, | |
| archivoNombre: archivoNombre, | |
| archivoRuta: archivoRuta, | |
| contrasenia: contrasenia, | |
| usuario: usuario); | |
| await ref.read(providerPruebasSaveItem.notifier).savePruebaDB(pruebaNueva); | |
| await ref.read(providerPruebasList.notifier).updatePruebasList(); | |
| _moveToPruebasList(); | |
| } | |
| Future<void> _submitFormDelete() async { | |
| final isValid = _formKey.currentState!.validate(); | |
| if (!isValid) return; | |
| // Collect value for deletion | |
| final id = _controllerIdHidden.text; | |
| // ref.read(providerPruebasDeleteItem.notifier).deletePruebaDB(id as int); | |
| await ref.read(pruebasRepositoryProvider).deletePrueba(int.parse(id)); | |
| await ref.read(providerPruebasList.notifier).updatePruebasList(); | |
| _moveToPruebasList(); | |
| } | |
| _moveToPruebasList() { | |
| ref.invalidate(providerPruebasGetById); | |
| context.go('/pruebas'); | |
| } | |
| //NOTE - INIT | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _formKey = GlobalKey<FormState>(); | |
| ref.read(providerPruebasSaveItem.notifier).savePruebaDB; | |
| ref.read(providerPruebasGetById.notifier).loadPrueba(widget.pruebaId); | |
| } | |
| @override | |
| void dispose() { | |
| _controllerIdHidden.dispose(); | |
| _controllerFlotante.dispose(); | |
| _controllerEntero.dispose(); | |
| _controllerBooleano.dispose(); | |
| _controllerNombre.dispose(); | |
| _controllerApellido.dispose(); | |
| _controllerTelefono.dispose(); | |
| _controllerEmail.dispose(); | |
| _controllerFecha.dispose(); | |
| _controllerTiempo.dispose(); | |
| _controllerArchivoNombre.dispose(); | |
| _controllerArchivoRuta.dispose(); | |
| _controllerContrasenia.dispose(); | |
| _controllerUsuario.dispose(); | |
| super.dispose(); | |
| } | |
| //NOTE - BUILD | |
| @override | |
| Widget build(BuildContext context) { | |
| final pruebaMap = ref.watch(providerPruebasGetById); | |
| final Prueba? pruebaResponse = pruebaMap[widget.pruebaId]; | |
| if (pruebaResponse == null) { | |
| return const Scaffold( | |
| body: Center( | |
| child: CircularProgressIndicator( | |
| strokeWidth: 2, | |
| ), | |
| ), | |
| ); | |
| } | |
| PruebaForm prueba = pruebaResponse!.toPruebaForm(); | |
| _controllerIdHidden.text = prueba.id; | |
| _controllerFlotante.text = prueba.flotante; | |
| _controllerEntero.text = prueba.entero; | |
| _controllerBooleano.setValue(prueba.booleano); | |
| _controllerNombre.text = prueba.nombre; | |
| _controllerApellido.text = prueba.apellido; | |
| _controllerTelefono.text = prueba.telefono; | |
| _controllerEmail.text = prueba.email; | |
| _controllerFecha.text = prueba.fecha; | |
| _controllerTiempo.text = prueba.tiempo; | |
| _controllerArchivoNombre.text = prueba.archivoNombre; | |
| _controllerArchivoRuta.text = prueba.archivoRuta; | |
| _controllerContrasenia.text = prueba.contrasenia; | |
| _controllerUsuario.text = prueba.usuario; | |
| // APPBAR | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Pruebas Item'), | |
| elevation: 5, | |
| leading: IconButton( | |
| onPressed: () { | |
| context.go('/pruebas'); | |
| }, | |
| icon: const Icon(Icons.arrow_back)), | |
| ), | |
| body: CustomScrollView( | |
| physics: const ClampingScrollPhysics(), | |
| slivers: <Widget>[ | |
| //ANCHOR - SliverList | |
| SliverList( | |
| delegate: SliverChildBuilderDelegate( | |
| (context, index) { | |
| return Padding( | |
| padding: const EdgeInsets.all(20.0), | |
| child: Form( | |
| key: _formKey, | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| // HIDDEN ID FIELD | |
| Visibility( | |
| visible: _isVisible, | |
| child: TextFormField( | |
| controller: _controllerIdHidden, | |
| decoration: const InputDecoration( | |
| labelText: 'Hidden Field'), | |
| validator: (value) { | |
| // Add your validation logic here | |
| if (value!.isEmpty) { | |
| return 'Hidden field cannot be empty'; | |
| } | |
| return null; // Validation passed | |
| }, | |
| )), | |
| // USER NAME | |
| CustomTextFormField( | |
| label: 'Usuario', | |
| controller: _controllerUsuario, | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.toString().isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.length < 6) { | |
| return 'Más de 6 carácteres requerido'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // PASSWORD | |
| CustomTextFormField( | |
| label: 'Contraseña', | |
| obscureText: true, | |
| hasObscuredText: true, | |
| controller: _controllerContrasenia, | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.toString().isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.length < 6) { | |
| return 'Más de 6 carácteres requerido'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // FIRST NAME | |
| CustomTextFormField( | |
| label: 'Nombre', | |
| controller: _controllerNombre, | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.toString().isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.length < 4) { | |
| return 'Más de 6 carácteres requerido'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // LAST NAME | |
| CustomTextFormField( | |
| label: 'Apellido', | |
| controller: _controllerApellido, | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.toString().isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.length < 4) { | |
| return 'Más de 6 carácteres requerido'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // PHONE NUMBER | |
| CustomTextFormField( | |
| label: 'Teléfono', | |
| controller: _controllerTelefono, | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.toString().isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.length < 6) { | |
| return 'Más de 6 carácteres requerido'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| CustomTextFormField( | |
| label: 'Email', | |
| controller: _controllerEmail, | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| if (value.toString().isEmpty) { | |
| return 'Campo requerido'; | |
| } | |
| final emailRegExp = RegExp( | |
| r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$', | |
| ); | |
| if (!emailRegExp.hasMatch(value)) { | |
| return 'No tiene formato de correo'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // ENTERO | |
| CustomTextFormField( | |
| label: 'Número entero', | |
| keyboardType: TextInputType.number, | |
| controller: _controllerEntero, | |
| validator: (value) { | |
| if (value == null) { | |
| return null; | |
| } | |
| final n = int.tryParse(value); | |
| if (n == null) { | |
| return '"$value" no es un número entero'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // DECIMAL | |
| CustomTextFormField( | |
| label: 'Número decimal', | |
| keyboardType: TextInputType.number, | |
| controller: _controllerFlotante, | |
| validator: (value) { | |
| if (value == null) { | |
| return null; | |
| } | |
| final n = num.tryParse(value); | |
| if (n == null) { | |
| return '"$value" no es un número decimal'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // BOOLEANO | |
| CustomSwitchFormField( | |
| title: 'Booleano', | |
| subtitle: 'This is the subtitle', | |
| controller: _controllerBooleano, | |
| ), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // FECHA | |
| CustomDatePicker( | |
| label: 'Fecha', controller: _controllerFecha), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // HORA | |
| CustomTimePicker( | |
| label: 'Hora, minutos y segundos', | |
| controller: _controllerTiempo), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // BUTTON | |
| FilledButton.icon( | |
| // onPressed: _submitForm, | |
| onPressed: _submitFormSave, | |
| icon: const Icon(Icons.save_as_outlined), | |
| label: const Text('Guardar')), | |
| const SizedBox( | |
| height: 20.0, | |
| ), | |
| // BUTTON | |
| FilledButton.icon( | |
| onPressed: () => showDialog( | |
| context: context, | |
| builder: (context) => CustomDialogAlert( | |
| title: '¿Desea proceder?', | |
| description: | |
| 'Esta acción de BORRADO no puede ser deshecha.', | |
| buttonLeft: 'Cancelar', | |
| buttonRight: 'Aceptar', | |
| callbackFunction: _submitFormDelete)), | |
| icon: const Icon(Icons.delete_outline_outlined), | |
| label: const Text('Borrar')), | |
| ], | |
| ), | |
| ), | |
| ); | |
| }, | |
| childCount: 1, | |
| ), | |
| ), | |
| ], | |
| ), | |
| ); | |
| } | |
| //!SECTION - BUILD | |
| } | |
| // TODO - https://pub.dev/packages/flutter_spinbox | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment