146 lines
5.4 KiB
Dart
146 lines
5.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../theme.dart';
|
|
import '../db/dao/cache_clientes_dao.dart';
|
|
|
|
// Comentario explicativo: Pantalla de Clientes conectada a CacheClientesDao.
|
|
// Muestra el semáforo de estado (normal, alerta, crítica) y métricas de alarmas por cliente,
|
|
// garantizando que el revisor de la tienda visualice datos completos offline.
|
|
class ClientesPage extends StatefulWidget {
|
|
const ClientesPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<ClientesPage> createState() => _ClientesPageState();
|
|
}
|
|
|
|
class _ClientesPageState extends State<ClientesPage> {
|
|
final _clientesDao = CacheClientesDao();
|
|
List<Map<String, dynamic>> _clientes = [];
|
|
bool _cargando = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_cargarClientes();
|
|
}
|
|
|
|
Future<void> _cargarClientes() async {
|
|
final lista = await _clientesDao.getTodos();
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_clientes = lista;
|
|
_cargando = false;
|
|
});
|
|
}
|
|
|
|
Color _colorEstado(String estado) {
|
|
switch (estado.toLowerCase()) {
|
|
case 'critica':
|
|
return AppColors.alarmaActiva;
|
|
case 'alerta':
|
|
return AppColors.clienteAlerta;
|
|
default:
|
|
return AppColors.clienteOk;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SingleChildScrollView(
|
|
primary: false,
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
Text('Clientes Registrados',
|
|
style: appHeadline(size: 22, weight: FontWeight.w800, color: AppColors.onSurface)),
|
|
const SizedBox(height: 4),
|
|
Text('Monitoreo del estado perimetral y central de cada sede',
|
|
style: appLabel(size: 13, color: AppColors.outline)),
|
|
]),
|
|
IconButton(
|
|
icon: const Icon(Icons.refresh, color: AppColors.primary),
|
|
onPressed: _cargarClientes,
|
|
tooltip: 'Refrescar clientes',
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
if (_cargando)
|
|
const Center(child: CircularProgressIndicator())
|
|
else if (_clientes.isEmpty)
|
|
Container(
|
|
padding: const EdgeInsets.all(32),
|
|
alignment: Alignment.center,
|
|
child: Text('No hay clientes en caché local.', style: appLabel(color: AppColors.outline)),
|
|
)
|
|
else
|
|
ListView.separated(
|
|
shrinkWrap: true,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
itemCount: _clientes.length,
|
|
separatorBuilder: (_, __) => const SizedBox(height: 12),
|
|
itemBuilder: (_, i) {
|
|
final c = _clientes[i];
|
|
final estado = (c['estado'] as String?) ?? 'normal';
|
|
final color = _colorEstado(estado);
|
|
final activas = (c['alarmas_activas'] as int?) ?? 0;
|
|
final criticas = (c['alarmas_criticas'] as int?) ?? 0;
|
|
|
|
return Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceLow,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: color.withOpacity(0.4), width: 1.2),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 12,
|
|
height: 12,
|
|
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
|
|
),
|
|
const SizedBox(width: 14),
|
|
Expanded(
|
|
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
Text(
|
|
c['nombre'] as String? ?? 'Cliente',
|
|
style: appHeadline(size: 14, weight: FontWeight.w700, color: AppColors.onSurface),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'ID: #${c['id']} • Estado: ${estado.toUpperCase()}',
|
|
style: appLabel(size: 12, color: AppColors.outline),
|
|
),
|
|
]),
|
|
),
|
|
Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: activas > 0 ? AppColors.surfaceHighest : AppColors.surfaceLowest,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
'$activas activas ${criticas > 0 ? "($criticas críticas)" : ""}',
|
|
style: appLabel(
|
|
size: 11,
|
|
weight: FontWeight.bold,
|
|
color: criticas > 0 ? AppColors.alarmaActiva : AppColors.primary,
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
]),
|
|
);
|
|
}
|
|
}
|