516 lines
20 KiB
Dart
516 lines
20 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/cliente.dart';
|
|
import '../firma/firma_screen.dart';
|
|
import '../scanner/scanner_helper.dart';
|
|
import '../clientes/cliente_selector_screen.dart';
|
|
|
|
class PedidoFormScreen extends StatefulWidget {
|
|
const PedidoFormScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<PedidoFormScreen> createState() => _PedidoFormScreenState();
|
|
}
|
|
|
|
class _PedidoFormScreenState extends State<PedidoFormScreen> {
|
|
final _notasCtrl = TextEditingController();
|
|
final _searchCtrl = TextEditingController();
|
|
String _productoQuery = '';
|
|
bool _showSearch = false;
|
|
|
|
NumberFormat get _fmt => context.watch<AppProvider>().currencyFormat;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final provider = context.read<AppProvider>();
|
|
_notasCtrl.text = provider.notasPedido;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_notasCtrl.dispose();
|
|
_searchCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<AppProvider>();
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppTheme.bgColor,
|
|
appBar: AppBar(
|
|
title: const Text('Nuevo Pedido', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.delete_outline),
|
|
onPressed: () {
|
|
showDialog(
|
|
context: context,
|
|
builder: (_) => AlertDialog(
|
|
title: const Text('Cancelar pedido'),
|
|
content: const Text('¿Descartar el pedido en curso?'),
|
|
actions: [
|
|
TextButton(onPressed: () => Navigator.pop(context), child: const Text('No')),
|
|
TextButton(
|
|
onPressed: () {
|
|
provider.limpiarCarrito();
|
|
Navigator.pop(context);
|
|
Navigator.pop(context);
|
|
},
|
|
child: const Text('Sí, descartar', style: TextStyle(color: AppTheme.dangerColor)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Expanded(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_buildClienteSection(context, provider),
|
|
const SizedBox(height: 16),
|
|
_buildProductoSearch(context, provider),
|
|
if (_showSearch) _buildSearchResults(context, provider),
|
|
const SizedBox(height: 16),
|
|
_buildCarrito(context, provider),
|
|
const SizedBox(height: 16),
|
|
_buildNotasSection(context, provider),
|
|
const SizedBox(height: 80),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
_buildBottomBar(context, provider),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildClienteSection(BuildContext context, AppProvider provider) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text('Cliente', style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
|
|
const SizedBox(height: 8),
|
|
if (provider.clienteSeleccionado != null)
|
|
_buildClienteCard(provider.clienteSeleccionado!)
|
|
else
|
|
_buildClienteSelector(context, provider),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildClienteCard(Cliente cliente) {
|
|
return Container(
|
|
padding: const EdgeInsets.all(14),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: AppTheme.primaryColor.withOpacity(0.3)),
|
|
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.04), blurRadius: 6)],
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 44,
|
|
height: 44,
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: const Icon(Icons.store, color: AppTheme.primaryColor, size: 24),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(cliente.nombre, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14)),
|
|
Text(cliente.direccion, style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary), maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
Text('NIT: ${cliente.nit}', style: const TextStyle(fontSize: 11, color: AppTheme.textSecondary)),
|
|
],
|
|
),
|
|
),
|
|
const Icon(Icons.check_circle, color: AppTheme.successColor, size: 22),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildClienteSelector(BuildContext context, AppProvider provider) {
|
|
return Material(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
elevation: 1,
|
|
child: InkWell(
|
|
borderRadius: BorderRadius.circular(12),
|
|
onTap: () => _abrirSelectorCliente(context, provider),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: const Color(0xFFCBD5E1)),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Row(
|
|
children: const [
|
|
Icon(Icons.store_outlined, color: AppTheme.textSecondary),
|
|
SizedBox(width: 12),
|
|
Text('Seleccionar cliente...', style: TextStyle(color: AppTheme.textSecondary)),
|
|
Spacer(),
|
|
Icon(Icons.arrow_drop_down, color: AppTheme.textSecondary),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _abrirSelectorCliente(BuildContext context, AppProvider provider) async {
|
|
final cliente = await Navigator.push<Cliente>(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => ClienteSelectorScreen(clientes: provider.clientes),
|
|
),
|
|
);
|
|
if (cliente != null) provider.seleccionarCliente(cliente);
|
|
}
|
|
|
|
Widget _buildProductoSearch(BuildContext context, AppProvider provider) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text('Agregar productos', style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
|
|
const SizedBox(height: 8),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _searchCtrl,
|
|
onTap: () => setState(() => _showSearch = true),
|
|
onChanged: (v) => setState(() => _productoQuery = v),
|
|
decoration: InputDecoration(
|
|
hintText: 'Buscar producto o SKU...',
|
|
prefixIcon: const Icon(Icons.search),
|
|
suffixIcon: _showSearch
|
|
? IconButton(
|
|
icon: const Icon(Icons.close),
|
|
onPressed: () {
|
|
_searchCtrl.clear();
|
|
setState(() {
|
|
_productoQuery = '';
|
|
_showSearch = false;
|
|
});
|
|
},
|
|
)
|
|
: null,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
Material(
|
|
color: AppTheme.primaryColor,
|
|
borderRadius: BorderRadius.circular(10),
|
|
child: InkWell(
|
|
borderRadius: BorderRadius.circular(10),
|
|
onTap: () async {
|
|
final resultado = await abrirScanner(context, titulo: 'Escanear producto');
|
|
if (resultado == null || !mounted) return;
|
|
final coincidencias = provider.productos.where(
|
|
(p) => p.sku.toLowerCase() == resultado.toLowerCase() ||
|
|
p.nombre.toLowerCase().contains(resultado.toLowerCase()),
|
|
).toList();
|
|
final producto = coincidencias.isEmpty ? null : coincidencias.first;
|
|
if (producto != null) {
|
|
provider.agregarAlCarrito(producto);
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text('${producto.nombre} agregado al pedido'),
|
|
backgroundColor: AppTheme.successColor,
|
|
duration: const Duration(seconds: 2),
|
|
));
|
|
} else {
|
|
// Si no coincide exacto, poner en el campo de búsqueda
|
|
_searchCtrl.text = resultado;
|
|
setState(() {
|
|
_productoQuery = resultado;
|
|
_showSearch = true;
|
|
});
|
|
}
|
|
},
|
|
child: const Padding(
|
|
padding: EdgeInsets.all(14),
|
|
child: Icon(Icons.qr_code_scanner, color: Colors.white, size: 22),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildSearchResults(BuildContext context, AppProvider provider) {
|
|
final results = provider.productos.where((p) {
|
|
if (_productoQuery.isEmpty) return true;
|
|
return p.nombre.toLowerCase().contains(_productoQuery.toLowerCase()) ||
|
|
p.sku.toLowerCase().contains(_productoQuery.toLowerCase());
|
|
}).take(6).toList();
|
|
|
|
return Container(
|
|
margin: const EdgeInsets.only(top: 4),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.08), blurRadius: 8)],
|
|
),
|
|
child: Column(
|
|
children: results.map((p) {
|
|
final enCarrito = provider.carrito.where((i) => i.producto.id == p.id);
|
|
final qty = enCarrito.isNotEmpty ? enCarrito.first.cantidad : 0;
|
|
return ListTile(
|
|
dense: true,
|
|
leading: Text(p.emoji, style: const TextStyle(fontSize: 24)),
|
|
title: Text(p.nombre, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w500)),
|
|
subtitle: Text(_fmt.format(p.precio), style: const TextStyle(fontSize: 12, color: AppTheme.primaryColor)),
|
|
trailing: qty == 0
|
|
? IconButton(
|
|
icon: const Icon(Icons.add_circle, color: AppTheme.primaryColor),
|
|
onPressed: () => provider.agregarAlCarrito(p),
|
|
)
|
|
: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.remove_circle_outline, size: 20),
|
|
onPressed: () => provider.actualizarCantidad(p.id, qty - 1),
|
|
padding: EdgeInsets.zero,
|
|
constraints: const BoxConstraints(),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 6),
|
|
child: Text('$qty', style: const TextStyle(fontWeight: FontWeight.bold)),
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.add_circle, size: 20, color: AppTheme.primaryColor),
|
|
onPressed: () => provider.agregarAlCarrito(p),
|
|
padding: EdgeInsets.zero,
|
|
constraints: const BoxConstraints(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildCarrito(BuildContext context, AppProvider provider) {
|
|
if (provider.carrito.isEmpty) {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 32),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: const Color(0xFFE2E8F0), style: BorderStyle.solid),
|
|
),
|
|
child: Center(
|
|
child: Column(
|
|
children: const [
|
|
Icon(Icons.shopping_cart_outlined, size: 40, color: AppTheme.textSecondary),
|
|
SizedBox(height: 8),
|
|
Text('Carrito vacío', style: TextStyle(color: AppTheme.textSecondary)),
|
|
Text('Busca y agrega productos arriba', style: TextStyle(fontSize: 12, color: AppTheme.textSecondary)),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text('Carrito (${provider.carrito.length} productos)',
|
|
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
|
|
TextButton.icon(
|
|
icon: const Icon(Icons.delete_outline, size: 16),
|
|
label: const Text('Limpiar', style: TextStyle(fontSize: 12)),
|
|
onPressed: provider.limpiarCarrito,
|
|
style: TextButton.styleFrom(foregroundColor: AppTheme.dangerColor),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.04), blurRadius: 6)],
|
|
),
|
|
child: Column(
|
|
children: [
|
|
...provider.carrito.map((item) => Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
|
decoration: BoxDecoration(
|
|
border: Border(bottom: BorderSide(color: Colors.grey.shade100)),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Text(item.producto.emoji, style: const TextStyle(fontSize: 20)),
|
|
const SizedBox(width: 10),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(item.producto.nombre, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w500)),
|
|
Text('${_fmt.format(item.precioUnitario)} x ${item.cantidad}',
|
|
style: const TextStyle(fontSize: 11, color: AppTheme.textSecondary)),
|
|
],
|
|
),
|
|
),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
_counterBtn(Icons.remove, () => provider.actualizarCantidad(item.producto.id, item.cantidad - 1)),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
child: Text('${item.cantidad}', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14)),
|
|
),
|
|
_counterBtn(Icons.add, () => provider.agregarAlCarrito(item.producto)),
|
|
],
|
|
),
|
|
const SizedBox(width: 8),
|
|
Text(
|
|
_fmt.format(item.subtotal),
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: AppTheme.primaryColor),
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.close, size: 16, color: AppTheme.textSecondary),
|
|
padding: EdgeInsets.zero,
|
|
constraints: const BoxConstraints(),
|
|
onPressed: () => provider.quitarDelCarrito(item.producto.id),
|
|
),
|
|
],
|
|
),
|
|
)).toList(),
|
|
// Totals
|
|
Container(
|
|
padding: const EdgeInsets.all(14),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor.withOpacity(0.04),
|
|
borderRadius: const BorderRadius.vertical(bottom: Radius.circular(12)),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('${provider.cantidadCarrito} unidades', style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary)),
|
|
const Text('Total a pagar:', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500)),
|
|
],
|
|
),
|
|
Text(
|
|
_fmt.format(provider.totalCarrito),
|
|
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: AppTheme.primaryColor),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildNotasSection(BuildContext context, AppProvider provider) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text('Notas del pedido', style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
|
|
const SizedBox(height: 8),
|
|
TextField(
|
|
controller: _notasCtrl,
|
|
onChanged: provider.setNotasPedido,
|
|
maxLines: 3,
|
|
decoration: const InputDecoration(
|
|
hintText: 'Instrucciones especiales, horario de entrega...',
|
|
alignLabelWithHint: true,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildBottomBar(BuildContext context, AppProvider provider) {
|
|
final canConfirm = provider.clienteSeleccionado != null && provider.carrito.isNotEmpty;
|
|
|
|
return Container(
|
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 24),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.08), blurRadius: 12, offset: const Offset(0, -4))],
|
|
),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
const Text('Total del pedido:', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500)),
|
|
Text(
|
|
_fmt.format(provider.totalCarrito),
|
|
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: AppTheme.primaryColor),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 52,
|
|
child: ElevatedButton.icon(
|
|
icon: const Icon(Icons.draw_outlined, size: 22),
|
|
label: const Text('Confirmar y Firmar', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
|
|
onPressed: canConfirm
|
|
? () => Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const FirmaScreen()),
|
|
)
|
|
: null,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _counterBtn(IconData icon, VoidCallback onTap) {
|
|
return InkWell(
|
|
onTap: onTap,
|
|
borderRadius: BorderRadius.circular(4),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(3),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(4),
|
|
),
|
|
child: Icon(icon, size: 16, color: AppTheme.primaryColor),
|
|
),
|
|
);
|
|
}
|
|
}
|