descuento_screen.dart 5.7 KB

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