411 lines
14 KiB
Dart
411 lines
14 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../theme.dart';
|
|
import '../db/dao/mensajes_dao.dart';
|
|
import '../models/mensaje.dart';
|
|
import 'llamada_page.dart';
|
|
|
|
// ─── Página ───────────────────────────────────────────────────────────────────
|
|
class MensajeriaPage extends StatefulWidget {
|
|
const MensajeriaPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<MensajeriaPage> createState() => _MensajeriaPageState();
|
|
}
|
|
|
|
class _MensajeriaPageState extends State<MensajeriaPage> {
|
|
final _inputController = TextEditingController();
|
|
final _scrollController = ScrollController();
|
|
final _dao = MensajesDao();
|
|
|
|
List<Mensaje> _mensajes = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_cargar();
|
|
}
|
|
|
|
Future<void> _cargar() async {
|
|
final lista = await _dao.getAll();
|
|
if (!mounted) return;
|
|
setState(() => _mensajes = lista);
|
|
_scrollAlFinal();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_inputController.dispose();
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _enviarMensaje() async {
|
|
final texto = _inputController.text.trim();
|
|
if (texto.isEmpty) return;
|
|
|
|
final nuevo = Mensaje(
|
|
texto: texto,
|
|
esMio: true,
|
|
hora: _horaActual(),
|
|
timestamp: DateTime.now().toIso8601String(),
|
|
);
|
|
_inputController.clear();
|
|
await _dao.insert(nuevo);
|
|
await _cargar();
|
|
}
|
|
|
|
void _scrollAlFinal() {
|
|
WidgetsBinding.instance!.addPostFrameCallback((_) {
|
|
if (_scrollController.hasClients) {
|
|
_scrollController.animateTo(
|
|
_scrollController.position.maxScrollExtent,
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeOut,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
void _llamarCentral() {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
fullscreenDialog: true,
|
|
builder: (_) => const LlamadaPage(),
|
|
),
|
|
);
|
|
}
|
|
|
|
@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 (compactas) ────────────────────────────────
|
|
Widget _tarjetasAccion() => Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 10, 16, 0),
|
|
child: Row(children: [
|
|
Expanded(
|
|
child: _accionChip(
|
|
color: AppColors.error,
|
|
bgColor: const Color(0xFF2A0A0A),
|
|
icon: Icons.warning_rounded,
|
|
label: 'PÁNICO',
|
|
labelBoton: 'ACTIVAR',
|
|
onTap: () async {
|
|
final m = Mensaje(
|
|
texto: 'ALERTA DE PÁNICO ACTIVADA. Requiero asistencia inmediata.',
|
|
esMio: true,
|
|
hora: _horaActual(),
|
|
timestamp: DateTime.now().toIso8601String(),
|
|
);
|
|
await _dao.insert(m);
|
|
await _cargar();
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Expanded(
|
|
child: _accionChip(
|
|
color: AppColors.primary,
|
|
bgColor: const Color(0xFF001228),
|
|
icon: Icons.headset_mic_rounded,
|
|
label: 'SOPORTE',
|
|
labelBoton: 'CONTACTAR',
|
|
onTap: () async {
|
|
final m = Mensaje(
|
|
texto: 'Hola, necesito asistencia técnica.',
|
|
esMio: true,
|
|
hora: _horaActual(),
|
|
timestamp: DateTime.now().toIso8601String(),
|
|
);
|
|
await _dao.insert(m);
|
|
await _cargar();
|
|
},
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
|
|
Widget _accionChip({
|
|
required Color color,
|
|
required Color bgColor,
|
|
required IconData icon,
|
|
required String label,
|
|
required String labelBoton,
|
|
required VoidCallback onTap,
|
|
}) =>
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
|
|
decoration: BoxDecoration(
|
|
color: bgColor,
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: color.withOpacity(0.35), width: 1),
|
|
),
|
|
child: Row(children: [
|
|
Icon(icon, color: color, size: 18),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(label,
|
|
style: appLabel(
|
|
size: 11, weight: FontWeight.bold,
|
|
color: AppColors.onSurface, spacing: 0.5)),
|
|
),
|
|
const SizedBox(width: 6),
|
|
GestureDetector(
|
|
onTap: onTap,
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
|
decoration: BoxDecoration(
|
|
color: color,
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Text(labelBoton,
|
|
style: appLabel(
|
|
size: 10, weight: FontWeight.bold,
|
|
color: Colors.white, spacing: 0.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() => _mensajes.isEmpty
|
|
? Center(
|
|
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
|
|
const Icon(Icons.chat_bubble_outline,
|
|
color: AppColors.outlineVariant, size: 40),
|
|
const SizedBox(height: 12),
|
|
Text('Sin mensajes',
|
|
style: appLabel(size: 13, color: AppColors.outlineVariant)),
|
|
Text('Carga datos de demo para ver mensajes de ejemplo',
|
|
style: appLabel(size: 11, color: AppColors.outlineVariant),
|
|
textAlign: TextAlign.center),
|
|
]),
|
|
)
|
|
: 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),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
}
|