variable_form.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. activeColor: AppTheme.primary,
  65. title: const Text(
  66. "Activo",
  67. style: TextStyle(
  68. fontWeight: FontWeight.bold, fontSize: 18),
  69. ),
  70. value: _activo,
  71. onChanged: (bool value) {
  72. setState(() {
  73. _activo = value;
  74. });
  75. },
  76. ),
  77. ],
  78. ),
  79. ),
  80. ),
  81. SizedBox(height: 15),
  82. boton("Guardar", () async {
  83. Provider.of<VariableViewModel>(context, listen: false)
  84. .setIsLoading(true);
  85. widget.variable.nombre = _nombre.text;
  86. widget.variable.clave = _clave.text;
  87. widget.variable.descripcion = _descripcion.text;
  88. widget.variable.activo = _activo;
  89. await Provider.of<VariableViewModel>(context, listen: false)
  90. .updateVariable(widget.variable);
  91. Provider.of<VariableViewModel>(context, listen: false)
  92. .setIsLoading(false);
  93. if (context.mounted) {
  94. Navigator.pop(context);
  95. }
  96. })
  97. ],
  98. ),
  99. ),
  100. );
  101. }
  102. @override
  103. void dispose() {
  104. _nombre.dispose();
  105. _clave.dispose();
  106. _descripcion.dispose();
  107. super.dispose();
  108. }
  109. }