variable_form.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import '../../models/models.dart';
  4. import '../../viewmodels/variable_view_model.dart';
  5. import '../../themes/themes.dart';
  6. import '../../widgets/widgets.dart';
  7. class VariableForm extends StatefulWidget {
  8. final Variable variable;
  9. const VariableForm({Key? key, required this.variable}) : super(key: key);
  10. @override
  11. _VariableFormState createState() => _VariableFormState();
  12. }
  13. class _VariableFormState extends State<VariableForm> {
  14. final _nombre = TextEditingController();
  15. final _clave = TextEditingController();
  16. final _descripcion = TextEditingController();
  17. bool _activo = true;
  18. @override
  19. void initState() {
  20. super.initState();
  21. _nombre.text = widget.variable.nombre ?? "";
  22. _clave.text = widget.variable.clave ?? "";
  23. _descripcion.text = widget.variable.descripcion ?? "";
  24. _activo = widget.variable.activo ?? true;
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. return Scaffold(
  29. appBar: AppBar(
  30. title: Text(
  31. widget.variable.id == null ? 'Nueva Variable' : 'Editar Variable',
  32. style: TextStyle(color: AppTheme.secondary),
  33. ),
  34. iconTheme: IconThemeData(color: AppTheme.secondary),
  35. ),
  36. body: SingleChildScrollView(
  37. padding: EdgeInsets.all(8),
  38. child: Column(
  39. children: [
  40. tarjeta(
  41. Padding(
  42. padding: const EdgeInsets.all(8),
  43. child: Column(
  44. children: [
  45. AppTextField(
  46. maxLength: 100,
  47. etiqueta: 'Nombre',
  48. controller: _nombre,
  49. hintText: 'Nombre de la variable',
  50. ),
  51. AppTextField(
  52. maxLength: 100,
  53. etiqueta: 'Clave',
  54. controller: _clave,
  55. hintText: 'Clave de la variable',
  56. ),
  57. AppTextField(
  58. maxLength: 200,
  59. etiqueta: 'Descripción',
  60. controller: _descripcion,
  61. hintText: 'Descripción de la variable',
  62. ),
  63. SwitchListTile(
  64. title: Text("Activo"),
  65. value: _activo,
  66. onChanged: (bool value) {
  67. setState(() {
  68. _activo = value;
  69. });
  70. },
  71. ),
  72. ],
  73. ),
  74. ),
  75. ),
  76. SizedBox(height: 15),
  77. boton("Guardar", () async {
  78. Provider.of<VariableViewModel>(context, listen: false)
  79. .setIsLoading(true);
  80. widget.variable.nombre = _nombre.text;
  81. widget.variable.clave = _clave.text;
  82. widget.variable.descripcion = _descripcion.text;
  83. widget.variable.activo = _activo;
  84. await Provider.of<VariableViewModel>(context, listen: false)
  85. .updateVariable(widget.variable);
  86. Provider.of<VariableViewModel>(context, listen: false)
  87. .setIsLoading(false);
  88. if (context.mounted) {
  89. Navigator.pop(context);
  90. }
  91. })
  92. ],
  93. ),
  94. ),
  95. );
  96. }
  97. @override
  98. void dispose() {
  99. _nombre.dispose();
  100. _clave.dispose();
  101. _descripcion.dispose();
  102. super.dispose();
  103. }
  104. }