171 lines
5.9 KiB
Dart
171 lines
5.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:farolero/l10n/generated/app_localizations.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../estado/estado_juego.dart';
|
|
import '../tema/componentes_farolero.dart';
|
|
import '../tema/tema_app.dart';
|
|
import 'pantalla_resultado.dart';
|
|
|
|
class PantallaVotacion extends StatefulWidget {
|
|
const PantallaVotacion({super.key});
|
|
|
|
@override
|
|
State<PantallaVotacion> createState() => _PantallaVotacionState();
|
|
}
|
|
|
|
class _PantallaVotacionState extends State<PantallaVotacion> {
|
|
String? _seleccionado;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final estado = context.watch<EstadoJuego>();
|
|
final partida = estado.partida;
|
|
if (partida == null) return const SizedBox.shrink();
|
|
|
|
final activos = partida.jugadoresActivos;
|
|
final todosVotaron = estado.todosHanVotado();
|
|
|
|
return _construirVotacionUnMovil(context, estado, partida, activos, todosVotaron);
|
|
}
|
|
|
|
Widget _construirVotacionUnMovil(
|
|
BuildContext context,
|
|
EstadoJuego estado,
|
|
partida,
|
|
List activos,
|
|
bool todosVotaron,
|
|
) {
|
|
final sinVotar = activos.where((j) => !estado.votos.containsKey(j.id)).toList();
|
|
|
|
if (todosVotaron) {
|
|
return _construirTodosVotaron(context, estado);
|
|
}
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
final votanteActual = sinVotar.isNotEmpty ? sinVotar[0] : activos[0];
|
|
final puedenRecibir = activos.where((j) => j.id != votanteActual.id).toList();
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(l10n.voting),
|
|
automaticallyImplyLeading: false,
|
|
),
|
|
body: FondoFarolero(
|
|
intenso: true,
|
|
child: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
children: [
|
|
const ArteGameplayFarolero.fase(height: 108),
|
|
const SizedBox(height: 10),
|
|
TarjetaFaseFarolero(
|
|
icono: Icons.how_to_vote,
|
|
titulo: l10n.voteOf(votanteActual.nombre),
|
|
subtitulo: l10n.votesProgress(estado.votos.length, activos.length),
|
|
color: TemaApp.colorAcento,
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(4),
|
|
child: LinearProgressIndicator(
|
|
value: estado.votos.length / activos.length,
|
|
backgroundColor: TemaApp.colorSuperficie,
|
|
valueColor: const AlwaysStoppedAnimation(TemaApp.colorAcento),
|
|
minHeight: 7,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Expanded(
|
|
child: TarjetaFaseFarolero(
|
|
icono: Icons.person_search,
|
|
titulo: l10n.whoIsImpostor,
|
|
subtitulo: l10n.selectOnePlayer,
|
|
color: TemaApp.colorNaranja,
|
|
child: Expanded(
|
|
child: ListView.separated(
|
|
itemCount: puedenRecibir.length,
|
|
separatorBuilder: (context, index) => const SizedBox(height: 8),
|
|
itemBuilder: (context, index) {
|
|
final candidato = puedenRecibir[index];
|
|
final seleccionado = _seleccionado == candidato.id;
|
|
return SelectorVotoFarolero(
|
|
nombre: '${index + 1}. ${candidato.nombre}',
|
|
seleccionado: seleccionado,
|
|
onTap: () => setState(() => _seleccionado = candidato.id),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
BotonFarolero(
|
|
texto: l10n.confirmVote,
|
|
icono: Icons.how_to_vote,
|
|
onPressed: _seleccionado != null
|
|
? () {
|
|
estado.registrarVoto(votanteActual.id, _seleccionado!);
|
|
setState(() => _seleccionado = null);
|
|
}
|
|
: null,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _construirTodosVotaron(BuildContext context, EstadoJuego estado) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(l10n.votingComplete),
|
|
automaticallyImplyLeading: false,
|
|
),
|
|
body: FondoFarolero(
|
|
intenso: true,
|
|
child: SafeArea(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const ArteGameplayFarolero.fase(height: 132),
|
|
const SizedBox(height: 24),
|
|
TarjetaFaseFarolero(
|
|
icono: Icons.check_circle,
|
|
titulo: l10n.allVoted,
|
|
subtitulo: l10n.tapToReveal,
|
|
color: TemaApp.colorVerde,
|
|
child: const SizedBox.shrink(),
|
|
),
|
|
const SizedBox(height: 28),
|
|
BotonFarolero(
|
|
texto: l10n.revealResult,
|
|
icono: Icons.visibility,
|
|
onPressed: () {
|
|
final resultado = estado.procesarVotacion();
|
|
if (resultado != null) {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => PantallaResultado(resultado: resultado),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|