Files
farolero/lib/pantallas/pantalla_lobby_host.dart
ShanaiaBot 23472707ad
All checks were successful
Build & Deploy Farolero / Análisis de código (push) Successful in 9s
Build & Deploy Farolero / Build APK + AAB release (push) Successful in 1m10s
feat: modo multidispositivo con Nearby Connections
- ServicioNearby completo: P2P_STAR, auto-accept, protocolo mensajes
- PantallaLobbyHost: QR code + lista jugadores tiempo real
- PantallaUnirse: escaneo QR + conexión + sala espera
- Protocolo MensajeP2P: salaInfo, partidaInicio, fase, voto, resultado, fin
- Manejo desconexiones jugador/host
- l10n: nuevas keys es/en
- Version bump 1.1.0+5
2026-04-04 03:09:51 +02:00

210 lines
7.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:farolero/l10n/generated/app_localizations.dart';
import '../servicios/servicio_nearby.dart';
import '../tema/tema_app.dart';
/// Pantalla de lobby del host: muestra QR y lista de jugadores conectados
class PantallaLobbyHost extends StatefulWidget {
final String nombreSala;
final VoidCallback onIniciar;
const PantallaLobbyHost({
super.key,
required this.nombreSala,
required this.onIniciar,
});
@override
State<PantallaLobbyHost> createState() => _PantallaLobbyHostState();
}
class _PantallaLobbyHostState extends State<PantallaLobbyHost> {
bool _iniciando = false;
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final nearby = context.watch<ServicioNearby>();
final jugadores = nearby.jugadores;
final totalJugadores = jugadores.length + 1; // +1 host
return Scaffold(
appBar: AppBar(
title: Text(widget.nombreSala),
leading: IconButton(
icon: const Icon(Icons.close),
onPressed: () async {
await nearby.desconectar();
if (context.mounted) Navigator.pop(context);
},
),
),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
// QR Code
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
),
child: QrImageView(
data: nearby.generarDatosQR(widget.nombreSala),
version: QrVersions.auto,
size: 180,
backgroundColor: Colors.white,
),
),
const SizedBox(height: 12),
Text(
l10n.scanToJoin,
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 24),
// Lista de jugadores
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
l10n.connectedPlayers,
style: Theme.of(context).textTheme.titleLarge,
),
const Spacer(),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 4,
),
decoration: BoxDecoration(
color: totalJugadores >= 3
? TemaApp.colorVerde.withValues(alpha: 0.2)
: TemaApp.colorNaranja.withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(12),
),
child: Text(
'$totalJugadores',
style: TextStyle(
color: totalJugadores >= 3
? TemaApp.colorVerde
: TemaApp.colorNaranja,
fontWeight: FontWeight.bold,
),
),
),
],
),
const SizedBox(height: 12),
// Host (yo)
_buildJugadorTile(
nombre: nearby.miNombre ?? 'Host',
esHost: true,
),
// Jugadores conectados
Expanded(
child: jugadores.isEmpty
? Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('📱', style: TextStyle(fontSize: 48)),
const SizedBox(height: 12),
Text(
l10n.waitingForPlayers,
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
)
: ListView.builder(
itemCount: jugadores.length,
itemBuilder: (context, index) {
final j = jugadores[index];
return _buildJugadorTile(nombre: j.nombre);
},
),
),
],
),
),
// Botón iniciar
if (totalJugadores < 3)
Text(
l10n.needMorePlayers(3 - totalJugadores),
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: TemaApp.colorNaranja,
),
),
const SizedBox(height: 12),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: totalJugadores >= 3 && !_iniciando
? () {
setState(() => _iniciando = true);
widget.onIniciar();
}
: null,
icon: const Icon(Icons.play_arrow),
label: Text(_iniciando ? l10n.starting : l10n.startGame),
),
),
],
),
),
);
}
Widget _buildJugadorTile({required String nombre, bool esHost = false}) {
return Container(
margin: const EdgeInsets.only(bottom: 8),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: TemaApp.colorTarjeta,
borderRadius: BorderRadius.circular(12),
border: esHost
? Border.all(color: TemaApp.colorAcento.withValues(alpha: 0.5))
: null,
),
child: Row(
children: [
Text(esHost ? '👑' : '🎭', style: const TextStyle(fontSize: 20)),
const SizedBox(width: 12),
Expanded(
child: Text(
nombre,
style: Theme.of(context).textTheme.titleMedium,
),
),
if (esHost)
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: TemaApp.colorAcento.withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'HOST',
style: TextStyle(
color: TemaApp.colorAcento,
fontSize: 10,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
}