318 lines
12 KiB
Dart
318 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../../providers/app_provider.dart';
|
|
import '../../theme/app_theme.dart';
|
|
import '../../models/producto.dart';
|
|
import '../scanner/scanner_helper.dart';
|
|
|
|
class CatalogoScreen extends StatefulWidget {
|
|
const CatalogoScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<CatalogoScreen> createState() => _CatalogoScreenState();
|
|
}
|
|
|
|
class _CatalogoScreenState extends State<CatalogoScreen> {
|
|
final _searchCtrl = TextEditingController();
|
|
String _categoriaSeleccionada = 'Todos';
|
|
String _query = '';
|
|
|
|
final _categorias = ['Todos', 'Redes & Conectividad', 'Telefonía & Móviles', 'Cómputo & Periféricos', 'Seguridad & Vigilancia', 'Servicios'];
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<AppProvider>();
|
|
final fmt = provider.currencyFormat;
|
|
|
|
final filtrados = provider.productos.where((p) {
|
|
final matchCat = _categoriaSeleccionada == 'Todos' || p.categoria == _categoriaSeleccionada;
|
|
final matchQuery = _query.isEmpty ||
|
|
p.nombre.toLowerCase().contains(_query.toLowerCase()) ||
|
|
p.sku.toLowerCase().contains(_query.toLowerCase());
|
|
return matchCat && matchQuery;
|
|
}).toList();
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppTheme.bgColor,
|
|
appBar: AppBar(
|
|
title: const Text('Catálogo de Productos', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
actions: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 12),
|
|
child: Stack(
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.shopping_cart_outlined),
|
|
onPressed: () {},
|
|
),
|
|
if (provider.cantidadCarrito > 0)
|
|
Positioned(
|
|
right: 6,
|
|
top: 6,
|
|
child: Container(
|
|
padding: const EdgeInsets.all(4),
|
|
decoration: const BoxDecoration(color: Colors.red, shape: BoxShape.circle),
|
|
child: Text(
|
|
'${provider.cantidadCarrito}',
|
|
style: const TextStyle(color: Colors.white, fontSize: 9, fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Container(
|
|
color: AppTheme.primaryColor,
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
|
|
child: TextField(
|
|
controller: _searchCtrl,
|
|
onChanged: (v) => setState(() => _query = v),
|
|
style: const TextStyle(color: Colors.white),
|
|
decoration: InputDecoration(
|
|
hintText: 'Buscar producto o SKU...',
|
|
hintStyle: const TextStyle(color: Colors.white60),
|
|
prefixIcon: const Icon(Icons.search, color: Colors.white70),
|
|
suffixIcon: _query.isNotEmpty
|
|
? IconButton(
|
|
icon: const Icon(Icons.clear, color: Colors.white70),
|
|
onPressed: () {
|
|
_searchCtrl.clear();
|
|
setState(() => _query = '');
|
|
},
|
|
)
|
|
: IconButton(
|
|
tooltip: 'Escanear código',
|
|
icon: const Icon(Icons.qr_code_scanner, color: Colors.white70),
|
|
onPressed: () async {
|
|
final resultado = await abrirScanner(context, titulo: 'Buscar producto');
|
|
if (resultado != null && mounted) {
|
|
_searchCtrl.text = resultado;
|
|
setState(() => _query = resultado);
|
|
}
|
|
},
|
|
),
|
|
filled: true,
|
|
fillColor: Colors.white.withOpacity(0.15),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: const BorderSide(color: Colors.white54),
|
|
),
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
),
|
|
),
|
|
),
|
|
// Categories
|
|
Container(
|
|
height: 44,
|
|
color: Colors.white,
|
|
child: ListView.builder(
|
|
scrollDirection: Axis.horizontal,
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
|
itemCount: _categorias.length,
|
|
itemBuilder: (context, i) {
|
|
final cat = _categorias[i];
|
|
final selected = cat == _categoriaSeleccionada;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(right: 8),
|
|
child: FilterChip(
|
|
label: Text(cat),
|
|
selected: selected,
|
|
onSelected: (_) => setState(() => _categoriaSeleccionada = cat),
|
|
selectedColor: AppTheme.primaryColor,
|
|
checkmarkColor: Colors.white,
|
|
labelStyle: TextStyle(
|
|
color: selected ? Colors.white : AppTheme.textPrimary,
|
|
fontSize: 12,
|
|
fontWeight: selected ? FontWeight.w600 : FontWeight.normal,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
// Count
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
'${filtrados.length} productos',
|
|
style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
// Grid
|
|
Expanded(
|
|
child: GridView.builder(
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 80),
|
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: 2,
|
|
childAspectRatio: 0.72,
|
|
crossAxisSpacing: 12,
|
|
mainAxisSpacing: 12,
|
|
),
|
|
itemCount: filtrados.length,
|
|
itemBuilder: (context, i) => _buildProductCard(context, filtrados[i], provider, fmt),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildProductCard(BuildContext context, Producto producto, AppProvider provider, NumberFormat fmt) {
|
|
final enCarrito = provider.carrito.where((i) => i.producto.id == producto.id);
|
|
final cantidadEnCarrito = enCarrito.isNotEmpty ? enCarrito.first.cantidad : 0;
|
|
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(14),
|
|
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.06), blurRadius: 8, offset: const Offset(0, 3))],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// Product image placeholder
|
|
Container(
|
|
height: 110,
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor.withOpacity(0.06),
|
|
borderRadius: const BorderRadius.vertical(top: Radius.circular(14)),
|
|
),
|
|
child: Stack(
|
|
children: [
|
|
Center(
|
|
child: Text(producto.emoji, style: const TextStyle(fontSize: 52)),
|
|
),
|
|
if (producto.stock < 20)
|
|
Positioned(
|
|
top: 8,
|
|
left: 8,
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.dangerColor,
|
|
borderRadius: BorderRadius.circular(4),
|
|
),
|
|
child: const Text('Bajo stock', style: TextStyle(color: Colors.white, fontSize: 8, fontWeight: FontWeight.bold)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(10),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
producto.nombre,
|
|
style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 12, color: AppTheme.textPrimary),
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
const SizedBox(height: 2),
|
|
Text(
|
|
'SKU: ${producto.sku}',
|
|
style: const TextStyle(fontSize: 10, color: AppTheme.textSecondary),
|
|
),
|
|
const SizedBox(height: 6),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
fmt.format(producto.precio),
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: AppTheme.primaryColor),
|
|
),
|
|
Text(
|
|
'Mayor: ${fmt.format(producto.precioMayorista)}',
|
|
style: const TextStyle(fontSize: 9, color: AppTheme.textSecondary),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
cantidadEnCarrito == 0
|
|
? SizedBox(
|
|
width: double.infinity,
|
|
height: 32,
|
|
child: ElevatedButton(
|
|
onPressed: () => provider.agregarAlCarrito(producto),
|
|
style: ElevatedButton.styleFrom(
|
|
padding: EdgeInsets.zero,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: const [
|
|
Icon(Icons.add, size: 16),
|
|
SizedBox(width: 4),
|
|
Text('Agregar', style: TextStyle(fontSize: 12)),
|
|
],
|
|
),
|
|
),
|
|
)
|
|
: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
_counterBtn(Icons.remove, () => provider.actualizarCantidad(producto.id, cantidadEnCarrito - 1)),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Text(
|
|
'$cantidadEnCarrito',
|
|
style: const TextStyle(fontWeight: FontWeight.bold, color: AppTheme.primaryColor),
|
|
),
|
|
),
|
|
_counterBtn(Icons.add, () => provider.agregarAlCarrito(producto)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _counterBtn(IconData icon, VoidCallback onTap) {
|
|
return InkWell(
|
|
onTap: onTap,
|
|
borderRadius: BorderRadius.circular(6),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(4),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor,
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Icon(icon, size: 16, color: Colors.white),
|
|
),
|
|
);
|
|
}
|
|
}
|