From 6114c6f7c5bdffe9c0fcca1cd1ef5e54e366a421 Mon Sep 17 00:00:00 2001 From: Pablinux Date: Tue, 14 Apr 2026 00:45:16 -0500 Subject: [PATCH] feat: add messaging page with WhatsApp-style chat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Chat con burbujas enviados/recibidos, timestamps y doble check. Tarjetas de acción fijas: Botón de Pánico y Soporte/Asistencia. Input multi-línea, botón de llamada a central con diálogo de confirmación. Co-Authored-By: Claude Sonnet 4.6 --- lib/pages/mensageria_page.dart | 515 +++++++++++++++++++++++++++++++++ 1 file changed, 515 insertions(+) create mode 100644 lib/pages/mensageria_page.dart diff --git a/lib/pages/mensageria_page.dart b/lib/pages/mensageria_page.dart new file mode 100644 index 0000000..b14e0a9 --- /dev/null +++ b/lib/pages/mensageria_page.dart @@ -0,0 +1,515 @@ +import 'package:flutter/material.dart'; +import '../theme.dart'; + +// ─── Modelo de mensaje ──────────────────────────────────────────────────────── +class _Mensaje { + final String texto; + final bool esMio; + final String hora; + _Mensaje({required this.texto, required this.esMio, required this.hora}); +} + +// ─── Página ─────────────────────────────────────────────────────────────────── +class MensajeriaPage extends StatefulWidget { + const MensajeriaPage({Key? key}) : super(key: key); + + @override + State createState() => _MensajeriaPageState(); +} + +class _MensajeriaPageState extends State { + final _inputController = TextEditingController(); + final _scrollController = ScrollController(); + + final List<_Mensaje> _mensajes = [ + _Mensaje( + texto: 'Sistema armado correctamente. Monitoreo activo en todas las zonas.', + esMio: false, + hora: '08:00', + ), + _Mensaje( + texto: 'El sensor Z-03 marcó una alerta hace unos minutos, ¿qué pasó?', + esMio: true, + hora: '10:46', + ), + _Mensaje( + texto: 'Confirmado. Detectamos movimiento en Almacén G-4. Enviamos una unidad para verificar. Estamos en contacto.', + esMio: false, + hora: '10:47', + ), + _Mensaje( + texto: 'Entendido. ¿Cuánto tiempo tarda la unidad en llegar?', + esMio: true, + hora: '10:48', + ), + _Mensaje( + texto: 'Aproximadamente 8 minutos. Por favor, no ingrese a la zona hasta recibir confirmación de seguridad.', + esMio: false, + hora: '10:48', + ), + ]; + + @override + void dispose() { + _inputController.dispose(); + _scrollController.dispose(); + super.dispose(); + } + + void _enviarMensaje() { + final texto = _inputController.text.trim(); + if (texto.isEmpty) return; + + setState(() { + _mensajes.add(_Mensaje(texto: texto, esMio: true, hora: _horaActual())); + _inputController.clear(); + }); + + // Scroll al final + WidgetsBinding.instance!.addPostFrameCallback((_) { + if (_scrollController.hasClients) { + _scrollController.animateTo( + _scrollController.position.maxScrollExtent, + duration: const Duration(milliseconds: 300), + curve: Curves.easeOut, + ); + } + }); + } + + void _llamarCentral() { + showDialog( + context: context, + builder: (_) => AlertDialog( + backgroundColor: AppColors.surfaceHigh, + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), + title: Row(children: [ + Container( + width: 40, + height: 40, + decoration: BoxDecoration( + color: AppColors.primaryContainer, + shape: BoxShape.circle, + ), + child: const Icon(Icons.call, color: AppColors.primary, size: 20), + ), + const SizedBox(width: 12), + Expanded( + child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ + Text('Llamar a Central', + style: appHeadline(size: 15, weight: FontWeight.bold)), + Text('Central de Monitoreo', + style: appLabel(size: 12)), + ]), + ), + ]), + content: Text( + '¿Deseas iniciar una llamada de voz con la central de monitoreo?', + style: appLabel(size: 13, color: AppColors.onSurfaceVariant), + ), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: Text('Cancelar', + style: appLabel( + size: 13, color: AppColors.onSurfaceVariant)), + ), + ElevatedButton.icon( + onPressed: () { + Navigator.pop(context); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + backgroundColor: AppColors.surfaceHigh, + content: Row(children: [ + const Icon(Icons.call, color: AppColors.primary, size: 18), + const SizedBox(width: 10), + Text('Conectando con Central de Monitoreo...', + style: appLabel( + size: 13, color: AppColors.onSurface)), + ]), + duration: const Duration(seconds: 3), + ), + ); + }, + icon: const Icon(Icons.call, size: 16), + label: Text('Llamar', + style: appLabel( + size: 13, + weight: FontWeight.bold, + color: AppColors.primaryContainer)), + style: ElevatedButton.styleFrom( + primary: AppColors.primary, + onPrimary: AppColors.primaryContainer, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(8)), + elevation: 0, + ), + ), + ], + ), + ); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + backgroundColor: AppColors.surfaceLowest, + appBar: _appBar(), + body: Column(children: [ + _tarjetasAccion(), + Expanded(child: _listaMensajes()), + _inputBar(), + ]), + ); + } + + // ── App Bar ──────────────────────────────────────────────────────────────── + PreferredSizeWidget _appBar() => AppBar( + backgroundColor: AppColors.surfaceLow, + elevation: 0, + leading: IconButton( + icon: const Icon(Icons.arrow_back_ios_new, + color: AppColors.onSurface, size: 18), + onPressed: () => Navigator.pop(context), + ), + title: Row(children: [ + // Avatar operador + Container( + width: 40, + height: 40, + decoration: BoxDecoration( + shape: BoxShape.circle, + color: AppColors.primaryContainer, + ), + child: const Icon(Icons.shield, + color: AppColors.primary, size: 20), + ), + const SizedBox(width: 12), + Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ + Text('Central de Monitoreo', + style: appHeadline(size: 15, weight: FontWeight.w700)), + Row(children: [ + Container( + width: 7, + height: 7, + decoration: const BoxDecoration( + shape: BoxShape.circle, + color: Color(0xFF4CAF50), + ), + ), + const SizedBox(width: 5), + Text('En línea', + style: appLabel( + size: 11, color: AppColors.onSurfaceVariant)), + ]), + ]), + ]), + actions: [ + IconButton( + onPressed: _llamarCentral, + icon: Container( + width: 36, + height: 36, + decoration: BoxDecoration( + color: AppColors.primaryContainer, + borderRadius: BorderRadius.circular(10), + ), + child: const Icon(Icons.call, + color: AppColors.primary, size: 18), + ), + ), + const SizedBox(width: 8), + ], + ); + + // ── Tarjetas de acción rápida ────────────────────────────────────────────── + Widget _tarjetasAccion() => Padding( + padding: const EdgeInsets.fromLTRB(16, 16, 16, 0), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: _accionCard( + color: AppColors.error, + bgColor: const Color(0xFF2A0A0A), + borderColor: AppColors.error, + icon: Icons.warning_rounded, + titulo: 'BOTÓN DE\nPÁNICO', + descripcion: 'Activa alerta de emergencia inmediata', + labelBoton: 'ACTIVAR', + onTap: () { + setState(() { + _mensajes.add(_Mensaje( + texto: '🚨 ALERTA DE PÁNICO ACTIVADA. Requiero asistencia inmediata.', + esMio: true, + hora: _horaActual(), + )); + }); + WidgetsBinding.instance!.addPostFrameCallback((_) { + if (_scrollController.hasClients) { + _scrollController.animateTo( + _scrollController.position.maxScrollExtent, + duration: const Duration(milliseconds: 300), + curve: Curves.easeOut, + ); + } + }); + }, + ), + ), + const SizedBox(width: 12), + Expanded( + child: _accionCard( + color: AppColors.primary, + bgColor: const Color(0xFF001228), + borderColor: AppColors.primary, + icon: Icons.headset_mic_rounded, + titulo: 'SOPORTE Y\nASISTENCIA', + descripcion: 'Comunícate con nuestro equipo 24/7', + labelBoton: 'CONTACTAR', + onTap: () { + setState(() { + _mensajes.add(_Mensaje( + texto: 'Hola, necesito asistencia técnica.', + esMio: true, + hora: _horaActual(), + )); + }); + WidgetsBinding.instance!.addPostFrameCallback((_) { + if (_scrollController.hasClients) { + _scrollController.animateTo( + _scrollController.position.maxScrollExtent, + duration: const Duration(milliseconds: 300), + curve: Curves.easeOut, + ); + } + }); + }, + ), + ), + ], + ), + ); + + Widget _accionCard({ + required Color color, + required Color bgColor, + required Color borderColor, + required IconData icon, + required String titulo, + required String descripcion, + required String labelBoton, + required VoidCallback onTap, + }) => + Container( + padding: const EdgeInsets.all(14), + decoration: BoxDecoration( + color: bgColor, + borderRadius: BorderRadius.circular(16), + border: Border.all(color: borderColor.withOpacity(0.4), width: 1), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( + width: 40, + height: 40, + decoration: BoxDecoration( + color: color.withOpacity(0.15), + borderRadius: BorderRadius.circular(10), + ), + child: Icon(icon, color: color, size: 22), + ), + const SizedBox(height: 10), + Text(titulo, + style: appHeadline( + size: 13, weight: FontWeight.w800, + color: AppColors.onSurface)), + const SizedBox(height: 4), + Text(descripcion, + style: appLabel(size: 10, color: AppColors.onSurfaceVariant), + maxLines: 2), + const SizedBox(height: 12), + SizedBox( + width: double.infinity, + child: ElevatedButton( + onPressed: onTap, + style: ElevatedButton.styleFrom( + primary: color, + onPrimary: Colors.white, + padding: const EdgeInsets.symmetric(vertical: 9), + elevation: 0, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(8)), + ), + child: Text(labelBoton, + style: appLabel( + size: 11, + weight: FontWeight.bold, + color: Colors.white, + spacing: 1.5)), + ), + ), + ], + ), + ); + + String _horaActual() { + final t = TimeOfDay.now(); + return '${t.hour.toString().padLeft(2, '0')}:${t.minute.toString().padLeft(2, '0')}'; + } + + // ── Lista de mensajes ────────────────────────────────────────────────────── + Widget _listaMensajes() => ListView.builder( + controller: _scrollController, + padding: const EdgeInsets.fromLTRB(16, 16, 16, 8), + itemCount: _mensajes.length, + itemBuilder: (_, i) => _burbuja(_mensajes[i]), + ); + + Widget _burbuja(_Mensaje msg) { + final esMio = msg.esMio; + return Padding( + padding: const EdgeInsets.only(bottom: 8), + child: Row( + mainAxisAlignment: + esMio ? MainAxisAlignment.end : MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + // Avatar operador (solo mensajes recibidos) + if (!esMio) ...[ + Container( + width: 32, + height: 32, + decoration: BoxDecoration( + shape: BoxShape.circle, + color: AppColors.primaryContainer, + ), + child: const Icon(Icons.shield, + color: AppColors.primary, size: 16), + ), + const SizedBox(width: 8), + ], + // Burbuja + ConstrainedBox( + constraints: BoxConstraints( + maxWidth: MediaQuery.of(context).size.width * 0.72, + ), + child: Container( + padding: const EdgeInsets.fromLTRB(14, 10, 14, 8), + decoration: BoxDecoration( + color: esMio + ? AppColors.primaryContainer + : AppColors.surfaceHigh, + borderRadius: BorderRadius.only( + topLeft: const Radius.circular(16), + topRight: const Radius.circular(16), + bottomLeft: Radius.circular(esMio ? 16 : 4), + bottomRight: Radius.circular(esMio ? 4 : 16), + ), + ), + child: Column( + crossAxisAlignment: esMio + ? CrossAxisAlignment.end + : CrossAxisAlignment.start, + children: [ + Text( + msg.texto, + style: appLabel( + size: 14, + color: esMio + ? AppColors.primary + : AppColors.onSurface, + ), + ), + const SizedBox(height: 4), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text(msg.hora, + style: appLabel( + size: 10, + color: esMio + ? AppColors.primary.withOpacity(0.6) + : AppColors.outline, + )), + if (esMio) ...[ + const SizedBox(width: 4), + Icon(Icons.done_all, + size: 14, + color: AppColors.primary.withOpacity(0.6)), + ], + ], + ), + ], + ), + ), + ), + // Espaciador para mensajes propios + if (esMio) const SizedBox(width: 4), + ], + ), + ); + } + + // ── Barra de entrada ─────────────────────────────────────────────────────── + Widget _inputBar() => Container( + padding: EdgeInsets.fromLTRB( + 16, 10, 16, 10 + MediaQuery.of(context).padding.bottom), + decoration: BoxDecoration( + color: AppColors.surfaceLow, + boxShadow: [ + BoxShadow( + color: Colors.black.withOpacity(0.2), + offset: const Offset(0, -4), + blurRadius: 16, + ), + ], + ), + child: Row(children: [ + // Campo de texto + Expanded( + child: Container( + decoration: BoxDecoration( + color: AppColors.surfaceHighest, + borderRadius: BorderRadius.circular(24), + ), + child: TextField( + controller: _inputController, + style: appLabel(size: 14, color: AppColors.onSurface), + minLines: 1, + maxLines: 4, + textCapitalization: TextCapitalization.sentences, + decoration: InputDecoration( + hintText: 'Escribe un mensaje...', + hintStyle: appLabel( + size: 14, color: AppColors.outline), + border: InputBorder.none, + contentPadding: const EdgeInsets.symmetric( + horizontal: 18, vertical: 10), + // Ícono adjunto + prefixIcon: const Icon(Icons.attach_file, + color: AppColors.outline, size: 20), + ), + onSubmitted: (_) => _enviarMensaje(), + ), + ), + ), + const SizedBox(width: 10), + // Botón enviar + GestureDetector( + onTap: _enviarMensaje, + child: Container( + width: 46, + height: 46, + decoration: BoxDecoration( + color: AppColors.primary, + shape: BoxShape.circle, + ), + child: const Icon(Icons.send_rounded, + color: AppColors.primaryContainer, size: 20), + ), + ), + ]), + ); +}