toping_screen.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'package:flutter/material.dart';
  3. import 'package:provider/provider.dart';
  4. import '../../models/models.dart';
  5. import '../../themes/themes.dart';
  6. import '../../viewmodels/viewmodels.dart';
  7. import '../../widgets/app_textfield.dart';
  8. import '../../widgets/pagination_buttons.dart';
  9. import '../../widgets/widgets_components.dart';
  10. import 'toping_form.dart';
  11. class TopingScreen extends StatefulWidget {
  12. const TopingScreen({Key? key}) : super(key: key);
  13. @override
  14. State<TopingScreen> createState() => Formulario();
  15. }
  16. class Formulario extends State<TopingScreen> {
  17. final _busqueda = TextEditingController(text: '');
  18. @override
  19. void initState() {
  20. super.initState();
  21. Future(() async {
  22. await Provider.of<TopingViewModel>(context, listen: false)
  23. .fetchRegistros();
  24. });
  25. }
  26. go(Toping item) async {
  27. Provider.of<TopingViewModel>(context, listen: false).selectModelo(item);
  28. Navigator.push(
  29. context,
  30. MaterialPageRoute(
  31. builder: (context) => const TopingForm(),
  32. ),
  33. ).then((value) async {
  34. await Provider.of<TopingViewModel>(context, listen: false)
  35. .fetchRegistros();
  36. });
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. final mvm = Provider.of<TopingViewModel>(context);
  41. TextStyle estilo = const TextStyle(fontWeight: FontWeight.bold);
  42. int vuelta = 0;
  43. List<DataRow> registros = [];
  44. if (mvm.registros.isNotEmpty) {
  45. for (Toping item in mvm.registros) {
  46. var _tipo = vuelta % 2;
  47. vuelta++;
  48. registros.add(DataRow(selected: _tipo > 0, cells: [
  49. DataCell(
  50. Text(item.clave.toString()),
  51. onTap: () => go(item),
  52. ),
  53. DataCell(
  54. Text(item.nombre.toString()),
  55. onTap: () => go(item),
  56. ),
  57. DataCell(
  58. Text(item.descripcion.toString()),
  59. onTap: () => go(item),
  60. ),
  61. DataCell(
  62. Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
  63. PopupMenuButton(
  64. surfaceTintColor: AppTheme.primary,
  65. itemBuilder: (context) => [
  66. PopupMenuItem(
  67. child: const Text('Editar'),
  68. onTap: () => go(item),
  69. ),
  70. PopupMenuItem(
  71. child: const Text(
  72. 'Eliminar',
  73. ),
  74. onTap: () async {
  75. return showDialog(
  76. context: context,
  77. builder: (context) {
  78. return AlertDialog(
  79. title: const Text("Eliminar registro"),
  80. content: const Text('¿Desea eliminar el registro?'),
  81. actions: [
  82. Row(children: [
  83. Expanded(
  84. child: TextButton(
  85. onPressed: () {
  86. Navigator.pop(context);
  87. },
  88. child: const Text('Cancelar'),
  89. )),
  90. Expanded(
  91. child: TextButton(
  92. onPressed: () async {
  93. Navigator.pop(context);
  94. },
  95. child: const Text('Continuar'),
  96. ))
  97. ])
  98. ],
  99. );
  100. },
  101. );
  102. },
  103. )
  104. ],
  105. icon: const Icon(Icons.more_vert),
  106. shape: RoundedRectangleBorder(
  107. borderRadius: BorderRadius.circular(15)),
  108. )
  109. ]))
  110. ]));
  111. }
  112. }
  113. return Scaffold(
  114. appBar: AppBar(
  115. title: const Text('Toping'),
  116. ),
  117. floatingActionButton: FloatingActionButton(
  118. onPressed: () {
  119. mvm.selectModelo(Toping());
  120. Navigator.push(
  121. context,
  122. MaterialPageRoute(
  123. builder: (context) => const TopingForm(),
  124. ),
  125. ).then((value) async {
  126. await Provider.of<TopingViewModel>(context, listen: false)
  127. .fetchRegistros();
  128. });
  129. },
  130. child: const Icon(Icons.add),
  131. ),
  132. body: Column(
  133. children: [
  134. Expanded(
  135. child: ListView(
  136. padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
  137. children: [
  138. const SizedBox(height: 8),
  139. tarjeta(Padding(
  140. padding: const EdgeInsets.all(10),
  141. child: Row(
  142. children: [
  143. Expanded(
  144. flex: 8,
  145. child: AppTextField(
  146. prefixIcon: const Icon(Icons.search),
  147. maxLength: 100,
  148. etiqueta: 'Búsqueda por nombre...',
  149. controller: _busqueda,
  150. hintText: 'Búsqueda por nombre...',
  151. ),
  152. ),
  153. const SizedBox(width: 5),
  154. Expanded(
  155. flex: 2,
  156. child: botonElevated(
  157. accion: () async {
  158. _busqueda.text = _busqueda.text.trim();
  159. await Provider.of<TopingViewModel>(context,
  160. listen: false)
  161. .setIsLoading(true);
  162. await Provider.of<TopingViewModel>(context,
  163. listen: false)
  164. .setBusqueda(_busqueda.text);
  165. await Provider.of<TopingViewModel>(context,
  166. listen: false)
  167. .fetchRegistros();
  168. await Provider.of<TopingViewModel>(context,
  169. listen: false)
  170. .setBusqueda("");
  171. await Provider.of<TopingViewModel>(context,
  172. listen: false)
  173. .setIsLoading(false);
  174. },
  175. ),
  176. ),
  177. ],
  178. ))),
  179. const SizedBox(height: 8),
  180. tarjeta(DataTable(
  181. sortAscending: true,
  182. sortColumnIndex: 1,
  183. columns: [
  184. DataColumn(label: Text("CLAVE", style: estilo)),
  185. DataColumn(label: Text("NOMBRE", style: estilo)),
  186. DataColumn(label: Text("DESCRIPCIÓN", style: estilo)),
  187. DataColumn(label: Text("", style: estilo)),
  188. ],
  189. rows: registros)),
  190. PaginationButtons(
  191. currentPage: mvm.pagina,
  192. totalPages: mvm.totalPaginas,
  193. onPageChanged: (i) => mvm.cambiarPagina(i),
  194. )
  195. ],
  196. ),
  197. ),
  198. ],
  199. ),
  200. );
  201. }
  202. }