123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- // ignore_for_file: use_build_context_synchronously
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import '../../models/models.dart';
- import '../../themes/themes.dart';
- import '../../viewmodels/viewmodels.dart';
- import '../../widgets/app_textfield.dart';
- import '../../widgets/pagination_buttons.dart';
- import '../../widgets/widgets_components.dart';
- import 'toping_form.dart';
- class TopingScreen extends StatefulWidget {
- const TopingScreen({Key? key}) : super(key: key);
- @override
- State<TopingScreen> createState() => Formulario();
- }
- class Formulario extends State<TopingScreen> {
- final _busqueda = TextEditingController(text: '');
- @override
- void initState() {
- super.initState();
- Future(() async {
- await Provider.of<TopingViewModel>(context, listen: false)
- .fetchRegistros();
- });
- }
- go(Toping item) async {
- Provider.of<TopingViewModel>(context, listen: false).selectModelo(item);
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => const TopingForm(),
- ),
- ).then((value) async {
- await Provider.of<TopingViewModel>(context, listen: false)
- .fetchRegistros();
- });
- }
- @override
- Widget build(BuildContext context) {
- final mvm = Provider.of<TopingViewModel>(context);
- TextStyle estilo = const TextStyle(fontWeight: FontWeight.bold);
- int vuelta = 0;
- List<DataRow> registros = [];
- if (mvm.registros.isNotEmpty) {
- for (Toping item in mvm.registros) {
- var _tipo = vuelta % 2;
- vuelta++;
- registros.add(DataRow(selected: _tipo > 0, cells: [
- DataCell(
- Text(item.clave.toString()),
- onTap: () => go(item),
- ),
- DataCell(
- Text(item.nombre.toString()),
- onTap: () => go(item),
- ),
- DataCell(
- Text(item.descripcion.toString()),
- onTap: () => go(item),
- ),
- DataCell(
- Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
- PopupMenuButton(
- surfaceTintColor: AppTheme.primary,
- itemBuilder: (context) => [
- PopupMenuItem(
- child: const Text('Editar'),
- onTap: () => go(item),
- ),
- PopupMenuItem(
- child: const Text(
- 'Eliminar',
- ),
- onTap: () async {
- return showDialog(
- context: context,
- builder: (context) {
- return AlertDialog(
- title: const Text("Eliminar registro"),
- content: const Text('¿Desea eliminar el registro?'),
- actions: [
- Row(children: [
- Expanded(
- child: TextButton(
- onPressed: () {
- Navigator.pop(context);
- },
- child: const Text('Cancelar'),
- )),
- Expanded(
- child: TextButton(
- onPressed: () async {
- Navigator.pop(context);
- },
- child: const Text('Continuar'),
- ))
- ])
- ],
- );
- },
- );
- },
- )
- ],
- icon: const Icon(Icons.more_vert),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(15)),
- )
- ]))
- ]));
- }
- }
- return Scaffold(
- appBar: AppBar(
- title: const Text('Toping'),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () {
- mvm.selectModelo(Toping());
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => const TopingForm(),
- ),
- ).then((value) async {
- await Provider.of<TopingViewModel>(context, listen: false)
- .fetchRegistros();
- });
- },
- child: const Icon(Icons.add),
- ),
- body: Column(
- children: [
- Expanded(
- child: ListView(
- padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
- children: [
- const SizedBox(height: 8),
- tarjeta(Padding(
- padding: const EdgeInsets.all(10),
- child: Row(
- children: [
- Expanded(
- flex: 8,
- child: AppTextField(
- prefixIcon: const Icon(Icons.search),
- maxLength: 100,
- etiqueta: 'Búsqueda por nombre...',
- controller: _busqueda,
- hintText: 'Búsqueda por nombre...',
- ),
- ),
- const SizedBox(width: 5),
- Expanded(
- flex: 2,
- child: botonElevated(
- accion: () async {
- _busqueda.text = _busqueda.text.trim();
- await Provider.of<TopingViewModel>(context,
- listen: false)
- .setIsLoading(true);
- await Provider.of<TopingViewModel>(context,
- listen: false)
- .setBusqueda(_busqueda.text);
- await Provider.of<TopingViewModel>(context,
- listen: false)
- .fetchRegistros();
- await Provider.of<TopingViewModel>(context,
- listen: false)
- .setBusqueda("");
- await Provider.of<TopingViewModel>(context,
- listen: false)
- .setIsLoading(false);
- },
- ),
- ),
- ],
- ))),
- const SizedBox(height: 8),
- tarjeta(DataTable(
- sortAscending: true,
- sortColumnIndex: 1,
- columns: [
- DataColumn(label: Text("CLAVE", style: estilo)),
- DataColumn(label: Text("NOMBRE", style: estilo)),
- DataColumn(label: Text("DESCRIPCIÓN", style: estilo)),
- DataColumn(label: Text("", style: estilo)),
- ],
- rows: registros)),
- PaginationButtons(
- currentPage: mvm.pagina,
- totalPages: mvm.totalPaginas,
- onPageChanged: (i) => mvm.cambiarPagina(i),
- )
- ],
- ),
- ),
- ],
- ),
- );
- }
- }
|