El Impostor v0.1 — app Flutter completa
Juego de deducción social para 3-20 jugadores. Modo un solo móvil completamente funcional. 1000 palabras en 10 categorías. Notas privadas, votación, adivinanza, revancha. Material 3 dark theme. Package: es.freetimelab.elimpostor
This commit is contained in:
222
lib/pantallas/pantalla_notas.dart
Normal file
222
lib/pantallas/pantalla_notas.dart
Normal file
@@ -0,0 +1,222 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../estado/estado_juego.dart';
|
||||
import '../servicios/servicio_notas.dart';
|
||||
import '../tema/tema_app.dart';
|
||||
|
||||
class PantallaNotas extends StatefulWidget {
|
||||
const PantallaNotas({super.key});
|
||||
|
||||
@override
|
||||
State<PantallaNotas> createState() => _PantallaNotasState();
|
||||
}
|
||||
|
||||
class _PantallaNotasState extends State<PantallaNotas> {
|
||||
String? _jugadorSeleccionadoId;
|
||||
final Map<String, TextEditingController> _controladores = {};
|
||||
final _controladorNotaLibre = TextEditingController();
|
||||
bool _cargado = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final c in _controladores.values) {
|
||||
c.dispose();
|
||||
}
|
||||
_controladorNotaLibre.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _cargarNotas(String jugadorId) async {
|
||||
final datos = await ServicioNotas.cargarNotas(jugadorId);
|
||||
final notas = datos['notas'] as Map<String, String>;
|
||||
final notaLibre = datos['notaLibre'] as String;
|
||||
|
||||
for (final entrada in notas.entries) {
|
||||
if (_controladores.containsKey(entrada.key)) {
|
||||
_controladores[entrada.key]!.text = entrada.value;
|
||||
}
|
||||
}
|
||||
_controladorNotaLibre.text = notaLibre;
|
||||
setState(() => _cargado = true);
|
||||
}
|
||||
|
||||
Future<void> _guardarNotas() async {
|
||||
if (_jugadorSeleccionadoId == null) return;
|
||||
final notas = <String, String>{};
|
||||
for (final entrada in _controladores.entries) {
|
||||
if (entrada.value.text.isNotEmpty) {
|
||||
notas[entrada.key] = entrada.value.text;
|
||||
}
|
||||
}
|
||||
await ServicioNotas.guardarNotas(
|
||||
_jugadorSeleccionadoId!,
|
||||
notas,
|
||||
_controladorNotaLibre.text,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final estado = context.watch<EstadoJuego>();
|
||||
final partida = estado.partida;
|
||||
if (partida == null) return const SizedBox.shrink();
|
||||
|
||||
// Inicializar controladores
|
||||
for (final j in partida.jugadores) {
|
||||
_controladores.putIfAbsent(j.id, () => TextEditingController());
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('📝 Notas'),
|
||||
actions: [
|
||||
if (_jugadorSeleccionadoId != null)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.save),
|
||||
onPressed: () async {
|
||||
await _guardarNotas();
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Notas guardadas')),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _jugadorSeleccionadoId == null
|
||||
? _construirSelectorJugador(partida)
|
||||
: _construirNotas(partida),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _construirSelectorJugador(dynamic partida) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'¿Quién eres?',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Selecciona tu nombre para ver tus notas privadas',
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: partida.jugadoresActivos.length,
|
||||
itemBuilder: (context, index) {
|
||||
final j = partida.jugadoresActivos[index];
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: TemaApp.colorAcento,
|
||||
child: Text('${index + 1}',
|
||||
style: const TextStyle(color: Colors.white)),
|
||||
),
|
||||
title: Text(j.nombre),
|
||||
trailing: const Icon(Icons.arrow_forward_ios, size: 16),
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_jugadorSeleccionadoId = j.id;
|
||||
_cargado = false;
|
||||
});
|
||||
_cargarNotas(j.id);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _construirNotas(dynamic partida) {
|
||||
if (!_cargado) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final jugadorActual = partida.jugadores
|
||||
.firstWhere((j) => j.id == _jugadorSeleccionadoId);
|
||||
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () async {
|
||||
await _guardarNotas();
|
||||
setState(() {
|
||||
_jugadorSeleccionadoId = null;
|
||||
_cargado = false;
|
||||
for (final c in _controladores.values) {
|
||||
c.clear();
|
||||
}
|
||||
_controladorNotaLibre.clear();
|
||||
});
|
||||
},
|
||||
),
|
||||
Text(
|
||||
'Notas de ${jugadorActual.nombre}',
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Notas por jugador
|
||||
Text(
|
||||
'Apuntes sobre cada jugador',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
color: TemaApp.colorTextoSecundario,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...partida.jugadoresActivos.where((j) => j.id != _jugadorSeleccionadoId).map<Widget>((j) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: TextField(
|
||||
controller: _controladores[j.id],
|
||||
decoration: InputDecoration(
|
||||
labelText: j.nombre,
|
||||
prefixIcon: const Icon(Icons.person, size: 20),
|
||||
hintText: '¿Qué ha dicho? ¿Sospechoso?',
|
||||
),
|
||||
maxLines: 2,
|
||||
minLines: 1,
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Nota libre',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
color: TemaApp.colorTextoSecundario,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _controladorNotaLibre,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Apuntes personales...',
|
||||
prefixIcon: Icon(Icons.note, size: 20),
|
||||
),
|
||||
maxLines: 5,
|
||||
minLines: 3,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user