feat: suite de tiendas, sync reactivo, bitácora de errores y alertas whatsapp
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
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';
|
||||
|
||||
/// Botón e indicador visual de sincronización en tiempo real para AppBar.
|
||||
/// Permite al agente de ventas conocer en todo momento el estado de la conexión cloud:
|
||||
/// - Animación de carga cuando está sincronizando.
|
||||
/// - Badge naranja/ámbar si hay pedidos pendientes de subir.
|
||||
/// - Icono verde/limpio cuando toda la operación local ya está subida.
|
||||
/// - Modal interactivo al pulsar con detalles de sincronización y acción manual.
|
||||
class SyncStatusButton extends StatelessWidget {
|
||||
final Color? iconColor;
|
||||
|
||||
const SyncStatusButton({Key? key, this.iconColor}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final provider = context.watch<AppProvider>();
|
||||
final pendientes = provider.pedidosPendientesSync;
|
||||
final syncing = provider.syncing;
|
||||
final color = iconColor ?? Colors.white;
|
||||
|
||||
return IconButton(
|
||||
tooltip: syncing
|
||||
? 'Sincronizando con la nube...'
|
||||
: pendientes > 0
|
||||
? '$pendientes pedidos pendientes de sincronizar'
|
||||
: 'Nube al día',
|
||||
icon: syncing
|
||||
? SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2.2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(color),
|
||||
),
|
||||
)
|
||||
: Stack(
|
||||
clipBehavior: Clip.none,
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
pendientes > 0
|
||||
? Icons.cloud_upload_rounded
|
||||
: (provider.modoOnline ? Icons.cloud_done_rounded : Icons.cloud_queue_rounded),
|
||||
color: color,
|
||||
size: 24,
|
||||
),
|
||||
if (pendientes > 0)
|
||||
Positioned(
|
||||
top: -4,
|
||||
right: -6,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF59E0B), // Ámbar llamativo
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: Colors.white, width: 1.2),
|
||||
),
|
||||
constraints: const BoxConstraints(minWidth: 16, minHeight: 16),
|
||||
child: Text(
|
||||
'$pendientes',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
height: 1.1,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
onPressed: () => _mostrarDetallesSync(context, provider),
|
||||
);
|
||||
}
|
||||
|
||||
void _mostrarDetallesSync(BuildContext context, AppProvider provider) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
|
||||
),
|
||||
builder: (ctx) {
|
||||
final fmtTime = provider.ultimoSync != null
|
||||
? DateFormat('HH:mm:ss · d MMM', 'es').format(provider.ultimoSync!)
|
||||
: 'Pendiente de inicio';
|
||||
final pendientes = provider.pedidosPendientesSync;
|
||||
|
||||
return SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(20, 16, 20, 24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Center(
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 4,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.shade300,
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.primaryColor.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Icon(Icons.cloud_sync_rounded, color: AppTheme.primaryColor, size: 28),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'Sincronización Cloud',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
provider.modoOnline ? 'Conectado a API-SIGMA-CLOUD' : 'Modo fuera de línea (SQLite)',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: provider.modoOnline ? const Color(0xFF10B981) : Colors.grey.shade600,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
const Divider(),
|
||||
const SizedBox(height: 10),
|
||||
_buildInfoRow('Última sincronización:', fmtTime, Icons.access_time_rounded),
|
||||
const SizedBox(height: 10),
|
||||
_buildInfoRow(
|
||||
'Pedidos pendientes de subir:',
|
||||
'$pendientes pedido${pendientes == 1 ? '' : 's'}',
|
||||
Icons.receipt_long_rounded,
|
||||
destacado: pendientes > 0,
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
_buildInfoRow('Artículos en catálogo local:', '${provider.productos.length}', Icons.inventory_2_outlined),
|
||||
const SizedBox(height: 10),
|
||||
_buildInfoRow('Clientes en cartera:', '${provider.clientes.length}', Icons.people_outline_rounded),
|
||||
const SizedBox(height: 22),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 48,
|
||||
child: ElevatedButton.icon(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppTheme.primaryColor,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
),
|
||||
icon: provider.syncing
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.sync_rounded, color: Colors.white),
|
||||
label: Text(
|
||||
provider.syncing ? 'Sincronizando...' : 'Sincronizar ahora',
|
||||
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 15),
|
||||
),
|
||||
onPressed: provider.syncing
|
||||
? null
|
||||
: () async {
|
||||
Navigator.pop(ctx);
|
||||
final res = await provider.sincronizarManual();
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Row(
|
||||
children: [
|
||||
Icon(
|
||||
res.ok ? Icons.check_circle_rounded : Icons.info_outline,
|
||||
color: Colors.white,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: Text(res.mensaje)),
|
||||
],
|
||||
),
|
||||
backgroundColor: res.ok ? const Color(0xFF10B981) : const Color(0xFFF59E0B),
|
||||
duration: const Duration(seconds: 4),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoRow(String label, String value, IconData icon, {bool destacado = false}) {
|
||||
return Row(
|
||||
children: [
|
||||
Icon(icon, size: 18, color: destacado ? const Color(0xFFF59E0B) : Colors.grey.shade600),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 13, color: Color(0xFF475569)),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: destacado ? const Color(0xFFD97706) : const Color(0xFF1E293B),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user