toping_screen.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. await mvm.eliminar(item);
  94. await mvm.fetchRegistros();
  95. if (context.mounted) {
  96. Navigator.pop(context);
  97. }
  98. },
  99. child: const Text('Continuar'),
  100. ))
  101. ])
  102. ],
  103. );
  104. },
  105. );
  106. },
  107. )
  108. ],
  109. icon: const Icon(Icons.more_vert),
  110. shape: RoundedRectangleBorder(
  111. borderRadius: BorderRadius.circular(15)),
  112. )
  113. ]))
  114. ]));
  115. }
  116. }
  117. return Scaffold(
  118. appBar: AppBar(
  119. title: const Text('Toping'),
  120. ),
  121. floatingActionButton: FloatingActionButton(
  122. onPressed: () {
  123. mvm.selectModelo(Toping());
  124. Navigator.push(
  125. context,
  126. MaterialPageRoute(
  127. builder: (context) => const TopingForm(),
  128. ),
  129. ).then((value) async {
  130. await Provider.of<TopingViewModel>(context, listen: false)
  131. .fetchRegistros();
  132. });
  133. },
  134. child: const Icon(Icons.add),
  135. ),
  136. body: Column(
  137. children: [
  138. Expanded(
  139. child: ListView(
  140. padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
  141. children: [
  142. const SizedBox(height: 8),
  143. tarjeta(Padding(
  144. padding: const EdgeInsets.all(10),
  145. child: Row(
  146. children: [
  147. Expanded(
  148. flex: 8,
  149. child: AppTextField(
  150. prefixIcon: const Icon(Icons.search),
  151. maxLength: 100,
  152. etiqueta: 'Búsqueda por nombre...',
  153. controller: _busqueda,
  154. hintText: 'Búsqueda por nombre...',
  155. ),
  156. ),
  157. const SizedBox(width: 5),
  158. Expanded(
  159. flex: 2,
  160. child: botonElevated(
  161. accion: () async {
  162. _busqueda.text = _busqueda.text.trim();
  163. await Provider.of<TopingViewModel>(context,
  164. listen: false)
  165. .setIsLoading(true);
  166. await Provider.of<TopingViewModel>(context,
  167. listen: false)
  168. .setBusqueda(_busqueda.text);
  169. await Provider.of<TopingViewModel>(context,
  170. listen: false)
  171. .fetchRegistros();
  172. await Provider.of<TopingViewModel>(context,
  173. listen: false)
  174. .setBusqueda("");
  175. await Provider.of<TopingViewModel>(context,
  176. listen: false)
  177. .setIsLoading(false);
  178. },
  179. ),
  180. ),
  181. ],
  182. ))),
  183. const SizedBox(height: 8),
  184. tarjeta(DataTable(
  185. sortAscending: true,
  186. sortColumnIndex: 1,
  187. columns: [
  188. DataColumn(label: Text("CLAVE", style: estilo)),
  189. DataColumn(label: Text("NOMBRE", style: estilo)),
  190. DataColumn(label: Text("DESCRIPCIÓN", style: estilo)),
  191. DataColumn(label: Text("", style: estilo)),
  192. ],
  193. rows: registros)),
  194. PaginationButtons(
  195. currentPage: mvm.pagina,
  196. totalPages: mvm.totalPaginas,
  197. onPageChanged: (i) => mvm.cambiarPagina(i),
  198. )
  199. ],
  200. ),
  201. ),
  202. ],
  203. ),
  204. );
  205. }
  206. }