Ahora se guarda la dificultad en las preferencias

parent 85fe25b0
import 'package:flutter/material.dart';
import 'package:peponator/modelo/dificultad.dart';
import 'package:shared_preferences/shared_preferences.dart';
// TODO: CARGAR LAS DIFICULTADES CARGADAS EN MEMORIA
class ListaDificultad extends ChangeNotifier {
......@@ -8,13 +9,30 @@ class ListaDificultad extends ChangeNotifier {
Dificultad.normal,
Dificultad.dificil
];
int _indiceSeleccionado = 0;
bool _cargando = true;
ListaDificultad() {
_getDificultadPreferencias();
}
List<Dificultad> get dificultades => List.unmodifiable(_dificultades);
int get length => _dificultades.length;
int get indiceSeleccionado => _indiceSeleccionado;
Dificultad get seleccionada => _dificultades[_indiceSeleccionado];
bool get cargando => _cargando;
bool get listo => !_cargando;
void _getDificultadPreferencias() async {
final prefs = await SharedPreferences.getInstance();
_indiceSeleccionado = prefs.getInt("dificultad") ?? 0;
_cargando = false;
notifyListeners();
}
Dificultad get(int index) {
return _dificultades[index];
}
......@@ -34,8 +52,11 @@ class ListaDificultad extends ChangeNotifier {
notifyListeners();
}
void select(int indice) {
void select(int indice) async {
_indiceSeleccionado = indice;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
prefs.setInt("dificultad", _indiceSeleccionado);
}
}
\ No newline at end of file
......@@ -10,13 +10,16 @@ class PantallaDificultad extends StatelessWidget {
return Consumer<ListaDificultad>(
builder: (context, manager, child) {
return SafeArea(
child: ListView.separated(
itemBuilder: (context, index) {
return DificultadWidget(indice: index,);
},
separatorBuilder: (context, index) => const SizedBox(height: 8),
itemCount: manager.length)
);
child: manager.listo
? ListView.separated(
itemBuilder: (context, index) {
return DificultadWidget(indice: index,);
},
separatorBuilder: (context, index) => const SizedBox(height: 8),
itemCount: manager.length
)
: CircularProgressIndicator()
);
}
);
}
......
......@@ -10,7 +10,6 @@ import 'package:peponator/modelo/pista.dart';
import 'package:peponator/paginas/paginas.dart';
import 'package:peponator/widgets/pantalla_pausa.dart';
import 'package:peponator/widgets/peponator_mensaje.dart';
import 'dart:math';
import 'package:peponator/widgets/teclado_numerico.dart';
import 'package:peponator/widgets/tu_mensaje.dart';
......@@ -37,6 +36,7 @@ class _PantallaJuegoState extends State<PantallaJuego>
double animatedValue = 0.0;
bool _updateMaximum = false;
late Dificultad dificultad;
late int limiteInferior;
late int limiteSuperior;
......@@ -50,8 +50,8 @@ class _PantallaJuegoState extends State<PantallaJuego>
late bool manoDerecha = true;
List<Pista> pistas = [];
late bool mostrarPistas;
late bool todasPistasBien;
bool mostrarPistas = false;
late bool todasPistasBien ;
late bool porDesbloquear;
late bool algunaDesbloqueada;
......@@ -71,9 +71,6 @@ class _PantallaJuegoState extends State<PantallaJuego>
super.initState();
_loadRecords();
ListaDificultad dificultades = context.read<ListaDificultad>();
dificultad = dificultades.seleccionada;
recordTextController.addListener(() =>
setState(() {
jugador = recordTextController.text;
......@@ -89,57 +86,54 @@ class _PantallaJuegoState extends State<PantallaJuego>
animatedValue = _controller.value;
});
});
_nuevaPartida();
}
void _nuevaPartida(){
setState(() {
textController.clear();
mensajes.clear();
pistas.clear();
limiteInferior = 1;
limiteSuperior = dificultad.limite;
numeroAdivinar = dificultad.generarNumero();
maxIntentos = dificultad.maxIntentos;
intentos = 0;
for(int i = 0; i < ClasePista.values.length && i < ((maxIntentos-1)/5).floor(); i++){
pistas.add(Pista(
clase: ClasePista.values[i],
numero: numeroAdivinar,
onPressed: () {
if(pistas[i].estado == EstadoPista.disponible){
setState(() {
pistas[i].estado = EstadoPista.confirmacion;
});
}
else if(pistas[i].estado == EstadoPista.confirmacion) {
setState(() {
pistas[i].estado = EstadoPista.desbloqueado;
});
_actualizarPistas();
}
},
));
}
textController.clear();
mensajes.clear();
pistas.clear();
limiteInferior = 1;
limiteSuperior = dificultad.limite;
numeroAdivinar = dificultad.generarNumero();
maxIntentos = dificultad.maxIntentos;
intentos = 0;
for(int i = 0; i < ClasePista.values.length && i < ((maxIntentos-1)/5).floor(); i++){
pistas.add(Pista(
clase: ClasePista.values[i],
numero: numeroAdivinar,
onPressed: () {
if(pistas[i].estado == EstadoPista.disponible){
setState(() {
pistas[i].estado = EstadoPista.confirmacion;
});
}
else if(pistas[i].estado == EstadoPista.confirmacion) {
setState(() {
pistas[i].estado = EstadoPista.desbloqueado;
});
_actualizarPistas();
}
},
));
}
mensajes.add(PeponatorMensaje(message: "¡Hola! Estoy pensando en un número del $limiteInferior al $limiteSuperior. ¿Te crees capaz de adivinarlo?"));
mensajes.add(PeponatorMensaje(message: "¡Hola! Estoy pensando en un número "
"del $limiteInferior al $limiteSuperior. ¿Te crees capaz de adivinarlo?"));
mostrarPistas = false;
todasPistasBien = false;
porDesbloquear = false;
algunaDesbloqueada = false;
mostrarPistas = false;
todasPistasBien = false;
porDesbloquear = false;
algunaDesbloqueada = false;
error = false;
espera = false;
victoria = false;
error = false;
espera = false;
victoria = false;
puntuacion = 0;
posicion = PantallaRecords.maxRecords;
jugador = "";
});
puntuacion = 0;
posicion = PantallaRecords.maxRecords;
jugador = "";
}
void _cambiarDificultadPausa() async {
......@@ -166,113 +160,121 @@ class _PantallaJuegoState extends State<PantallaJuego>
super.dispose();
}
@override
Widget build(BuildContext context) {
return OrientationBuilder(
builder: (context, orientation) {
return Scaffold(
backgroundColor: (mostrarPistas)? Theme.of(context).colorScheme.surfaceDim : null,
body: SafeArea(
child: Stack(
children: [
Positioned(
top: 0,
right: (orientation == Orientation.portrait || !manoDerecha)? 0 : null,
left: (orientation == Orientation.portrait || !manoDerecha)? null : 0,
child: SizedBox(
width: (orientation == Orientation.portrait)?
MediaQuery.of(context).size.width - MediaQuery.of(context).padding.horizontal :
MediaQuery.of(context).size.width - MediaQuery.of(context).padding.horizontal - 420,
height: (orientation == Orientation.portrait)?
MediaQuery.of(context).size.height - 420 :
MediaQuery.of(context).size.height - MediaQuery.of(context).padding.vertical,
child: (mostrarPistas)? _buildVistaPistas(orientation) : _buildVistaMensajes()
)
),
if(!(victoria || intentos >= maxIntentos)) Positioned(
top: 0,
right: (orientation == Orientation.portrait || !manoDerecha)? 10 : null,
left: (orientation == Orientation.portrait || !manoDerecha)? null : 10,
child: ElevatedButton(
onPressed: () async {
switch(await showDialog<OpcionPausa>(context: context,
builder: (context) {
return PantallaPausa(
orientacion: orientation,
manoDerecha: manoDerecha,
);
}
)) {
case null:
case OpcionPausa.reanudar:
break;
case OpcionPausa.cambioMano:
setState(() {
manoDerecha = !manoDerecha;
});
break;
case OpcionPausa.nuevaPartida:
_nuevaPartida();
break;
case OpcionPausa.cambiarDificultad:
_cambiarDificultadPausa();
break;
case OpcionPausa.salir:
// TODO: Handle this case.
break;
}
},
style: ButtonStyle(
Widget _buildPaginaJuego(BuildContext context, Orientation orientation) {
dificultad = context.read<ListaDificultad>().seleccionada;
_nuevaPartida();
return SafeArea(
child: Stack(
children: [
Positioned(
top: 0,
right: (orientation == Orientation.portrait || !manoDerecha)? 0 : null,
left: (orientation == Orientation.portrait || !manoDerecha)? null : 0,
child: SizedBox(
width: (orientation == Orientation.portrait)?
MediaQuery.of(context).size.width - MediaQuery.of(context).padding.horizontal :
MediaQuery.of(context).size.width - MediaQuery.of(context).padding.horizontal - 420,
height: (orientation == Orientation.portrait)?
MediaQuery.of(context).size.height - 420 :
MediaQuery.of(context).size.height - MediaQuery.of(context).padding.vertical,
child: (mostrarPistas)? _buildVistaPistas(orientation) : _buildVistaMensajes()
)
),
if(!(victoria || intentos >= maxIntentos)) Positioned(
top: 0,
right: (orientation == Orientation.portrait || !manoDerecha)? 10 : null,
left: (orientation == Orientation.portrait || !manoDerecha)? null : 10,
child: ElevatedButton(
onPressed: () async {
switch(await showDialog<OpcionPausa>(context: context,
builder: (context) {
return PantallaPausa(
orientacion: orientation,
manoDerecha: manoDerecha,
);
}
)) {
case null:
case OpcionPausa.reanudar:
break;
case OpcionPausa.cambioMano:
setState(() {
manoDerecha = !manoDerecha;
});
break;
case OpcionPausa.nuevaPartida:
setState(() {_nuevaPartida();});
break;
case OpcionPausa.cambiarDificultad:
_cambiarDificultadPausa();
break;
case OpcionPausa.salir:
// TODO: Handle this case.
break;
}
},
style: ButtonStyle(
elevation: WidgetStatePropertyAll<double>(5.0),
shape: WidgetStatePropertyAll<OutlinedBorder>(CircleBorder()),
padding: WidgetStatePropertyAll<EdgeInsets>(
EdgeInsets.all(16.0)
EdgeInsets.all(16.0)
),
side: WidgetStatePropertyAll<BorderSide>(
BorderSide(
width: 2.0,
color: (Theme.of(context).brightness == Brightness.light)?
BorderSide(
width: 2.0,
color: (Theme.of(context).brightness == Brightness.light)?
Colors.black12 :
Colors.grey.shade400,
)
)
)
),
child: Icon(
Icons.pause,
size: 28.0,
)
)
),
Positioned(
bottom: 0,
left: (manoDerecha)? null : 0,
right: (manoDerecha)? 0 : null,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
colors: <Color>[
Theme.of(context).colorScheme.surface,
Theme.of(context).colorScheme.surface.withAlpha(0)
],
stops: <double>[
(orientation == Orientation.portrait)? 0.97 : 1.0,
1.0
]
)
),
child: (victoria || intentos >= maxIntentos)? _buildPantallaFinal(orientation) :_buildTeclado(orientation)
),
child: Icon(
Icons.pause,
size: 28.0,
)
)
],
)
),
);
}
)
),
Positioned(
bottom: 0,
left: (manoDerecha)? null : 0,
right: (manoDerecha)? 0 : null,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
colors: <Color>[
Theme.of(context).colorScheme.surface,
Theme.of(context).colorScheme.surface.withAlpha(0)
],
stops: <double>[
(orientation == Orientation.portrait)? 0.97 : 1.0,
1.0
]
)
),
child: (victoria || intentos >= maxIntentos)? _buildPantallaFinal(orientation) :_buildTeclado(orientation)
)
)
],
)
);
}
@override
Widget build(BuildContext context) {
return Consumer<ListaDificultad>(builder: (context, manager, child) {
return OrientationBuilder(builder: (context, orientation) {
return Scaffold(
backgroundColor: (mostrarPistas)? Theme.of(context).colorScheme.surfaceDim : null,
body: manager.listo
? _buildPaginaJuego(context, orientation)
: CircularProgressIndicator()
);
});
});
}
void _scrollDown() {
scrollController.animateTo(
scrollController.position.maxScrollExtent,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment