556 lines
20 KiB
Dart
556 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 '../../services/notification_service.dart';
|
|
import '../../theme/app_theme.dart';
|
|
import '../../widgets/stat_card.dart';
|
|
import '../../models/pedido.dart';
|
|
import '../notificaciones/notificaciones_screen.dart';
|
|
// Botón reactivo de sincronización y estado cloud en AppBar
|
|
import '../../widgets/sync_status_button.dart';
|
|
|
|
class HomeScreen extends StatelessWidget {
|
|
const HomeScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<AppProvider>();
|
|
final notifService = context.watch<NotificationService>();
|
|
final fmt = provider.currencyFormat;
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppTheme.bgColor,
|
|
body: RefreshIndicator(
|
|
// Sincronización manual reactiva al deslizar hacia abajo
|
|
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: CustomScrollView(
|
|
slivers: [
|
|
_buildSliverAppBar(context, provider, notifService),
|
|
SliverPadding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
sliver: SliverList(
|
|
delegate: SliverChildListDelegate([
|
|
const SizedBox(height: 16),
|
|
_buildStatsGrid(context, provider, fmt),
|
|
const SizedBox(height: 20),
|
|
_buildRutaCard(context, provider),
|
|
const SizedBox(height: 20),
|
|
_buildRecentOrders(context, provider, fmt),
|
|
const SizedBox(height: 20),
|
|
_buildQuickActions(context, notifService),
|
|
const SizedBox(height: 80),
|
|
]),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildSliverAppBar(
|
|
BuildContext context,
|
|
AppProvider provider,
|
|
NotificationService notifService,
|
|
) {
|
|
final hour = DateTime.now().hour;
|
|
final greeting = hour < 12 ? 'Buenos días' : hour < 18 ? 'Buenas tardes' : 'Buenas noches';
|
|
final dateStr = DateFormat('EEEE, d \'de\' MMMM', 'es').format(DateTime.now());
|
|
final unread = notifService.unreadCount;
|
|
|
|
return SliverAppBar(
|
|
expandedHeight: 160,
|
|
floating: false,
|
|
pinned: true,
|
|
backgroundColor: AppTheme.primaryColor,
|
|
actions: [
|
|
// Indicador y botón interactivo de sincronización con la nube
|
|
const SyncStatusButton(),
|
|
Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.notifications_outlined, color: Colors.white, size: 26),
|
|
onPressed: () => Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const NotificacionesScreen()),
|
|
),
|
|
),
|
|
if (unread > 0)
|
|
Positioned(
|
|
top: 10,
|
|
right: 10,
|
|
child: Container(
|
|
width: 17,
|
|
height: 17,
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFFEF4444),
|
|
shape: BoxShape.circle,
|
|
border: Border.all(color: AppTheme.primaryColor, width: 1.5),
|
|
),
|
|
child: Center(
|
|
child: Text(
|
|
unread > 9 ? '9+' : '$unread',
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 9,
|
|
fontWeight: FontWeight.bold,
|
|
height: 1,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
flexibleSpace: FlexibleSpaceBar(
|
|
background: Container(
|
|
decoration: const BoxDecoration(
|
|
gradient: LinearGradient(
|
|
colors: [Color(0xFF1E3A8A), Color(0xFF2563EB)],
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 16, 60, 0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
CircleAvatar(
|
|
radius: 22,
|
|
backgroundColor: Colors.white.withOpacity(0.2),
|
|
child: Text(
|
|
provider.vendedor.avatarInitials,
|
|
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 16),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'$greeting,',
|
|
style: const TextStyle(color: Colors.white70, fontSize: 13),
|
|
),
|
|
Text(
|
|
provider.vendedor.nombre,
|
|
style: const TextStyle(color: Colors.white, fontSize: 17, fontWeight: FontWeight.bold),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 10),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.calendar_today, color: Colors.white60, size: 14),
|
|
const SizedBox(width: 6),
|
|
Text(
|
|
dateStr,
|
|
style: const TextStyle(color: Colors.white70, fontSize: 13),
|
|
),
|
|
const Spacer(),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white.withOpacity(0.2),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
const Icon(Icons.location_on, color: Colors.white, size: 14),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
provider.vendedor.zona,
|
|
style: const TextStyle(color: Colors.white, fontSize: 12, fontWeight: FontWeight.w500),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
title: Text(
|
|
greeting,
|
|
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
),
|
|
titlePadding: const EdgeInsets.only(left: 16, bottom: 12),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildStatsGrid(BuildContext context, AppProvider provider, NumberFormat fmt) {
|
|
final ruta = provider.ruta;
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text('Resumen del día',
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
|
|
const SizedBox(height: 12),
|
|
GridView.count(
|
|
crossAxisCount: 2,
|
|
shrinkWrap: true,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
crossAxisSpacing: 12,
|
|
mainAxisSpacing: 12,
|
|
childAspectRatio: 1.5,
|
|
children: [
|
|
StatCard(
|
|
titulo: 'Pedidos hoy',
|
|
valor: '${provider.pedidosHoy}',
|
|
subtitulo: 'confirmados',
|
|
icono: Icons.receipt_long,
|
|
color: AppTheme.primaryColor,
|
|
),
|
|
StatCard(
|
|
titulo: 'Ventas del día',
|
|
valor: fmt.format(provider.ventasTotalesHoy),
|
|
subtitulo: 'total acumulado',
|
|
icono: Icons.attach_money,
|
|
color: AppTheme.successColor,
|
|
),
|
|
StatCard(
|
|
titulo: 'Clientes visitados',
|
|
valor: '${ruta.visitadas}/${ruta.paradas.length}',
|
|
subtitulo: 'en la ruta',
|
|
icono: Icons.store,
|
|
color: AppTheme.accentColor,
|
|
),
|
|
StatCard(
|
|
titulo: 'Paradas restantes',
|
|
valor: '${ruta.pendientes}',
|
|
subtitulo: 'por visitar',
|
|
icono: Icons.flag_outlined,
|
|
color: AppTheme.warningColor,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildRutaCard(BuildContext context, AppProvider provider) {
|
|
final ruta = provider.ruta;
|
|
final proxima = ruta.paradas.where((p) => p.estado.name == 'pendiente').isNotEmpty
|
|
? ruta.paradas.firstWhere((p) => p.estado.name == 'pendiente')
|
|
: null;
|
|
|
|
return Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
gradient: const LinearGradient(
|
|
colors: [Color(0xFF0F172A), Color(0xFF1E3A8A)],
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
),
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.route, color: Colors.white, size: 20),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(
|
|
ruta.nombre,
|
|
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 15),
|
|
),
|
|
),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white.withOpacity(0.15),
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: Text(
|
|
'${ruta.visitadas}/${ruta.paradas.length} paradas',
|
|
style: const TextStyle(color: Colors.white, fontSize: 12),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(4),
|
|
child: LinearProgressIndicator(
|
|
value: ruta.paradas.isEmpty ? 0 : ruta.visitadas / ruta.paradas.length,
|
|
backgroundColor: Colors.white.withOpacity(0.2),
|
|
valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF34D399)),
|
|
minHeight: 6,
|
|
),
|
|
),
|
|
if (proxima != null) ...[
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.navigation, color: Color(0xFF34D399), size: 16),
|
|
const SizedBox(width: 6),
|
|
const Text('Próxima parada:', style: TextStyle(color: Colors.white60, fontSize: 12)),
|
|
const SizedBox(width: 6),
|
|
Expanded(
|
|
child: Text(
|
|
proxima.cliente.nombre,
|
|
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.w600, fontSize: 13),
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
Text(
|
|
proxima.horaEstimada,
|
|
style: const TextStyle(color: Color(0xFFFBBF24), fontSize: 13, fontWeight: FontWeight.bold),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildRecentOrders(BuildContext context, AppProvider provider, NumberFormat fmt) {
|
|
final recientes = provider.pedidos.take(3).toList();
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
const Text('Pedidos recientes',
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
|
|
TextButton(
|
|
onPressed: () {},
|
|
child: const Text('Ver todos'),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
...recientes.map((pedido) => _buildOrderTile(context, pedido, fmt)).toList(),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildOrderTile(BuildContext context, Pedido pedido, NumberFormat fmt) {
|
|
final estadoColor = AppTheme.estadoColor(pedido.estado.label);
|
|
return Container(
|
|
margin: const EdgeInsets.only(bottom: 8),
|
|
padding: const EdgeInsets.all(14),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
boxShadow: [
|
|
BoxShadow(color: Colors.black.withOpacity(0.04), blurRadius: 6, offset: const Offset(0, 2)),
|
|
],
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 40,
|
|
height: 40,
|
|
decoration: BoxDecoration(
|
|
color: estadoColor.withOpacity(0.12),
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: Icon(Icons.receipt_outlined, color: estadoColor, size: 20),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(pedido.id,
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: AppTheme.textPrimary)),
|
|
Text(pedido.cliente.nombre,
|
|
style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary)),
|
|
],
|
|
),
|
|
),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Text(fmt.format(pedido.total),
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: AppTheme.textPrimary)),
|
|
const SizedBox(height: 4),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: estadoColor.withOpacity(0.12),
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Text(
|
|
pedido.estado.label,
|
|
style: TextStyle(fontSize: 10, color: estadoColor, fontWeight: FontWeight.w600),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildQuickActions(BuildContext context, NotificationService notifService) {
|
|
final staticActions = [
|
|
{'icon': Icons.add_shopping_cart, 'label': 'Nuevo\nPedido', 'color': AppTheme.primaryColor},
|
|
{'icon': Icons.map_outlined, 'label': 'Ver\nRuta', 'color': AppTheme.successColor},
|
|
{'icon': Icons.inventory_2_outlined, 'label': 'Catálogo', 'color': AppTheme.accentColor},
|
|
];
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text('Acciones rápidas',
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
children: [
|
|
...staticActions.map((a) {
|
|
final color = a['color'] as Color;
|
|
return Expanded(
|
|
child: GestureDetector(
|
|
onTap: () {},
|
|
child: Container(
|
|
margin: const EdgeInsets.symmetric(horizontal: 4),
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
boxShadow: [
|
|
BoxShadow(color: color.withOpacity(0.1), blurRadius: 8, offset: const Offset(0, 3)),
|
|
],
|
|
),
|
|
child: Column(
|
|
children: [
|
|
Icon(a['icon'] as IconData, color: color, size: 26),
|
|
const SizedBox(height: 6),
|
|
Text(
|
|
a['label'] as String,
|
|
style: TextStyle(fontSize: 11, color: color, fontWeight: FontWeight.w600),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}).toList(),
|
|
Expanded(
|
|
child: GestureDetector(
|
|
onTap: () => notifService.dispararDemo(),
|
|
child: Container(
|
|
margin: const EdgeInsets.symmetric(horizontal: 4),
|
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: const Color(0xFF7C3AED).withOpacity(0.15),
|
|
blurRadius: 8,
|
|
offset: const Offset(0, 3),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
children: const [
|
|
Icon(Icons.notifications_active, color: Color(0xFF7C3AED), size: 26),
|
|
SizedBox(height: 6),
|
|
Text(
|
|
'Demo\nNotif.',
|
|
style: TextStyle(fontSize: 11, color: Color(0xFF7C3AED), fontWeight: FontWeight.w600),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
_buildNotifDemoCard(context, notifService),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildNotifDemoCard(BuildContext context, NotificationService notifService) {
|
|
return Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
gradient: const LinearGradient(
|
|
colors: [Color(0xFF4C1D95), Color(0xFF7C3AED)],
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
),
|
|
borderRadius: BorderRadius.circular(14),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 44,
|
|
height: 44,
|
|
decoration: BoxDecoration(
|
|
color: Colors.white.withOpacity(0.15),
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: const Icon(Icons.notifications_active, color: Colors.white, size: 22),
|
|
),
|
|
const SizedBox(width: 14),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: const [
|
|
Text(
|
|
'Demo de notificaciones',
|
|
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 14),
|
|
),
|
|
SizedBox(height: 2),
|
|
Text(
|
|
'Pulsa para disparar 5 notificaciones de muestra con banner animado',
|
|
style: TextStyle(color: Colors.white70, fontSize: 11),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
ElevatedButton(
|
|
onPressed: () => notifService.dispararDemo(),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.white,
|
|
foregroundColor: const Color(0xFF7C3AED),
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
|
elevation: 0,
|
|
),
|
|
child: const Text('Probar', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|