|
@@ -2,12 +2,15 @@ import 'dart:async';
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
+import 'package:provider/provider.dart';
|
|
import 'package:yoshi_papas_app/widgets/widgets_components.dart';
|
|
import 'package:yoshi_papas_app/widgets/widgets_components.dart';
|
|
import '../../themes/themes.dart';
|
|
import '../../themes/themes.dart';
|
|
import '../../models/models.dart';
|
|
import '../../models/models.dart';
|
|
import '../../viewmodels/viewmodels.dart';
|
|
import '../../viewmodels/viewmodels.dart';
|
|
import 'package:collection/collection.dart';
|
|
import 'package:collection/collection.dart';
|
|
|
|
|
|
|
|
+import '../../widgets/widgets.dart';
|
|
|
|
+
|
|
class PedidoForm extends StatefulWidget {
|
|
class PedidoForm extends StatefulWidget {
|
|
@override
|
|
@override
|
|
_PedidoFormState createState() => _PedidoFormState();
|
|
_PedidoFormState createState() => _PedidoFormState();
|
|
@@ -23,6 +26,7 @@ class ItemCarrito {
|
|
}
|
|
}
|
|
|
|
|
|
class _PedidoFormState extends State<PedidoForm> {
|
|
class _PedidoFormState extends State<PedidoForm> {
|
|
|
|
+ final _busqueda = TextEditingController(text: '');
|
|
CategoriaProductoViewModel cvm = CategoriaProductoViewModel();
|
|
CategoriaProductoViewModel cvm = CategoriaProductoViewModel();
|
|
ProductoViewModel pvm = ProductoViewModel();
|
|
ProductoViewModel pvm = ProductoViewModel();
|
|
bool _isLoading = false;
|
|
bool _isLoading = false;
|
|
@@ -51,6 +55,7 @@ class _PedidoFormState extends State<PedidoForm> {
|
|
Map<int, List<int>> selectedToppingsByCategory = {};
|
|
Map<int, List<int>> selectedToppingsByCategory = {};
|
|
Map<String, bool> toppingOptions = {};
|
|
Map<String, bool> toppingOptions = {};
|
|
List<int> _selectedToppings = [];
|
|
List<int> _selectedToppings = [];
|
|
|
|
+ bool _estadoBusqueda = false;
|
|
|
|
|
|
double calcularTotalPedido() {
|
|
double calcularTotalPedido() {
|
|
double total = 0;
|
|
double total = 0;
|
|
@@ -68,6 +73,16 @@ class _PedidoFormState extends State<PedidoForm> {
|
|
_cargarTopingsDisponibles();
|
|
_cargarTopingsDisponibles();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ void _limpiarBusqueda() async {
|
|
|
|
+ setState(() {
|
|
|
|
+ _busqueda.text = '';
|
|
|
|
+ _estadoBusqueda = false;
|
|
|
|
+ });
|
|
|
|
+ // Carga nuevamente las categorías y productos iniciales.
|
|
|
|
+ await cargarCategoriasIniciales();
|
|
|
|
+ // Opcionalmente, puedes llamar a una función específica para cargar productos iniciales si tienes una.
|
|
|
|
+ }
|
|
|
|
+
|
|
void _cargarCategoriasDeTopings() async {
|
|
void _cargarCategoriasDeTopings() async {
|
|
var categoriasObtenidas = await _categoriaTopingViewModel.fetchRegistros();
|
|
var categoriasObtenidas = await _categoriaTopingViewModel.fetchRegistros();
|
|
setState(() {
|
|
setState(() {
|
|
@@ -154,6 +169,45 @@ class _PedidoFormState extends State<PedidoForm> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ Future<void> realizarBusqueda() async {
|
|
|
|
+ setState(() {
|
|
|
|
+ _isLoading = true;
|
|
|
|
+ _estadoBusqueda = true; // Establecer estado de búsqueda
|
|
|
|
+ });
|
|
|
|
+ _busqueda.text = _busqueda.text.trim();
|
|
|
|
+
|
|
|
|
+ // Buscar productos basados en el texto de búsqueda
|
|
|
|
+ //var productosResultantes = await pvm.fetchRegistros(q: _busqueda.text);
|
|
|
|
+ _busqueda.text = _busqueda.text.trim();
|
|
|
|
+ await Provider.of<ProductoViewModel>(context, listen: false)
|
|
|
|
+ .setIsLoading(true);
|
|
|
|
+ await Provider.of<ProductoViewModel>(context, listen: false)
|
|
|
|
+ .setBusqueda(_busqueda.text);
|
|
|
|
+ var productosResultantes =
|
|
|
|
+ await Provider.of<ProductoViewModel>(context, listen: false)
|
|
|
|
+ .fetchRegistros();
|
|
|
|
+ await Provider.of<ProductoViewModel>(context, listen: false)
|
|
|
|
+ .setBusqueda("");
|
|
|
|
+ await Provider.of<ProductoViewModel>(context, listen: false)
|
|
|
|
+ .setIsLoading(false);
|
|
|
|
+
|
|
|
|
+ // Extraer los IDs únicos de las categorías de los productos resultantes
|
|
|
|
+ var idsCategoriasUnicos =
|
|
|
|
+ productosResultantes.map((p) => p.idCategoria).toSet();
|
|
|
|
+
|
|
|
|
+ // Filtrar las categorías localmente para incluir solo las que coinciden con los IDs extraídos
|
|
|
|
+ var categoriasFiltradas = cvm.categoriaProductos
|
|
|
|
+ .where((categoria) => idsCategoriasUnicos.contains(categoria.id))
|
|
|
|
+ .toList();
|
|
|
|
+
|
|
|
|
+ setState(() {
|
|
|
|
+ _isLoading = false;
|
|
|
|
+ productos = productosResultantes;
|
|
|
|
+ categorias = categoriasFiltradas;
|
|
|
|
+ categoriaSeleccionada = categorias.isNotEmpty ? categorias.first : null;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
@override
|
|
@override
|
|
void dispose() {
|
|
void dispose() {
|
|
horizontalScrollController.dispose();
|
|
horizontalScrollController.dispose();
|
|
@@ -180,7 +234,8 @@ class _PedidoFormState extends State<PedidoForm> {
|
|
_isLoading = true;
|
|
_isLoading = true;
|
|
categoriaSeleccionada = categoria;
|
|
categoriaSeleccionada = categoria;
|
|
});
|
|
});
|
|
- productos = await pvm.fetchRegistros(categoriaProducto: categoria);
|
|
|
|
|
|
+ productos =
|
|
|
|
+ await pvm.fetchRegistros(categoriaProducto: categoria, limitee: -1);
|
|
if (!mounted) return;
|
|
if (!mounted) return;
|
|
setState(() => _isLoading = false);
|
|
setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
@@ -475,6 +530,38 @@ class _PedidoFormState extends State<PedidoForm> {
|
|
// Asumiendo que tienes un método para obtener los productos según la categoría seleccionada
|
|
// Asumiendo que tienes un método para obtener los productos según la categoría seleccionada
|
|
return Column(
|
|
return Column(
|
|
children: [
|
|
children: [
|
|
|
|
+ Row(
|
|
|
|
+ children: [
|
|
|
|
+ Expanded(
|
|
|
|
+ flex: 8,
|
|
|
|
+ child: AppTextField(
|
|
|
|
+ prefixIcon: const Icon(Icons.search),
|
|
|
|
+ etiqueta: 'Búsqueda por nombre...',
|
|
|
|
+ controller: _busqueda,
|
|
|
|
+ hintText: 'Búsqueda por nombre...',
|
|
|
|
+ suffixIcon: IconButton(
|
|
|
|
+ icon: Icon(
|
|
|
|
+ Icons.cancel,
|
|
|
|
+ color: AppTheme.primary,
|
|
|
|
+ ),
|
|
|
|
+ onPressed: () {
|
|
|
|
+ _limpiarBusqueda();
|
|
|
|
+ },
|
|
|
|
+ ),
|
|
|
|
+ ),
|
|
|
|
+ ),
|
|
|
|
+ const SizedBox(width: 10),
|
|
|
|
+ Expanded(
|
|
|
|
+ flex: 2,
|
|
|
|
+ child: botonElevated(
|
|
|
|
+ accion: () async {
|
|
|
|
+ realizarBusqueda();
|
|
|
|
+ },
|
|
|
|
+ ),
|
|
|
|
+ ),
|
|
|
|
+ ],
|
|
|
|
+ ),
|
|
|
|
+ const SizedBox(height: 5),
|
|
_buildCategoryButtons(),
|
|
_buildCategoryButtons(),
|
|
const SizedBox(height: 10),
|
|
const SizedBox(height: 10),
|
|
Expanded(
|
|
Expanded(
|
|
@@ -486,6 +573,12 @@ class _PedidoFormState extends State<PedidoForm> {
|
|
itemCount: productos.length,
|
|
itemCount: productos.length,
|
|
itemBuilder: (context, index) {
|
|
itemBuilder: (context, index) {
|
|
final producto = productos[index];
|
|
final producto = productos[index];
|
|
|
|
+ String nombreCategoria = categorias
|
|
|
|
+ .firstWhere(
|
|
|
|
+ (cat) => cat.id == producto.idCategoria,
|
|
|
|
+ orElse: () => CategoriaProducto(nombre: "Desconocida"),
|
|
|
|
+ )
|
|
|
|
+ .nombre!;
|
|
return Card(
|
|
return Card(
|
|
child: InkWell(
|
|
child: InkWell(
|
|
onTap: () {
|
|
onTap: () {
|
|
@@ -518,6 +611,13 @@ class _PedidoFormState extends State<PedidoForm> {
|
|
fontWeight: FontWeight.bold,
|
|
fontWeight: FontWeight.bold,
|
|
color: Color(0xFF008000)),
|
|
color: Color(0xFF008000)),
|
|
),
|
|
),
|
|
|
|
+ _estadoBusqueda
|
|
|
|
+ ? Text(
|
|
|
|
+ nombreCategoria,
|
|
|
|
+ style: const TextStyle(
|
|
|
|
+ fontSize: 16, fontWeight: FontWeight.bold),
|
|
|
|
+ )
|
|
|
|
+ : Container()
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|