Gestión de usuarios y avatares en la aplicación. Gestión de traducciones de las palabras.
BIN
assets/avatars/avatar_01.png
Normal file
|
After Width: | Height: | Size: 9.4 KiB |
BIN
assets/avatars/avatar_02.png
Normal file
|
After Width: | Height: | Size: 8.6 KiB |
BIN
assets/avatars/avatar_03.png
Normal file
|
After Width: | Height: | Size: 8.7 KiB |
BIN
assets/avatars/avatar_04.png
Normal file
|
After Width: | Height: | Size: 8.4 KiB |
BIN
assets/avatars/avatar_05.png
Normal file
|
After Width: | Height: | Size: 9.1 KiB |
BIN
assets/avatars/avatar_06.png
Normal file
|
After Width: | Height: | Size: 9.4 KiB |
BIN
assets/avatars/avatar_07.png
Normal file
|
After Width: | Height: | Size: 8.6 KiB |
BIN
assets/avatars/avatar_08.png
Normal file
|
After Width: | Height: | Size: 9.2 KiB |
BIN
assets/avatars/avatar_09.png
Normal file
|
After Width: | Height: | Size: 8.4 KiB |
BIN
assets/avatars/avatar_10.png
Normal file
|
After Width: | Height: | Size: 8.7 KiB |
BIN
assets/avatars/avatar_11.png
Normal file
|
After Width: | Height: | Size: 9.2 KiB |
BIN
assets/avatars/avatar_12.png
Normal file
|
After Width: | Height: | Size: 9.3 KiB |
BIN
assets/avatars/avatar_13.png
Normal file
|
After Width: | Height: | Size: 8.6 KiB |
BIN
assets/avatars/avatar_14.png
Normal file
|
After Width: | Height: | Size: 8.4 KiB |
BIN
assets/avatars/avatar_15.png
Normal file
|
After Width: | Height: | Size: 9.2 KiB |
BIN
assets/avatars/avatar_16.png
Normal file
|
After Width: | Height: | Size: 8.9 KiB |
BIN
assets/avatars/avatar_17.png
Normal file
|
After Width: | Height: | Size: 8.8 KiB |
BIN
assets/avatars/avatar_18.png
Normal file
|
After Width: | Height: | Size: 8.8 KiB |
BIN
assets/avatars/avatar_19.png
Normal file
|
After Width: | Height: | Size: 8.3 KiB |
BIN
assets/avatars/avatar_20.png
Normal file
|
After Width: | Height: | Size: 8.8 KiB |
BIN
assets/avatars/avatar_21.png
Normal file
|
After Width: | Height: | Size: 9.2 KiB |
BIN
assets/avatars/avatar_22.png
Normal file
|
After Width: | Height: | Size: 8.7 KiB |
BIN
assets/avatars/avatar_23.png
Normal file
|
After Width: | Height: | Size: 8.5 KiB |
BIN
assets/avatars/avatar_24.png
Normal file
|
After Width: | Height: | Size: 8.5 KiB |
BIN
assets/avatars/avatar_25.png
Normal file
|
After Width: | Height: | Size: 8.9 KiB |
BIN
assets/avatars/avatar_26.png
Normal file
|
After Width: | Height: | Size: 8.5 KiB |
BIN
assets/avatars/avatar_27.png
Normal file
|
After Width: | Height: | Size: 7.9 KiB |
BIN
assets/avatars/avatar_28.png
Normal file
|
After Width: | Height: | Size: 8.8 KiB |
BIN
assets/avatars/avatar_29.png
Normal file
|
After Width: | Height: | Size: 8.3 KiB |
BIN
assets/avatars/avatar_30.png
Normal file
|
After Width: | Height: | Size: 8.2 KiB |
BIN
assets/avatars/capybara_01.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
assets/avatars/capybara_02.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
assets/avatars/capybara_03.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/avatars/capybara_04.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/avatars/capybara_05.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/avatars/capybara_06.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
assets/avatars/capybara_07.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/avatars/capybara_08.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
assets/avatars/capybara_09.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/avatars/capybara_10.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/avatars/capybara_11.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/avatars/capybara_12.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
22026
assets/palabras_i18n.json
Normal file
@@ -4,8 +4,10 @@ import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:farolero/l10n/generated/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'estado/estado_juego.dart';
|
||||
import 'servicios/servicio_historial_partidas.dart';
|
||||
import 'servicios/servicio_idioma.dart';
|
||||
import 'servicios/servicio_nearby.dart';
|
||||
import 'servicios/servicio_perfil_usuario.dart';
|
||||
import 'tema/componentes_farolero.dart';
|
||||
import 'tema/tema_app.dart';
|
||||
import 'pantallas/pantalla_principal.dart';
|
||||
@@ -36,6 +38,12 @@ class FaroleroApp extends StatelessWidget {
|
||||
ChangeNotifierProvider(
|
||||
create: (_) => ServicioIdioma()..cargar(),
|
||||
),
|
||||
ChangeNotifierProvider(
|
||||
create: (_) => ServicioPerfilUsuario()..cargar(),
|
||||
),
|
||||
ChangeNotifierProvider(
|
||||
create: (_) => ServicioHistorialPartidas()..cargar(),
|
||||
),
|
||||
ChangeNotifierProvider(
|
||||
create: (_) => ServicioNearby(),
|
||||
),
|
||||
|
||||
@@ -95,3 +95,48 @@ class BancoPalabras {
|
||||
return nombres[clave] ?? clave;
|
||||
}
|
||||
}
|
||||
|
||||
class EntradaPalabraTraducida {
|
||||
final String palabra;
|
||||
final Map<String, String> traducciones;
|
||||
|
||||
const EntradaPalabraTraducida({
|
||||
required this.palabra,
|
||||
required this.traducciones,
|
||||
});
|
||||
}
|
||||
|
||||
class BancoPalabrasTraducidas {
|
||||
final Map<String, List<EntradaPalabraTraducida>> categorias;
|
||||
|
||||
const BancoPalabrasTraducidas(this.categorias);
|
||||
|
||||
static BancoPalabrasTraducidas? _instancia;
|
||||
|
||||
static Future<BancoPalabrasTraducidas> cargar() async {
|
||||
if (_instancia != null) return _instancia!;
|
||||
|
||||
final jsonStr = await rootBundle.loadString('assets/palabras_i18n.json');
|
||||
final data = json.decode(jsonStr) as Map<String, dynamic>;
|
||||
final cats = data['categorias'] as Map<String, dynamic>;
|
||||
final mapa = <String, List<EntradaPalabraTraducida>>{};
|
||||
|
||||
for (final categoria in cats.entries) {
|
||||
final entradas = categoria.value as List<dynamic>;
|
||||
mapa[categoria.key] = entradas.map((entradaRaw) {
|
||||
final entrada = entradaRaw as Map<String, dynamic>;
|
||||
final traduccionesRaw =
|
||||
entrada['traducciones'] as Map<String, dynamic>? ?? {};
|
||||
return EntradaPalabraTraducida(
|
||||
palabra: entrada['es'] as String,
|
||||
traducciones: traduccionesRaw.map(
|
||||
(idioma, valor) => MapEntry(idioma, valor?.toString() ?? ''),
|
||||
)..removeWhere((_, valor) => valor.isEmpty),
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
_instancia = BancoPalabrasTraducidas(mapa);
|
||||
return _instancia!;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,18 @@
|
||||
class Usuario {
|
||||
final String id;
|
||||
final String nombre;
|
||||
final String? nick;
|
||||
final String? avatar;
|
||||
final String? foto;
|
||||
final String? creadoPorClienteId;
|
||||
final String? clienteIdSeleccionado;
|
||||
|
||||
Usuario({
|
||||
required this.id,
|
||||
required this.nombre,
|
||||
this.nick,
|
||||
this.avatar,
|
||||
this.foto,
|
||||
this.creadoPorClienteId,
|
||||
this.clienteIdSeleccionado,
|
||||
});
|
||||
@@ -20,7 +24,9 @@ class Usuario {
|
||||
Usuario copiar({
|
||||
String? id,
|
||||
String? nombre,
|
||||
String? nick,
|
||||
String? avatar,
|
||||
String? foto,
|
||||
String? creadoPorClienteId,
|
||||
String? clienteIdSeleccionado,
|
||||
bool liberarSeleccion = false,
|
||||
@@ -28,7 +34,9 @@ class Usuario {
|
||||
return Usuario(
|
||||
id: id ?? this.id,
|
||||
nombre: nombre ?? this.nombre,
|
||||
nick: nick ?? this.nick,
|
||||
avatar: avatar ?? this.avatar,
|
||||
foto: foto ?? this.foto,
|
||||
creadoPorClienteId: creadoPorClienteId ?? this.creadoPorClienteId,
|
||||
clienteIdSeleccionado: liberarSeleccion
|
||||
? null
|
||||
@@ -39,7 +47,9 @@ class Usuario {
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'nombre': nombre,
|
||||
if (nick != null) 'nick': nick,
|
||||
if (avatar != null) 'avatar': avatar,
|
||||
if (foto != null) 'foto': foto,
|
||||
if (creadoPorClienteId != null) 'creadoPorClienteId': creadoPorClienteId,
|
||||
if (clienteIdSeleccionado != null)
|
||||
'clienteIdSeleccionado': clienteIdSeleccionado,
|
||||
@@ -48,7 +58,9 @@ class Usuario {
|
||||
factory Usuario.fromJson(Map<String, dynamic> json) => Usuario(
|
||||
id: json['id'] as String,
|
||||
nombre: json['nombre'] as String,
|
||||
nick: json['nick'] as String?,
|
||||
avatar: json['avatar'] as String?,
|
||||
foto: json['foto'] as String?,
|
||||
creadoPorClienteId: json['creadoPorClienteId'] as String?,
|
||||
clienteIdSeleccionado: json['clienteIdSeleccionado'] as String?,
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:farolero/l10n/generated/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../servicios/servicio_idioma.dart';
|
||||
import '../servicios/servicio_perfil_usuario.dart';
|
||||
import '../tema/componentes_farolero.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
|
||||
@@ -20,6 +21,7 @@ class _PantallaAjustesState extends State<PantallaAjustes> {
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final servicioIdioma = context.watch<ServicioIdioma>();
|
||||
final perfil = context.watch<ServicioPerfilUsuario>().perfil;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(l10n.settingsTitle)),
|
||||
@@ -29,6 +31,21 @@ class _PantallaAjustesState extends State<PantallaAjustes> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Card(
|
||||
child: ListTile(
|
||||
leading: AvatarFarolero(
|
||||
texto: perfil.nombre.substring(0, 1).toUpperCase(),
|
||||
assetPath: perfil.avatarAsset,
|
||||
size: 48,
|
||||
),
|
||||
title: Text(perfil.nombre),
|
||||
subtitle: Text('@${perfil.nick}'),
|
||||
trailing: const Icon(Icons.edit),
|
||||
onTap: () => _editarPerfil(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Selector de idioma
|
||||
Card(
|
||||
child: Padding(
|
||||
@@ -173,6 +190,107 @@ class _PantallaAjustesState extends State<PantallaAjustes> {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _editarPerfil(BuildContext context) async {
|
||||
final servicioPerfil = context.read<ServicioPerfilUsuario>();
|
||||
final actual = servicioPerfil.perfil;
|
||||
final nombreController = TextEditingController(text: actual.nombre);
|
||||
final nickController = TextEditingController(text: actual.nick);
|
||||
var avatarSeleccionado = actual.avatarAsset;
|
||||
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (ctx) => StatefulBuilder(
|
||||
builder: (ctx, setDialogState) => AlertDialog(
|
||||
title: const Text('Perfil del dispositivo'),
|
||||
content: SizedBox(
|
||||
width: 420,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: nombreController,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nombre',
|
||||
prefixIcon: Icon(Icons.person),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: nickController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nick',
|
||||
prefixIcon: Icon(Icons.alternate_email),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate:
|
||||
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 5,
|
||||
mainAxisSpacing: 8,
|
||||
crossAxisSpacing: 8,
|
||||
),
|
||||
itemCount: ServicioPerfilUsuario.avatares.length,
|
||||
itemBuilder: (context, index) {
|
||||
final avatar = ServicioPerfilUsuario.avatares[index];
|
||||
final seleccionado = avatar == avatarSeleccionado;
|
||||
return InkWell(
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
onTap: () => setDialogState(
|
||||
() => avatarSeleccionado = avatar,
|
||||
),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(
|
||||
color: seleccionado
|
||||
? TemaApp.colorNaranja
|
||||
: Colors.transparent,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
child: AvatarFarolero(
|
||||
texto: '',
|
||||
assetPath: avatar,
|
||||
size: 48,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await servicioPerfil.guardar(
|
||||
nombre: nombreController.text,
|
||||
nick: nickController.text,
|
||||
avatarAsset: avatarSeleccionado,
|
||||
);
|
||||
if (ctx.mounted) Navigator.pop(ctx);
|
||||
},
|
||||
child: const Text('Guardar'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
nombreController.dispose();
|
||||
nickController.dispose();
|
||||
}
|
||||
|
||||
String _nombreIdiomaDelSistema() {
|
||||
final locale = WidgetsBinding.instance.platformDispatcher.locale;
|
||||
final codigo = locale.countryCode != null && locale.countryCode!.isNotEmpty
|
||||
|
||||
@@ -8,6 +8,7 @@ import '../modelos/partida.dart';
|
||||
import '../modelos/usuario.dart';
|
||||
import '../servicios/servicio_nearby.dart';
|
||||
import '../servicios/servicio_permisos.dart';
|
||||
import '../servicios/servicio_perfil_usuario.dart';
|
||||
import '../tema/componentes_farolero.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
import 'pantalla_gestor_host.dart';
|
||||
@@ -131,8 +132,14 @@ class _PantallaCrearPartidaState extends State<PantallaCrearPartida> {
|
||||
// 3. Iniciar host en Nearby
|
||||
if (!mounted) return;
|
||||
final nearby = context.read<ServicioNearby>();
|
||||
final perfil = context.read<ServicioPerfilUsuario>().perfil;
|
||||
final nombreSala = '${nombre.trim()} - Farolero';
|
||||
final ok = await nearby.iniciarHost(nombreSala, nombre.trim());
|
||||
final ok = await nearby.iniciarHost(
|
||||
nombreSala,
|
||||
nombre.trim(),
|
||||
miNick: perfil.nick,
|
||||
miAvatar: perfil.avatarAsset,
|
||||
);
|
||||
|
||||
if (!ok) {
|
||||
if (mounted) {
|
||||
@@ -279,7 +286,11 @@ class _PantallaCrearPartidaState extends State<PantallaCrearPartida> {
|
||||
value: usuario.nombre,
|
||||
child: Row(
|
||||
children: [
|
||||
Text(usuario.avatar ?? '👤'),
|
||||
AvatarFarolero(
|
||||
texto: usuario.nombre.substring(0, 1),
|
||||
assetPath: usuario.avatar,
|
||||
size: 28,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(usuario.nombre),
|
||||
],
|
||||
@@ -324,6 +335,7 @@ class _PantallaCrearPartidaState extends State<PantallaCrearPartida> {
|
||||
final controller = TextEditingController();
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final nearby = context.read<ServicioNearby>();
|
||||
controller.text = context.read<ServicioPerfilUsuario>().perfil.nombre;
|
||||
|
||||
final nombre = await showDialog<String>(
|
||||
context: context,
|
||||
@@ -356,6 +368,9 @@ class _PantallaCrearPartidaState extends State<PantallaCrearPartida> {
|
||||
final nuevoUsuario = Usuario(
|
||||
id: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
nombre: nombre.trim(),
|
||||
nick: context.read<ServicioPerfilUsuario>().perfil.nick,
|
||||
avatar: context.read<ServicioPerfilUsuario>().perfil.avatarAsset,
|
||||
foto: context.read<ServicioPerfilUsuario>().perfil.avatarAsset,
|
||||
);
|
||||
nearby.agregarUsuario(nuevoUsuario);
|
||||
return nombre.trim();
|
||||
@@ -366,6 +381,7 @@ class _PantallaCrearPartidaState extends State<PantallaCrearPartida> {
|
||||
/// Método original para pedir nombre (usado cuando pool vacío)
|
||||
Future<String?> _pedirNombreHost() async {
|
||||
final controller = TextEditingController();
|
||||
controller.text = context.read<ServicioPerfilUsuario>().perfil.nombre;
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
return showDialog<String>(
|
||||
context: context,
|
||||
|
||||
@@ -3,13 +3,21 @@ import 'package:farolero/l10n/generated/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../estado/estado_juego.dart';
|
||||
import '../modelos/palabra.dart';
|
||||
import '../servicios/servicio_historial_partidas.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
import 'pantalla_principal.dart';
|
||||
import 'pantalla_ver_palabra.dart';
|
||||
|
||||
class PantallaFinPartida extends StatelessWidget {
|
||||
class PantallaFinPartida extends StatefulWidget {
|
||||
const PantallaFinPartida({super.key});
|
||||
|
||||
@override
|
||||
State<PantallaFinPartida> createState() => _PantallaFinPartidaState();
|
||||
}
|
||||
|
||||
class _PantallaFinPartidaState extends State<PantallaFinPartida> {
|
||||
bool _guardada = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
@@ -20,6 +28,14 @@ class PantallaFinPartida extends StatelessWidget {
|
||||
final ganaronJugadores = partida.ganador == 'jugadores';
|
||||
final impostores =
|
||||
partida.jugadores.where((j) => j.esImpostor).toList();
|
||||
if (!_guardada && partida.ganador != null) {
|
||||
_guardada = true;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
context.read<ServicioHistorialPartidas>().guardarPartida(partida);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
|
||||
58
lib/pantallas/pantalla_historial.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../servicios/servicio_historial_partidas.dart';
|
||||
import '../tema/componentes_farolero.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
|
||||
class PantallaHistorial extends StatelessWidget {
|
||||
const PantallaHistorial({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final historial = context.watch<ServicioHistorialPartidas>();
|
||||
final partidas = historial.partidas;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Historial')),
|
||||
body: FondoFarolero(
|
||||
child: partidas.isEmpty
|
||||
? const Center(child: Text('Todavía no hay partidas guardadas.'))
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: partidas.length,
|
||||
itemBuilder: (context, index) {
|
||||
final partida = partidas[index];
|
||||
final ganaronJugadores = partida.ganador == 'jugadores';
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: ganaronJugadores
|
||||
? TemaApp.colorVerde
|
||||
: TemaApp.colorAcento,
|
||||
child: Icon(
|
||||
ganaronJugadores ? Icons.groups : Icons.theater_comedy,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
ganaronJugadores
|
||||
? 'Ganaron los jugadores'
|
||||
: 'Ganaron los impostores',
|
||||
),
|
||||
subtitle: Text(
|
||||
'${partida.jugadores} jugadores · ${partida.impostores} impostor(es) · ${partida.rondas} ronda(s)\n${partida.palabra} · ${partida.categoria}',
|
||||
),
|
||||
isThreeLine: true,
|
||||
trailing: Text(
|
||||
'${partida.fecha.day.toString().padLeft(2, '0')}/${partida.fecha.month.toString().padLeft(2, '0')}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
import 'package:flutter/material.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 '../modelos/usuario.dart';
|
||||
import '../servicios/servicio_nearby.dart';
|
||||
import '../servicios/servicio_perfil_usuario.dart';
|
||||
import '../tema/componentes_farolero.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
|
||||
@@ -208,7 +209,8 @@ class _PantallaLobbyHostState extends State<PantallaLobbyHost> {
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: Colors.transparent,
|
||||
child: AvatarFarolero(
|
||||
texto: usuario.avatar ?? (usuario.nombre.isEmpty ? '?' : usuario.nombre[0]),
|
||||
texto: usuario.nombre.isEmpty ? '?' : usuario.nombre[0],
|
||||
assetPath: usuario.avatar,
|
||||
color: seleccionadoPorMi
|
||||
? TemaApp.colorVerde
|
||||
: seleccionadoPorOtro
|
||||
@@ -297,7 +299,13 @@ class _PantallaLobbyHostState extends State<PantallaLobbyHost> {
|
||||
);
|
||||
|
||||
if (nombre != null && nombre.trim().isNotEmpty) {
|
||||
await nearby.crearUsuarioSala(nombre.trim(), seleccionar: true);
|
||||
final perfil = context.read<ServicioPerfilUsuario>().perfil;
|
||||
await nearby.crearUsuarioSala(
|
||||
nombre.trim(),
|
||||
seleccionar: true,
|
||||
nick: perfil.nick,
|
||||
avatar: perfil.avatarAsset,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:farolero/l10n/generated/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../servicios/servicio_perfil_usuario.dart';
|
||||
import '../tema/componentes_farolero.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
import 'pantalla_ajustes.dart';
|
||||
import 'pantalla_crear_partida.dart';
|
||||
import 'pantalla_historial.dart';
|
||||
import 'pantalla_reglas.dart';
|
||||
import 'pantalla_unirse.dart';
|
||||
|
||||
@@ -13,6 +16,7 @@ class PantallaPrincipal extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final perfil = context.watch<ServicioPerfilUsuario>().perfil;
|
||||
|
||||
return Scaffold(
|
||||
body: FondoFarolero(
|
||||
@@ -27,18 +31,22 @@ class PantallaPrincipal extends StatelessWidget {
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const AvatarFarolero(texto: 'A', size: 48),
|
||||
AvatarFarolero(
|
||||
texto: perfil.nombre.substring(0, 1).toUpperCase(),
|
||||
assetPath: perfil.avatarAsset,
|
||||
size: 48,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Alex',
|
||||
perfil.nombre,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
Text(
|
||||
'Nivel 12',
|
||||
'@${perfil.nick}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 3),
|
||||
@@ -124,7 +132,14 @@ class PantallaPrincipal extends StatelessWidget {
|
||||
child: AccesoFarolero(
|
||||
etiqueta: 'Historial',
|
||||
icono: Icons.history,
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => const PantallaHistorial(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
|
||||
@@ -7,6 +7,7 @@ import '../modelos/inicio_partida_multijugador.dart';
|
||||
import '../modelos/usuario.dart';
|
||||
import '../servicios/servicio_nearby.dart';
|
||||
import '../servicios/servicio_permisos.dart';
|
||||
import '../servicios/servicio_perfil_usuario.dart';
|
||||
import '../tema/componentes_farolero.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
import 'pantalla_palabra_cliente.dart';
|
||||
@@ -46,6 +47,10 @@ class _PantallaUnirseState extends State<PantallaUnirse> {
|
||||
super.initState();
|
||||
// Registrar listener ANTES del primer build
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final perfil = context.read<ServicioPerfilUsuario>().perfil;
|
||||
if (_nombreController.text.isEmpty) {
|
||||
_nombreController.text = perfil.nombre;
|
||||
}
|
||||
_registrarListenerPartida();
|
||||
});
|
||||
}
|
||||
@@ -701,6 +706,7 @@ class _PantallaUnirseState extends State<PantallaUnirse> {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final controller = TextEditingController();
|
||||
final nearby = context.read<ServicioNearby>();
|
||||
controller.text = context.read<ServicioPerfilUsuario>().perfil.nombre;
|
||||
|
||||
final nombre = await showDialog<String>(
|
||||
context: context,
|
||||
@@ -730,7 +736,13 @@ class _PantallaUnirseState extends State<PantallaUnirse> {
|
||||
);
|
||||
|
||||
if (nombre != null && nombre.trim().isNotEmpty) {
|
||||
await nearby.crearUsuarioSala(nombre.trim(), seleccionar: true);
|
||||
final perfil = context.read<ServicioPerfilUsuario>().perfil;
|
||||
await nearby.crearUsuarioSala(
|
||||
nombre.trim(),
|
||||
seleccionar: true,
|
||||
nick: perfil.nick,
|
||||
avatar: perfil.avatarAsset,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
115
lib/servicios/servicio_historial_partidas.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../modelos/partida.dart';
|
||||
|
||||
class ResultadoPartidaGuardado {
|
||||
final String id;
|
||||
final DateTime fecha;
|
||||
final bool modoMultimovil;
|
||||
final int jugadores;
|
||||
final int impostores;
|
||||
final int rondas;
|
||||
final String ganador;
|
||||
final String palabra;
|
||||
final String categoria;
|
||||
|
||||
const ResultadoPartidaGuardado({
|
||||
required this.id,
|
||||
required this.fecha,
|
||||
required this.modoMultimovil,
|
||||
required this.jugadores,
|
||||
required this.impostores,
|
||||
required this.rondas,
|
||||
required this.ganador,
|
||||
required this.palabra,
|
||||
required this.categoria,
|
||||
});
|
||||
|
||||
factory ResultadoPartidaGuardado.desdePartida(Partida partida) {
|
||||
return ResultadoPartidaGuardado(
|
||||
id: DateTime.now().microsecondsSinceEpoch.toString(),
|
||||
fecha: DateTime.now(),
|
||||
modoMultimovil: partida.config.modoMultimovil,
|
||||
jugadores: partida.jugadores.length,
|
||||
impostores: partida.impostoresTotales,
|
||||
rondas: partida.rondaActual,
|
||||
ganador: partida.ganador ?? 'sin_resultado',
|
||||
palabra: partida.palabraSecreta,
|
||||
categoria: partida.categoriaReal,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'fecha': fecha.toIso8601String(),
|
||||
'modoMultimovil': modoMultimovil,
|
||||
'jugadores': jugadores,
|
||||
'impostores': impostores,
|
||||
'rondas': rondas,
|
||||
'ganador': ganador,
|
||||
'palabra': palabra,
|
||||
'categoria': categoria,
|
||||
};
|
||||
|
||||
factory ResultadoPartidaGuardado.fromJson(Map<String, dynamic> json) {
|
||||
return ResultadoPartidaGuardado(
|
||||
id: json['id'] as String,
|
||||
fecha: DateTime.parse(json['fecha'] as String),
|
||||
modoMultimovil: json['modoMultimovil'] as bool? ?? false,
|
||||
jugadores: json['jugadores'] as int? ?? 0,
|
||||
impostores: json['impostores'] as int? ?? 0,
|
||||
rondas: json['rondas'] as int? ?? 0,
|
||||
ganador: json['ganador'] as String? ?? 'sin_resultado',
|
||||
palabra: json['palabra'] as String? ?? '',
|
||||
categoria: json['categoria'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ServicioHistorialPartidas extends ChangeNotifier {
|
||||
static const _clave = 'historial.partidas';
|
||||
final List<ResultadoPartidaGuardado> _partidas = [];
|
||||
bool _cargado = false;
|
||||
|
||||
List<ResultadoPartidaGuardado> get partidas => List.unmodifiable(_partidas);
|
||||
bool get cargado => _cargado;
|
||||
|
||||
Future<void> cargar() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getString(_clave);
|
||||
_partidas.clear();
|
||||
if (raw != null) {
|
||||
final lista = json.decode(raw) as List<dynamic>;
|
||||
_partidas.addAll(
|
||||
lista.map((e) => ResultadoPartidaGuardado.fromJson(e as Map<String, dynamic>)),
|
||||
);
|
||||
}
|
||||
_cargado = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> guardarPartida(Partida partida) async {
|
||||
if (partida.ganador == null) return;
|
||||
_partidas.insert(0, ResultadoPartidaGuardado.desdePartida(partida));
|
||||
if (_partidas.length > 100) _partidas.removeRange(100, _partidas.length);
|
||||
await _persistir();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> limpiar() async {
|
||||
_partidas.clear();
|
||||
await _persistir();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> _persistir() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(
|
||||
_clave,
|
||||
json.encode(_partidas.map((p) => p.toJson()).toList()),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -204,7 +204,12 @@ class ServicioNearby extends ChangeNotifier {
|
||||
|
||||
// ==================== HOST ====================
|
||||
|
||||
Future<bool> iniciarHost(String nombreSala, String miNombre) async {
|
||||
Future<bool> iniciarHost(
|
||||
String nombreSala,
|
||||
String miNombre, {
|
||||
String? miNick,
|
||||
String? miAvatar,
|
||||
}) async {
|
||||
_nombreSala = nombreSala;
|
||||
_miNombre = miNombre;
|
||||
_roomId = DateTime.now().microsecondsSinceEpoch.toString();
|
||||
@@ -222,6 +227,9 @@ class ServicioNearby extends ChangeNotifier {
|
||||
final usuarioHost = Usuario(
|
||||
id: 'u-${_roomId!}-host',
|
||||
nombre: miNombre,
|
||||
nick: miNick,
|
||||
avatar: miAvatar,
|
||||
foto: miAvatar,
|
||||
creadoPorClienteId: _hostClientId,
|
||||
clienteIdSeleccionado: _hostClientId,
|
||||
);
|
||||
@@ -654,13 +662,21 @@ class ServicioNearby extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> crearUsuarioSala(String nombre, {bool seleccionar = true}) async {
|
||||
Future<void> crearUsuarioSala(
|
||||
String nombre, {
|
||||
bool seleccionar = true,
|
||||
String? nick,
|
||||
String? avatar,
|
||||
}) async {
|
||||
final nombreLimpio = nombre.trim();
|
||||
if (nombreLimpio.isEmpty) return;
|
||||
final clientId = _miClientId;
|
||||
final usuario = Usuario(
|
||||
id: 'u-${DateTime.now().microsecondsSinceEpoch}',
|
||||
nombre: nombreLimpio,
|
||||
nick: nick,
|
||||
avatar: avatar,
|
||||
foto: avatar,
|
||||
creadoPorClienteId: clientId,
|
||||
);
|
||||
if (_esHost && _estadoSala != null && clientId != null) {
|
||||
|
||||
104
lib/servicios/servicio_perfil_usuario.dart
Normal file
@@ -0,0 +1,104 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class PerfilUsuario {
|
||||
final String nombre;
|
||||
final String nick;
|
||||
final String avatarAsset;
|
||||
|
||||
const PerfilUsuario({
|
||||
required this.nombre,
|
||||
required this.nick,
|
||||
required this.avatarAsset,
|
||||
});
|
||||
|
||||
PerfilUsuario copiar({String? nombre, String? nick, String? avatarAsset}) {
|
||||
return PerfilUsuario(
|
||||
nombre: nombre ?? this.nombre,
|
||||
nick: nick ?? this.nick,
|
||||
avatarAsset: avatarAsset ?? this.avatarAsset,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ServicioPerfilUsuario extends ChangeNotifier {
|
||||
static const _claveNombre = 'perfil.nombre';
|
||||
static const _claveNick = 'perfil.nick';
|
||||
static const _claveAvatar = 'perfil.avatar';
|
||||
|
||||
static const avatares = [
|
||||
'assets/avatars/avatar_01.png',
|
||||
'assets/avatars/avatar_02.png',
|
||||
'assets/avatars/avatar_03.png',
|
||||
'assets/avatars/avatar_04.png',
|
||||
'assets/avatars/avatar_05.png',
|
||||
'assets/avatars/avatar_06.png',
|
||||
'assets/avatars/avatar_07.png',
|
||||
'assets/avatars/avatar_08.png',
|
||||
'assets/avatars/avatar_09.png',
|
||||
'assets/avatars/avatar_10.png',
|
||||
'assets/avatars/avatar_11.png',
|
||||
'assets/avatars/avatar_12.png',
|
||||
'assets/avatars/avatar_13.png',
|
||||
'assets/avatars/avatar_14.png',
|
||||
'assets/avatars/avatar_15.png',
|
||||
'assets/avatars/avatar_16.png',
|
||||
'assets/avatars/avatar_17.png',
|
||||
'assets/avatars/avatar_18.png',
|
||||
'assets/avatars/avatar_19.png',
|
||||
'assets/avatars/avatar_20.png',
|
||||
'assets/avatars/avatar_21.png',
|
||||
'assets/avatars/avatar_22.png',
|
||||
'assets/avatars/avatar_23.png',
|
||||
'assets/avatars/avatar_24.png',
|
||||
'assets/avatars/avatar_25.png',
|
||||
'assets/avatars/avatar_26.png',
|
||||
'assets/avatars/avatar_27.png',
|
||||
'assets/avatars/avatar_28.png',
|
||||
'assets/avatars/avatar_29.png',
|
||||
'assets/avatars/avatar_30.png',
|
||||
];
|
||||
|
||||
PerfilUsuario _perfil = const PerfilUsuario(
|
||||
nombre: 'Jugador',
|
||||
nick: 'farolero',
|
||||
avatarAsset: 'assets/avatars/avatar_01.png',
|
||||
);
|
||||
bool _cargado = false;
|
||||
|
||||
PerfilUsuario get perfil => _perfil;
|
||||
bool get cargado => _cargado;
|
||||
|
||||
Future<void> cargar() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_perfil = PerfilUsuario(
|
||||
nombre: prefs.getString(_claveNombre) ?? _perfil.nombre,
|
||||
nick: prefs.getString(_claveNick) ?? _perfil.nick,
|
||||
avatarAsset: prefs.getString(_claveAvatar) ?? _perfil.avatarAsset,
|
||||
);
|
||||
_cargado = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> guardar({
|
||||
required String nombre,
|
||||
required String nick,
|
||||
required String avatarAsset,
|
||||
}) async {
|
||||
final nombreLimpio = nombre.trim().isEmpty ? 'Jugador' : nombre.trim();
|
||||
final nickLimpio = nick.trim().isEmpty ? 'farolero' : nick.trim();
|
||||
final avatarSeguro = avatares.contains(avatarAsset)
|
||||
? avatarAsset
|
||||
: avatares.first;
|
||||
_perfil = PerfilUsuario(
|
||||
nombre: nombreLimpio,
|
||||
nick: nickLimpio,
|
||||
avatarAsset: avatarSeguro,
|
||||
);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_claveNombre, _perfil.nombre);
|
||||
await prefs.setString(_claveNick, _perfil.nick);
|
||||
await prefs.setString(_claveAvatar, _perfil.avatarAsset);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -269,12 +269,14 @@ class TarjetaPalabraFarolero extends StatelessWidget {
|
||||
|
||||
class AvatarFarolero extends StatelessWidget {
|
||||
final String texto;
|
||||
final String? assetPath;
|
||||
final Color color;
|
||||
final double size;
|
||||
|
||||
const AvatarFarolero({
|
||||
super.key,
|
||||
required this.texto,
|
||||
this.assetPath,
|
||||
this.color = TemaApp.colorNaranja,
|
||||
this.size = 40,
|
||||
});
|
||||
@@ -292,15 +294,24 @@ class AvatarFarolero extends StatelessWidget {
|
||||
border: Border.all(color: TemaApp.colorDorado, width: 2),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
texto,
|
||||
style: TextStyle(
|
||||
color: TemaApp.colorTexto,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: size * 0.36,
|
||||
child: assetPath == null
|
||||
? Text(
|
||||
texto,
|
||||
style: TextStyle(
|
||||
color: TemaApp.colorTexto,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: size * 0.36,
|
||||
),
|
||||
)
|
||||
: ClipOval(
|
||||
child: Image.asset(
|
||||
assetPath!,
|
||||
width: size,
|
||||
height: size,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,5 +30,7 @@ flutter:
|
||||
uses-material-design: true
|
||||
assets:
|
||||
- assets/palabras.json
|
||||
- assets/palabras_i18n.json
|
||||
- assets/palabras_en.json
|
||||
- assets/palabras_fr.json
|
||||
- assets/avatars/
|
||||
|
||||