436 lines
16 KiB
Dart
436 lines
16 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'theme.dart';
|
|
import 'services/api_auth_service.dart';
|
|
import 'db/dao/sesiones_dao.dart';
|
|
import 'pages/dashboard_page.dart';
|
|
import 'pages/monitoreo_page.dart';
|
|
import 'pages/clientes_page.dart';
|
|
import 'pages/operadores_page.dart';
|
|
import 'pages/incidentes_page.dart';
|
|
import 'pages/dispositivos_page.dart';
|
|
import 'pages/reportes_page.dart';
|
|
import 'pages/configuracion_page.dart';
|
|
import 'services/sincronizador.dart';
|
|
|
|
class ShellPage extends StatefulWidget {
|
|
const ShellPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<ShellPage> createState() => _ShellPageState();
|
|
}
|
|
|
|
class _ShellPageState extends State<ShellPage> {
|
|
int _indice = 0;
|
|
String _operadorNombre = 'Operador';
|
|
bool _apiConectada = false;
|
|
|
|
final _sesionesDao = SesionesDao();
|
|
|
|
static const _paginas = [
|
|
DashboardPage(),
|
|
MonitoreoPage(),
|
|
ClientesPage(),
|
|
OperadoresPage(),
|
|
IncidentesPage(),
|
|
DispositivosPage(),
|
|
ReportesPage(),
|
|
ConfiguracionPage(),
|
|
];
|
|
|
|
static const _navItems = [
|
|
_NavItem(Icons.dashboard_outlined, Icons.dashboard, 'Dashboard'),
|
|
_NavItem(Icons.track_changes_outlined, Icons.track_changes, 'Monitoreo'),
|
|
_NavItem(Icons.business_outlined, Icons.business, 'Clientes'),
|
|
_NavItem(Icons.group_outlined, Icons.group, 'Operadores'),
|
|
_NavItem(Icons.assignment_outlined, Icons.assignment, 'Incidentes'),
|
|
_NavItem(Icons.devices_outlined, Icons.devices, 'Dispositivos'),
|
|
_NavItem(Icons.bar_chart_outlined, Icons.bar_chart, 'Reportes'),
|
|
_NavItem(Icons.settings_outlined, Icons.settings, 'Configuración'),
|
|
];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_cargarOperador();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// Comentario explicativo: Detiene el ciclo periódico del sincronizador al destruir ShellPage
|
|
Sincronizador.instance.detener();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _cargarOperador() async {
|
|
final nombre = await ApiAuthService.instance.getNombre();
|
|
final usuario = await ApiAuthService.instance.getUsuario() ?? '';
|
|
if (!mounted) return;
|
|
setState(() => _operadorNombre = nombre ?? usuario);
|
|
await _sesionesDao.iniciarSesion(_operadorNombre, usuario);
|
|
_verificarApi();
|
|
|
|
// Comentario explicativo: Inicia el ciclo periódico de sincronización automática en segundo plano
|
|
Sincronizador.instance.iniciar(intervaloSegundos: 30);
|
|
}
|
|
|
|
Future<void> _verificarApi() async {
|
|
final ok = await ApiAuthService.instance.isAuthenticated();
|
|
if (!mounted) return;
|
|
setState(() => _apiConectada = ok);
|
|
}
|
|
|
|
Future<void> _cerrarSesion() async {
|
|
final ok = await showDialog<bool>(
|
|
context: context,
|
|
builder: (_) => AlertDialog(
|
|
backgroundColor: AppColors.surfaceHigh,
|
|
title: Text('Cerrar sesión',
|
|
style: appHeadline(size: 16, weight: FontWeight.w700)),
|
|
content: Text('¿Finalizar el turno y cerrar sesión?',
|
|
style: appLabel(size: 13, color: AppColors.onSurface)),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: Text('Cancelar', style: appLabel(color: AppColors.onSurfaceVariant)),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: Text('CERRAR', style: appLabel(weight: FontWeight.bold, color: AppColors.error)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (ok != true) return;
|
|
Sincronizador.instance.detener();
|
|
await _sesionesDao.cerrarSesion();
|
|
await ApiAuthService.instance.logout();
|
|
if (!mounted) return;
|
|
Navigator.of(context).pushReplacementNamed('/login');
|
|
}
|
|
|
|
// Comentario explicativo: Soporte de layout adaptativo responsivo.
|
|
// En escritorio/pantallas grandes (ancho >= 720px) mantiene el Sidebar fijo de 220px original.
|
|
// En pantallas móviles (< 720px), colapsa el sidebar a un Drawer y BottomNavigationBar
|
|
// para permitir una experiencia óptima y capturas de pantalla móviles limpias sin desbordamiento.
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final ancho = MediaQuery.of(context).size.width;
|
|
final esMovil = ancho < 720;
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppColors.surfaceLowest,
|
|
drawer: esMovil
|
|
? Drawer(
|
|
backgroundColor: AppColors.surfaceLow,
|
|
child: SafeArea(
|
|
child: Column(children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(16),
|
|
alignment: Alignment.centerLeft,
|
|
child: Row(children: [
|
|
Container(
|
|
width: 32,
|
|
height: 32,
|
|
decoration: const BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: AppColors.primaryContainer,
|
|
),
|
|
child: const Icon(Icons.shield, color: AppColors.primary, size: 18),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Text('CENTRO DE MONITOREO',
|
|
style: appHeadline(
|
|
size: 13,
|
|
weight: FontWeight.w800,
|
|
color: AppColors.primary,
|
|
spacing: 1.2)),
|
|
]),
|
|
),
|
|
const Divider(color: AppColors.outlineVariant, height: 1),
|
|
Expanded(
|
|
child: ListView.builder(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
|
itemCount: _navItems.length,
|
|
itemBuilder: (_, i) => _navTile(i, cerrarDrawerAlTocar: true),
|
|
),
|
|
),
|
|
]),
|
|
),
|
|
)
|
|
: null,
|
|
body: SafeArea(
|
|
top: esMovil,
|
|
bottom: false,
|
|
child: Column(children: [
|
|
_header(esMovil: esMovil),
|
|
Expanded(
|
|
child: esMovil
|
|
? IndexedStack(
|
|
index: _indice,
|
|
children: _paginas,
|
|
)
|
|
: Row(children: [
|
|
_sidebar(),
|
|
const VerticalDivider(width: 1, color: AppColors.outlineVariant),
|
|
Expanded(
|
|
child: IndexedStack(
|
|
index: _indice,
|
|
children: _paginas,
|
|
),
|
|
),
|
|
]),
|
|
),
|
|
]),
|
|
),
|
|
bottomNavigationBar: esMovil
|
|
? BottomNavigationBar(
|
|
// Comentario explicativo: Mapeo de índices de navegación móvil:
|
|
// 0: Dashboard, 1: Monitoreo, 2: Clientes, 4: Incidentes
|
|
currentIndex: _indice == 4 ? 3 : (_indice < 3 ? _indice : 0),
|
|
backgroundColor: AppColors.surfaceLow,
|
|
selectedItemColor: AppColors.primary,
|
|
unselectedItemColor: AppColors.onSurfaceVariant,
|
|
type: BottomNavigationBarType.fixed,
|
|
selectedFontSize: 11,
|
|
unselectedFontSize: 10,
|
|
onTap: (i) {
|
|
const indicesMovil = [0, 1, 2, 4];
|
|
setState(() => _indice = indicesMovil[i]);
|
|
},
|
|
items: const [
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.dashboard_outlined),
|
|
activeIcon: Icon(Icons.dashboard),
|
|
label: 'Dashboard',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.track_changes_outlined),
|
|
activeIcon: Icon(Icons.track_changes),
|
|
label: 'Monitoreo',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.business_outlined),
|
|
activeIcon: Icon(Icons.business),
|
|
label: 'Clientes',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.assignment_outlined),
|
|
activeIcon: Icon(Icons.assignment),
|
|
label: 'Incidentes',
|
|
),
|
|
],
|
|
)
|
|
: null,
|
|
);
|
|
}
|
|
|
|
// ── Header ──────────────────────────────────────────────────────────────────
|
|
Widget _header({bool esMovil = false}) => Container(
|
|
height: 56,
|
|
color: AppColors.surfaceLow,
|
|
padding: EdgeInsets.symmetric(horizontal: esMovil ? 12 : 24),
|
|
child: Row(children: [
|
|
if (esMovil)
|
|
Builder(
|
|
builder: (ctx) => IconButton(
|
|
icon: const Icon(Icons.menu, color: AppColors.onSurface, size: 22),
|
|
tooltip: 'Menú de navegación',
|
|
onPressed: () => Scaffold.of(ctx).openDrawer(),
|
|
),
|
|
),
|
|
// Logo + nombre app
|
|
Container(
|
|
width: 32,
|
|
height: 32,
|
|
decoration: const BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: AppColors.primaryContainer,
|
|
),
|
|
child: const Icon(Icons.shield, color: AppColors.primary, size: 18),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Text(
|
|
esMovil ? 'MONITOREO' : 'CENTRO DE MONITOREO',
|
|
style: appHeadline(
|
|
size: esMovil ? 13 : 14,
|
|
weight: FontWeight.w800,
|
|
color: AppColors.primary,
|
|
spacing: 1.2),
|
|
),
|
|
const Spacer(),
|
|
// Estado API
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: (_apiConectada ? AppColors.primaryContainer : AppColors.surfaceHighest),
|
|
borderRadius: BorderRadius.circular(20),
|
|
),
|
|
child: Row(mainAxisSize: MainAxisSize.min, children: [
|
|
Container(
|
|
width: 7,
|
|
height: 7,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: _apiConectada
|
|
? const Color(0xFF4CAF50)
|
|
: AppColors.outline,
|
|
),
|
|
),
|
|
const SizedBox(width: 6),
|
|
Text(
|
|
_apiConectada ? 'Online' : 'Offline',
|
|
style: appLabel(
|
|
size: 11,
|
|
color: _apiConectada ? AppColors.primary : AppColors.outline),
|
|
),
|
|
]),
|
|
),
|
|
// Comentario explicativo: Indicador visual prioritario de alarmas críticas en el header con salto directo a Monitoreo
|
|
ValueListenableBuilder<int>(
|
|
valueListenable: Sincronizador.instance.criticalAlarmsCount,
|
|
builder: (ctx, count, _) {
|
|
if (count == 0) return const SizedBox.shrink();
|
|
return Padding(
|
|
padding: const EdgeInsets.only(left: 6),
|
|
child: InkWell(
|
|
onTap: () => setState(() => _indice = 1),
|
|
borderRadius: BorderRadius.circular(20),
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.alarmaActiva.withAlpha(40),
|
|
borderRadius: BorderRadius.circular(20),
|
|
border: Border.all(color: AppColors.alarmaActiva),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.warning_amber_rounded, size: 14, color: AppColors.alarmaActiva),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
'$count CRÍTICA${count > 1 ? 'S' : ''}',
|
|
style: appHeadline(size: 11, color: AppColors.alarmaActiva, weight: FontWeight.bold),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
const SizedBox(width: 4),
|
|
// Comentario explicativo: Botón de sincronización con indicador giratorio de actividad
|
|
ValueListenableBuilder<bool>(
|
|
valueListenable: Sincronizador.instance.isSyncing,
|
|
builder: (ctx, syncing, _) => IconButton(
|
|
icon: syncing
|
|
? const SizedBox(
|
|
width: 16,
|
|
height: 16,
|
|
child: CircularProgressIndicator(strokeWidth: 2, color: AppColors.primary),
|
|
)
|
|
: const Icon(Icons.sync, color: AppColors.outline, size: 20),
|
|
tooltip: syncing ? 'Sincronizando con Centinel...' : 'Sincronizar ahora',
|
|
onPressed: syncing ? null : () => Sincronizador.instance.sincronizarAhora(),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
// Operador
|
|
Row(children: [
|
|
Container(
|
|
width: 30,
|
|
height: 30,
|
|
decoration: const BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: AppColors.primaryContainer,
|
|
),
|
|
child: Center(
|
|
child: Text(
|
|
_operadorNombre.isNotEmpty ? _operadorNombre[0].toUpperCase() : 'O',
|
|
style: appHeadline(size: 13, weight: FontWeight.bold, color: AppColors.primary),
|
|
),
|
|
),
|
|
),
|
|
if (!esMovil) ...[
|
|
const SizedBox(width: 8),
|
|
Text(_operadorNombre, style: appLabel(size: 13, color: AppColors.onSurface)),
|
|
],
|
|
]),
|
|
const SizedBox(width: 8),
|
|
IconButton(
|
|
icon: const Icon(Icons.logout, color: AppColors.outline, size: 20),
|
|
tooltip: 'Cerrar sesión',
|
|
onPressed: _cerrarSesion,
|
|
),
|
|
]),
|
|
);
|
|
|
|
// ── Sidebar ─────────────────────────────────────────────────────────────────
|
|
Widget _sidebar() => Container(
|
|
width: 220,
|
|
color: AppColors.surfaceLow,
|
|
child: Column(children: [
|
|
const SizedBox(height: 8),
|
|
Expanded(
|
|
child: ListView.builder(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
|
itemCount: _navItems.length,
|
|
itemBuilder: (_, i) => _navTile(i),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
|
|
Widget _navTile(int i, {bool cerrarDrawerAlTocar = false}) {
|
|
final item = _navItems[i];
|
|
final activo = _indice == i;
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 2),
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
borderRadius: BorderRadius.circular(10),
|
|
child: InkWell(
|
|
borderRadius: BorderRadius.circular(10),
|
|
onTap: () {
|
|
setState(() => _indice = i);
|
|
if (cerrarDrawerAlTocar) {
|
|
Navigator.of(context).pop();
|
|
}
|
|
},
|
|
child: AnimatedContainer(
|
|
duration: const Duration(milliseconds: 160),
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 11),
|
|
decoration: BoxDecoration(
|
|
color: activo ? AppColors.primaryContainer : Colors.transparent,
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: Row(children: [
|
|
Icon(
|
|
activo ? item.iconActivo : item.icon,
|
|
size: 20,
|
|
color: activo ? AppColors.primary : AppColors.onSurfaceVariant,
|
|
),
|
|
const SizedBox(width: 12),
|
|
Text(
|
|
item.label,
|
|
style: appLabel(
|
|
size: 13,
|
|
weight: activo ? FontWeight.w600 : FontWeight.normal,
|
|
color: activo ? AppColors.primary : AppColors.onSurfaceVariant,
|
|
),
|
|
),
|
|
]),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _NavItem {
|
|
final IconData icon;
|
|
final IconData iconActivo;
|
|
final String label;
|
|
const _NavItem(this.icon, this.iconActivo, this.label);
|
|
}
|