Files

422 lines
14 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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';
class PedidoDetailScreen extends StatelessWidget {
final Pedido pedido;
const PedidoDetailScreen({Key? key, required this.pedido}) : super(key: key);
@override
Widget build(BuildContext context) {
final provider = context.read<AppProvider>();
final fmt = provider.currencyFormat;
final estadoColor = AppTheme.estadoColor(pedido.estado.label);
final dateStr = DateFormat('d \'de\' MMMM yyyy, HH:mm', 'es').format(pedido.fecha);
return Scaffold(
backgroundColor: AppTheme.bgColor,
appBar: AppBar(
title: Text(pedido.id, style: const TextStyle(fontWeight: FontWeight.bold)),
actions: [
PopupMenuButton<EstadoPedido>(
icon: const Icon(Icons.more_vert),
itemBuilder: (_) => EstadoPedido.values.map((e) {
return PopupMenuItem(
value: e,
child: Text(e.label),
);
}).toList(),
onSelected: (estado) {
provider.cambiarEstadoPedido(pedido.id, estado);
Navigator.pop(context);
},
),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header card
_buildHeaderCard(estadoColor, dateStr),
const SizedBox(height: 14),
// Tarjeta de estado de sincronización hacia la nube central
_buildSyncCard(context, provider),
const SizedBox(height: 14),
// Cliente
_buildClienteCard(),
const SizedBox(height: 14),
// Productos
_buildProductosCard(fmt),
const SizedBox(height: 14),
// Totals
_buildTotalesCard(fmt),
if (pedido.notas.isNotEmpty) ...[
const SizedBox(height: 14),
_buildNotasCard(),
],
if (pedido.firmaBase64 != null) ...[
const SizedBox(height: 14),
_buildFirmaCard(),
],
const SizedBox(height: 80),
],
),
),
bottomNavigationBar: _buildBottomBar(context, provider, estadoColor, fmt),
);
}
Widget _buildHeaderCard(Color estadoColor, String dateStr) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: estadoColor.withOpacity(0.08),
borderRadius: BorderRadius.circular(14),
border: Border.all(color: estadoColor.withOpacity(0.3)),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: estadoColor.withOpacity(0.15),
borderRadius: BorderRadius.circular(10),
),
child: Icon(Icons.receipt_long, color: estadoColor, size: 28),
),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(pedido.id,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: AppTheme.textPrimary)),
const SizedBox(height: 3),
Text(dateStr, style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary)),
],
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: estadoColor,
borderRadius: BorderRadius.circular(20),
),
child: Text(
pedido.estado.label,
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12),
),
),
],
),
);
}
/// Construye la tarjeta informativa de estado de transmisión con la nube SIGMA
Widget _buildSyncCard(BuildContext context, AppProvider provider) {
final syncEstado = pedido.syncEstado;
final isSincronizado = syncEstado == 'SINCRONIZADO';
final isError = syncEstado == 'ERROR';
final bg = isSincronizado
? const Color(0xFF10B981).withOpacity(0.08)
: isError
? const Color(0xFFEF4444).withOpacity(0.08)
: const Color(0xFFF59E0B).withOpacity(0.08);
final border = isSincronizado
? const Color(0xFF10B981).withOpacity(0.25)
: isError
? const Color(0xFFEF4444).withOpacity(0.25)
: const Color(0xFFF59E0B).withOpacity(0.25);
final iconColor = isSincronizado
? const Color(0xFF10B981)
: isError
? const Color(0xFFEF4444)
: const Color(0xFFD97706);
final icon = isSincronizado
? Icons.cloud_done_rounded
: isError
? Icons.cloud_off_rounded
: Icons.cloud_upload_outlined;
final title = isSincronizado
? 'Pedido sincronizado en la nube'
: isError
? 'Error al subir a la nube'
: 'Almacenado localmente (Offline)';
final desc = isSincronizado
? 'Transmitido correctamente a API-SIGMA-CLOUD y registrado en el ERP.'
: isError
? 'Falló la conexión o la API central rechazó la carga.'
: 'Se sincronizará automáticamente cuando el dispositivo tenga conexión.';
return Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
decoration: BoxDecoration(
color: bg,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: border),
),
child: Row(
children: [
Icon(icon, color: iconColor, size: 26),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(fontSize: 13, fontWeight: FontWeight.bold, color: iconColor),
),
const SizedBox(height: 2),
Text(
desc,
style: const TextStyle(fontSize: 11, color: Color(0xFF475569)),
),
],
),
),
if (!isSincronizado && provider.modoOnline) ...[
const SizedBox(width: 8),
TextButton(
style: TextButton.styleFrom(
foregroundColor: iconColor,
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: BorderSide(color: iconColor.withOpacity(0.4)),
),
),
onPressed: () async {
final ok = await provider.reintentarSubidaPedido(pedido.id);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
ok
? '¡Pedido ${pedido.id} subido con éxito a la nube!'
: 'No se pudo subir el pedido. Revisa tu conexión a internet.',
),
backgroundColor: ok ? const Color(0xFF10B981) : const Color(0xFFEF4444),
),
);
}
},
child: const Text('Subir', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 12)),
),
],
],
),
);
}
Widget _buildClienteCard() {
return _sectionCard(
title: 'Cliente',
icon: Icons.store_outlined,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(pedido.cliente.nombre,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 15, color: AppTheme.textPrimary)),
const SizedBox(height: 4),
Text(pedido.cliente.razonSocial,
style: const TextStyle(fontSize: 13, color: AppTheme.textSecondary)),
const SizedBox(height: 8),
_infoRow(Icons.location_on_outlined, pedido.cliente.direccion),
const SizedBox(height: 4),
_infoRow(Icons.phone_outlined, pedido.cliente.telefono),
const SizedBox(height: 4),
_infoRow(Icons.badge_outlined, 'NIT: ${pedido.cliente.nit}'),
],
),
);
}
Widget _buildProductosCard(NumberFormat fmt) {
return _sectionCard(
title: 'Productos (${pedido.items.length})',
icon: Icons.inventory_2_outlined,
child: Column(
children: pedido.items.map((item) => Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
children: [
Text(item.producto.emoji, style: const TextStyle(fontSize: 22)),
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)} × ${item.cantidad} ${item.producto.unidad}',
style: const TextStyle(fontSize: 11, color: AppTheme.textSecondary)),
],
),
),
Text(
fmt.format(item.subtotal),
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: AppTheme.textPrimary),
),
],
),
)).toList(),
),
);
}
Widget _buildTotalesCard(NumberFormat fmt) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: AppTheme.primaryColor,
borderRadius: BorderRadius.circular(14),
),
child: Column(
children: [
_totalRow('Subtotal', fmt.format(pedido.subtotal), Colors.white70, Colors.white),
const SizedBox(height: 8),
_totalRow('Descuento', '0%', Colors.white70, Colors.white),
const Divider(color: Colors.white24, height: 20),
_totalRow('TOTAL', fmt.format(pedido.total), Colors.white, Colors.white,
isTotal: true),
],
),
);
}
Widget _totalRow(String label, String valor, Color labelColor, Color valueColor, {bool isTotal = false}) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(label, style: TextStyle(color: labelColor, fontSize: isTotal ? 16 : 13, fontWeight: isTotal ? FontWeight.bold : FontWeight.normal)),
Text(valor, style: TextStyle(color: valueColor, fontSize: isTotal ? 20 : 13, fontWeight: FontWeight.bold)),
],
);
}
Widget _buildNotasCard() {
return _sectionCard(
title: 'Notas',
icon: Icons.notes_outlined,
child: Text(pedido.notas, style: const TextStyle(fontSize: 13, color: AppTheme.textPrimary)),
);
}
Widget _buildFirmaCard() {
return _sectionCard(
title: 'Firma del cliente',
icon: Icons.draw_outlined,
child: Container(
height: 100,
width: double.infinity,
decoration: BoxDecoration(
color: AppTheme.bgColor,
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(Icons.check_circle, color: AppTheme.successColor, size: 20),
SizedBox(width: 8),
Text('Pedido firmado', style: TextStyle(color: AppTheme.successColor, fontWeight: FontWeight.w600)),
],
),
),
),
);
}
Widget _buildBottomBar(BuildContext context, AppProvider provider, Color estadoColor, NumberFormat fmt) {
return Container(
padding: const EdgeInsets.fromLTRB(16, 12, 16, 24),
decoration: const BoxDecoration(
color: Colors.white,
boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 8, offset: Offset(0, -2))],
),
child: Row(
children: [
Expanded(
child: OutlinedButton.icon(
icon: const Icon(Icons.print_outlined),
label: const Text('Imprimir'),
onPressed: () {},
),
),
const SizedBox(width: 12),
Expanded(
child: OutlinedButton.icon(
icon: const Icon(Icons.share_outlined),
label: const Text('Compartir'),
onPressed: () {},
),
),
if (pedido.estado == EstadoPedido.confirmado) ...[
const SizedBox(width: 12),
Expanded(
child: ElevatedButton.icon(
icon: const Icon(Icons.local_shipping_outlined, size: 18),
label: const Text('Marcar entregado', style: TextStyle(fontSize: 12)),
onPressed: () {
provider.cambiarEstadoPedido(pedido.id, EstadoPedido.entregado);
Navigator.pop(context);
},
),
),
],
],
),
);
}
Widget _sectionCard({required String title, required IconData icon, required Widget child}) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(14),
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.04), blurRadius: 6)],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(icon, size: 17, color: AppTheme.primaryColor),
const SizedBox(width: 6),
Text(title, style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
],
),
const Divider(height: 16),
child,
],
),
);
}
Widget _infoRow(IconData icon, String text) {
return Row(
children: [
Icon(icon, size: 14, color: AppTheme.textSecondary),
const SizedBox(width: 6),
Expanded(
child: Text(text, style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary)),
),
],
);
}
}