382 lines
14 KiB
Dart
382 lines
14 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/pedido.dart';
|
|
import 'pedido_form_screen.dart';
|
|
import 'pedido_detail_screen.dart';
|
|
// Botón interactivo de sincronización para AppBar
|
|
import '../../widgets/sync_status_button.dart';
|
|
|
|
class PedidosScreen extends StatefulWidget {
|
|
const PedidosScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<PedidosScreen> createState() => _PedidosScreenState();
|
|
}
|
|
|
|
class _PedidosScreenState extends State<PedidosScreen> with SingleTickerProviderStateMixin {
|
|
late TabController _tab;
|
|
NumberFormat get _fmt => context.watch<AppProvider>().currencyFormat;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_tab = TabController(length: 4, vsync: this);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_tab.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<AppProvider>();
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppTheme.bgColor,
|
|
appBar: AppBar(
|
|
title: const Text('Pedidos', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
actions: [
|
|
// Botón reactivo de sincronización para comprobar y forzar subidas
|
|
const SyncStatusButton(),
|
|
IconButton(icon: const Icon(Icons.search), onPressed: () {}),
|
|
IconButton(icon: const Icon(Icons.filter_list), onPressed: () {}),
|
|
],
|
|
bottom: TabBar(
|
|
controller: _tab,
|
|
indicatorColor: Colors.white,
|
|
indicatorWeight: 3,
|
|
isScrollable: true,
|
|
tabs: _buildTabs(provider),
|
|
),
|
|
),
|
|
body: TabBarView(
|
|
controller: _tab,
|
|
children: [
|
|
_buildList(context, provider.pedidos, provider),
|
|
_buildList(
|
|
context,
|
|
provider.pedidos.where((p) => p.estado == EstadoPedido.pendiente).toList(),
|
|
provider),
|
|
_buildList(
|
|
context,
|
|
provider.pedidos.where((p) => p.estado == EstadoPedido.confirmado).toList(),
|
|
provider),
|
|
_buildList(
|
|
context,
|
|
provider.pedidos.where((p) => p.estado == EstadoPedido.entregado).toList(),
|
|
provider),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const PedidoFormScreen()),
|
|
);
|
|
},
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('Nuevo pedido'),
|
|
backgroundColor: AppTheme.primaryColor,
|
|
),
|
|
);
|
|
}
|
|
|
|
List<Widget> _buildTabs(AppProvider provider) {
|
|
final todos = provider.pedidos.length;
|
|
final pend = provider.pedidos.where((p) => p.estado == EstadoPedido.pendiente).length;
|
|
final conf = provider.pedidos.where((p) => p.estado == EstadoPedido.confirmado).length;
|
|
final entr = provider.pedidos.where((p) => p.estado == EstadoPedido.entregado).length;
|
|
|
|
return [
|
|
Tab(text: 'Todos ($todos)'),
|
|
Tab(text: 'Pendiente ($pend)'),
|
|
Tab(text: 'Confirmado ($conf)'),
|
|
Tab(text: 'Entregado ($entr)'),
|
|
];
|
|
}
|
|
|
|
Widget _buildList(BuildContext context, List<Pedido> pedidos, AppProvider provider) {
|
|
// Soportar pull-to-refresh en lista de pedidos para sincronización en tiempo real
|
|
return RefreshIndicator(
|
|
onRefresh: () async {
|
|
final res = await provider.sincronizarManual();
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(res.mensaje),
|
|
duration: const Duration(seconds: 3),
|
|
backgroundColor: res.ok ? const Color(0xFF10B981) : const Color(0xFFF59E0B),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
child: pedidos.isEmpty
|
|
? ListView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
children: [
|
|
SizedBox(height: MediaQuery.of(context).size.height * 0.25),
|
|
Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.receipt_long_outlined, size: 64, color: Colors.grey.shade300),
|
|
const SizedBox(height: 12),
|
|
const Text('No hay pedidos', style: TextStyle(color: AppTheme.textSecondary, fontSize: 15)),
|
|
const SizedBox(height: 6),
|
|
Text(
|
|
'Desliza hacia abajo para actualizar',
|
|
style: TextStyle(color: Colors.grey.shade400, fontSize: 12),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
)
|
|
: ListView.builder(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 80),
|
|
itemCount: pedidos.length,
|
|
itemBuilder: (context, i) => _buildPedidoCard(context, pedidos[i], provider),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildPedidoCard(BuildContext context, Pedido pedido, AppProvider provider) {
|
|
final estadoColor = AppTheme.estadoColor(pedido.estado.label);
|
|
final timeStr = DateFormat('d MMM, HH:mm', 'es').format(pedido.fecha);
|
|
|
|
return Dismissible(
|
|
key: Key(pedido.id),
|
|
direction: DismissDirection.endToStart,
|
|
background: Container(
|
|
alignment: Alignment.centerRight,
|
|
padding: const EdgeInsets.only(right: 20),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.dangerColor,
|
|
borderRadius: BorderRadius.circular(14),
|
|
),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: const [
|
|
Icon(Icons.delete_outline, color: Colors.white, size: 28),
|
|
SizedBox(height: 4),
|
|
Text('Eliminar', style: TextStyle(color: Colors.white, fontSize: 11)),
|
|
],
|
|
),
|
|
),
|
|
confirmDismiss: (dir) async {
|
|
return await showDialog<bool>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('Eliminar pedido'),
|
|
content: Text('¿Eliminar el pedido ${pedido.id}?'),
|
|
actions: [
|
|
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancelar')),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx, true),
|
|
child: const Text('Eliminar', style: TextStyle(color: AppTheme.dangerColor)),
|
|
),
|
|
],
|
|
),
|
|
) ?? false;
|
|
},
|
|
onDismissed: (_) => provider.eliminarPedido(pedido.id),
|
|
child: GestureDetector(
|
|
onTap: () => Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => PedidoDetailScreen(pedido: pedido)),
|
|
),
|
|
child: Container(
|
|
margin: const EdgeInsets.only(bottom: 10),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(14),
|
|
border: Border.all(color: estadoColor.withOpacity(0.2)),
|
|
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.04), blurRadius: 6, offset: const Offset(0, 2))],
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(14),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
color: estadoColor.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Icon(Icons.receipt_outlined, color: estadoColor, size: 20),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
pedido.id,
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14, color: AppTheme.textPrimary),
|
|
),
|
|
Text(timeStr, style: const TextStyle(fontSize: 11, color: AppTheme.textSecondary)),
|
|
],
|
|
),
|
|
),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: estadoColor.withOpacity(0.12),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
pedido.estado.label,
|
|
style: TextStyle(fontSize: 11, color: estadoColor, fontWeight: FontWeight.w700),
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
// Badge visual del estado de sincronización hacia API-SIGMA-CLOUD
|
|
_buildSyncBadge(pedido.syncEstado),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
const Divider(height: 16),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.store_outlined, size: 14, color: AppTheme.textSecondary),
|
|
const SizedBox(width: 4),
|
|
Expanded(
|
|
child: Text(
|
|
pedido.cliente.nombre,
|
|
style: const TextStyle(fontSize: 13, color: AppTheme.textPrimary, fontWeight: FontWeight.w500),
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 6),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.inventory_2_outlined, size: 14, color: AppTheme.textSecondary),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
'${pedido.totalItems} productos · ${pedido.items.length} líneas',
|
|
style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary),
|
|
),
|
|
const Spacer(),
|
|
Text(
|
|
_fmt.format(pedido.total),
|
|
style: const TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppTheme.primaryColor,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
if (pedido.notas.isNotEmpty) ...[
|
|
const SizedBox(height: 8),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.bgColor,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
const Icon(Icons.notes, size: 13, color: AppTheme.textSecondary),
|
|
const SizedBox(width: 6),
|
|
Expanded(
|
|
child: Text(
|
|
pedido.notas,
|
|
style: const TextStyle(fontSize: 11, color: AppTheme.textSecondary),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Construye el indicador visual de sincronización para cada tarjeta de pedido
|
|
Widget _buildSyncBadge(String syncEstado) {
|
|
if (syncEstado == 'SINCRONIZADO') {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFF10B981).withOpacity(0.12),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: const [
|
|
Icon(Icons.cloud_done_rounded, size: 12, color: Color(0xFF10B981)),
|
|
SizedBox(width: 3),
|
|
Text(
|
|
'Nube',
|
|
style: TextStyle(fontSize: 10, color: Color(0xFF10B981), fontWeight: FontWeight.w600),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
if (syncEstado == 'ERROR') {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFFEF4444).withOpacity(0.12),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: const [
|
|
Icon(Icons.error_outline_rounded, size: 12, color: Color(0xFFEF4444)),
|
|
SizedBox(width: 3),
|
|
Text(
|
|
'Error sync',
|
|
style: TextStyle(fontSize: 10, color: Color(0xFFEF4444), fontWeight: FontWeight.w600),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// PENDIENTE
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFFF59E0B).withOpacity(0.12),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: const [
|
|
Icon(Icons.cloud_upload_outlined, size: 12, color: Color(0xFFD97706)),
|
|
SizedBox(width: 3),
|
|
Text(
|
|
'Pendiente',
|
|
style: TextStyle(fontSize: 10, color: Color(0xFFD97706), fontWeight: FontWeight.w600),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|