descuento_screen.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import '../../models/models.dart';
  4. import '../../viewmodels/viewmodels.dart';
  5. class DescuentoScreen extends StatefulWidget {
  6. DescuentoScreen({super.key});
  7. @override
  8. _DescuentoScreenState createState() => _DescuentoScreenState();
  9. }
  10. class _DescuentoScreenState extends State<DescuentoScreen> {
  11. final TextEditingController _porcentajeController = TextEditingController();
  12. String? _errorText;
  13. @override
  14. void initState() {
  15. super.initState();
  16. // Cargar los descuentos al entrar a la pantalla
  17. WidgetsBinding.instance.addPostFrameCallback((_) {
  18. Provider.of<DescuentoViewModel>(context, listen: false)
  19. .cargarDescuentos();
  20. });
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. appBar: AppBar(
  26. title: const Text("Gestionar Descuentos"),
  27. ),
  28. body: Consumer<DescuentoViewModel>(
  29. builder: (context, viewModel, child) {
  30. return Column(
  31. children: [
  32. // Sección para agregar nuevos descuentos
  33. Padding(
  34. padding: const EdgeInsets.all(16.0),
  35. child: Card(
  36. elevation: 4,
  37. shape: RoundedRectangleBorder(
  38. borderRadius: BorderRadius.circular(12),
  39. ),
  40. child: Padding(
  41. padding: const EdgeInsets.all(12.0),
  42. child: Column(
  43. crossAxisAlignment: CrossAxisAlignment.start,
  44. children: [
  45. Text(
  46. "Agregar Nuevo Descuento",
  47. style: TextStyle(
  48. fontWeight: FontWeight.bold,
  49. fontSize: 18,
  50. ),
  51. ),
  52. const SizedBox(height: 10),
  53. Row(
  54. children: [
  55. Expanded(
  56. child: TextField(
  57. controller: _porcentajeController,
  58. decoration: InputDecoration(
  59. labelText: "Porcentaje (%)",
  60. errorText: _errorText,
  61. border: OutlineInputBorder(
  62. borderRadius: BorderRadius.circular(8.0),
  63. ),
  64. ),
  65. keyboardType: TextInputType.number,
  66. ),
  67. ),
  68. const SizedBox(width: 10),
  69. ElevatedButton.icon(
  70. icon: Icon(Icons.add),
  71. label: Text("Agregar"),
  72. onPressed: () {
  73. setState(() {
  74. _errorText = null;
  75. });
  76. if (_porcentajeController.text.isNotEmpty) {
  77. final int porcentaje =
  78. int.parse(_porcentajeController.text);
  79. // Validación para asegurar que el porcentaje no sea mayor a 100
  80. if (porcentaje > 100) {
  81. setState(() {
  82. _errorText =
  83. 'El descuento no puede ser mayor a 100%';
  84. });
  85. } else {
  86. viewModel.guardarDescuento(
  87. Descuento(porcentaje: porcentaje));
  88. _porcentajeController.clear();
  89. }
  90. }
  91. },
  92. style: ElevatedButton.styleFrom(
  93. backgroundColor: Colors.black,
  94. foregroundColor: Colors.white,
  95. padding: const EdgeInsets.symmetric(
  96. vertical: 15, horizontal: 20),
  97. shape: RoundedRectangleBorder(
  98. borderRadius: BorderRadius.circular(8),
  99. ),
  100. ),
  101. ),
  102. ],
  103. ),
  104. ],
  105. ),
  106. ),
  107. ),
  108. ),
  109. const SizedBox(height: 10),
  110. Expanded(
  111. child: Card(
  112. elevation: 2,
  113. margin: const EdgeInsets.symmetric(horizontal: 16.0),
  114. shape: RoundedRectangleBorder(
  115. borderRadius: BorderRadius.circular(12),
  116. ),
  117. child: ListView.builder(
  118. itemCount: viewModel.descuentos.length,
  119. itemBuilder: (context, index) {
  120. final descuento = viewModel.descuentos[index];
  121. return ListTile(
  122. title: Text(
  123. '${descuento.porcentaje}%',
  124. style: TextStyle(
  125. fontSize: 18,
  126. fontWeight: FontWeight.w500,
  127. ),
  128. ),
  129. trailing: IconButton(
  130. icon: const Icon(Icons.delete, color: Colors.red),
  131. onPressed: () =>
  132. viewModel.eliminarDescuento(descuento.id!),
  133. ),
  134. );
  135. },
  136. ),
  137. ),
  138. ),
  139. ],
  140. );
  141. },
  142. ),
  143. );
  144. }
  145. }