refactor(presentation): guess page consumes gameProvider

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Maxiwere45 2026-06-09 11:45:20 +02:00
parent 4ac13bd233
commit 2c21b80a03

View File

@ -1,236 +1,109 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:math'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart'; import '../../domain/game/game_state.dart';
import '../models/pokemon.dart'; import '../providers/game_provider.dart';
import '../database/pokedex_database.dart'; import '../providers/navigation_provider.dart';
import 'main_page.dart'; import '../widgets/pokemon_image.dart';
import '../components/pokemon_image.dart';
class GuessPage extends StatefulWidget { class GuessPage extends ConsumerStatefulWidget {
const GuessPage({Key? key}) : super(key: key); const GuessPage({Key? key}) : super(key: key);
@override @override
State<GuessPage> createState() => _GuessPageState(); ConsumerState<GuessPage> createState() => _GuessPageState();
} }
class _GuessPageState extends State<GuessPage> { class _GuessPageState extends ConsumerState<GuessPage> {
Pokemon? _currentPokemon;
final TextEditingController _guessController = TextEditingController(); final TextEditingController _guessController = TextEditingController();
int _lives = 3; bool _started = false;
int _skips = 3;
int _hints = 3;
int _sessionCorrectCount = 0;
bool _isGuessed = false;
bool _isLoading = true;
bool _isHintUsed = false;
bool _isShiny = false;
int _currentScore = 0;
int _bestScore = 0;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_loadBestScore(); // Démarre la partie après le premier frame (le provider est prêt).
_startNewGame(); WidgetsBinding.instance.addPostFrameCallback((_) {
} if (!_started) {
_started = true;
void _startNewGame() { ref.read(gameProvider.notifier).startNewGame();
setState(() { }
_lives = 3;
_skips = 3;
_hints = 3;
_sessionCorrectCount = 0;
_currentScore = 0;
});
_loadRandomPokemon();
}
Future<void> _loadBestScore() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_bestScore = prefs.getInt('best_score') ?? 0;
}); });
} }
Future<void> _saveBestScore() async { @override
if (_currentScore > _bestScore) { void dispose() {
final prefs = await SharedPreferences.getInstance(); _guessController.dispose();
await prefs.setInt('best_score', _currentScore); super.dispose();
setState(() {
_bestScore = _currentScore;
});
}
} }
Future<void> _loadRandomPokemon() async { Future<void> _onGuess() async {
setState(() { final result = await ref.read(gameProvider.notifier).submitGuess(_guessController.text);
_isLoading = true; if (!mounted) return;
_isGuessed = false; final state = ref.read(gameProvider);
_isHintUsed = false;
_isShiny = Random().nextInt(10) == 0; // 10% chance for shiny
_guessController.clear();
});
try { switch (result) {
// Pick a random ID between 1 and 1025 (Gen 9) case GuessResult.correct:
int randomId = Random().nextInt(1025) + 1; ScaffoldMessenger.of(context).showSnackBar(SnackBar(
Pokemon? pokemon = await Pokemon.fromID(randomId); content: Text(state.isShiny
? '✨ SHINY! You caught ${state.currentPokemon!.formatedName}! (+20 pts) ✨'
// We only want to guess uncaught ones for optimal experience, : 'Correct! You caught ${state.currentPokemon!.formatedName}!'),
// but if all are caught, just play anyway. backgroundColor: state.isShiny ? Colors.amber[800] : Colors.green,
if (pokemon != null && pokemon.isCaught) { ));
int count = await PokedexDatabase.getCaughtCount(); break;
if (count < 1025) { case GuessResult.wrong:
// Find an uncaught one ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
for (int i = 1; i <= 1025; i++) { content: Text('Wrong guess! Try again.'), backgroundColor: Colors.orange));
int attemptId = (randomId + i) % 1025 + 1; break;
Pokemon? attempt = await Pokemon.fromID(attemptId); case GuessResult.gameOver:
if (attempt != null && !attempt.isCaught) { await _showGameOver();
pokemon = attempt; break;
break; case GuessResult.invalid:
} break;
}
}
}
if (mounted) {
setState(() {
_currentPokemon = pokemon;
_isLoading = false;
});
}
} catch (e) {
debugPrint(e.toString());
if (mounted) {
setState(() {
_isLoading = false;
});
}
} }
_guessController.clear();
} }
void _checkGuess() async { Future<void> _showGameOver() async {
if (_currentPokemon == null) return; final state = ref.read(gameProvider);
String guess = _guessController.text.trim().toLowerCase(); final playAgain = await Navigator.pushNamed(
String actual = _currentPokemon!.name.toLowerCase(); context,
'/game-over',
arguments: {
'pokemonName': state.currentPokemon!.formatedName,
'score': state.currentScore,
'streak': state.sessionCorrectCount,
'pokemonImage': state.currentPokemon!.imageUrl,
},
) as bool?;
// Normalize both for accent-insensitive comparison if (!mounted) return;
String normalizedGuess = _normalizeString(guess); if (playAgain == true) {
String normalizedActual = _normalizeString(actual); await ref.read(gameProvider.notifier).startNewGame();
} else if (playAgain == false) {
if (normalizedGuess == normalizedActual || normalizedGuess == 'pikachu') { ref.read(selectedTabProvider.notifier).set(0); // onglet LIST
// Correct! await ref.read(gameProvider.notifier).startNewGame();
_currentPokemon!.isCaught = true;
_currentPokemon!.isSeen = true;
await PokedexDatabase.updatePokemon(_currentPokemon!);
if (mounted) {
setState(() {
_currentScore += _isShiny ? 20 : 10;
_isGuessed = true;
_sessionCorrectCount++;
if (_sessionCorrectCount % 5 == 0) _hints++;
if (_sessionCorrectCount % 10 == 0) _skips++;
});
}
await _saveBestScore();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(_isShiny
? '✨ SHINY! You caught ${_currentPokemon!.formatedName}! (+20 pts) ✨'
: 'Correct! You caught ${_currentPokemon!.formatedName}!'),
backgroundColor: _isShiny ? Colors.amber[800] : Colors.green
),
);
// Wait for user to click Continue
} else {
// Wrong
if (mounted) {
setState(() {
_lives--;
});
}
if (_lives <= 0) {
if (!mounted) return;
final bool? playAgain = await Navigator.pushNamed(
context,
'/game-over',
arguments: {
'pokemonName': _currentPokemon!.formatedName,
'score': _currentScore,
'streak': _sessionCorrectCount,
'pokemonImage': _currentPokemon!.imageUrl,
},
) as bool?;
if (playAgain == true) {
_startNewGame();
} else if (playAgain == false) {
// Switch to Pokedex List tab
if (mounted) {
final mainState = context.findAncestorStateOfType<MainPageState>();
mainState?.setIndex(0); // Index 0 is Pokemon List
_startNewGame(); // Reset game state for next time
}
}
} else {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Wrong guess! Try again.'), backgroundColor: Colors.orange),
);
}
} }
} }
void _useHint() {
if (_currentPokemon == null || _isHintUsed || _hints <= 0) return;
setState(() {
_isHintUsed = true;
_hints--;
});
}
void _useSkip() {
if (_skips > 0) {
setState(() {
_skips--;
});
_loadRandomPokemon();
}
}
String _normalizeString(String input) {
var withDia = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ';
var withoutDia = 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn';
String output = input;
for (int i = 0; i < withDia.length; i++) {
output = output.replaceAll(withDia[i], withoutDia[i]);
}
return output;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_isLoading) { final state = ref.watch(gameProvider);
if (state.status == GameStatus.loading) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} }
if (_currentPokemon == null) { if (state.currentPokemon == null || state.status == GameStatus.error) {
return const Center(child: Text("Error loading Pokémon")); return const Center(child: Text("Error loading Pokémon"));
} }
final pokemon = state.currentPokemon!;
final isGuessed = state.status == GameStatus.roundWon;
return Container( return Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(color: Color(0xFFC8D1D8)),
color: Color(0xFFC8D1D8), // Silver-ish grey background with scanlines simulated
),
child: Stack( child: Stack(
children: [ children: [
Positioned.fill( Positioned.fill(
child: ListView.builder( child: ListView.builder(
itemCount: 100, // drawing artificial scanlines itemCount: 100,
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => Container( itemBuilder: (context, index) => Container(
height: 4, height: 4,
@ -242,7 +115,7 @@ class _GuessPageState extends State<GuessPage> {
SingleChildScrollView( SingleChildScrollView(
child: Column( child: Column(
children: [ children: [
// Screen top showing the silhouette // Silhouette screen
Container( Container(
height: 250, height: 250,
width: double.infinity, width: double.infinity,
@ -258,19 +131,19 @@ class _GuessPageState extends State<GuessPage> {
Expanded( Expanded(
child: Padding( child: Padding(
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
child: _isGuessed child: isGuessed
? PokemonImage( ? PokemonImage(
imageUrl: _isShiny ? _currentPokemon!.shinyImageUrl : _currentPokemon!.imageUrl, imageUrl: state.isShiny ? pokemon.shinyImageUrl : pokemon.imageUrl,
fallbackUrl: _currentPokemon!.imageUrl, fallbackUrl: pokemon.imageUrl,
fit: BoxFit.contain, fit: BoxFit.contain,
) )
: PokemonImage( : PokemonImage(
imageUrl: _isShiny ? _currentPokemon!.shinyImageUrl : _currentPokemon!.imageUrl, imageUrl: state.isShiny ? pokemon.shinyImageUrl : pokemon.imageUrl,
fallbackUrl: _currentPokemon!.imageUrl, fallbackUrl: pokemon.imageUrl,
fit: BoxFit.contain, fit: BoxFit.contain,
color: _isShiny ? Colors.yellow[700]! : Colors.black, color: state.isShiny ? Colors.yellow[700]! : Colors.black,
colorBlendMode: BlendMode.srcIn, colorBlendMode: BlendMode.srcIn,
), ),
), ),
), ),
Container( Container(
@ -278,11 +151,11 @@ class _GuessPageState extends State<GuessPage> {
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.symmetric(vertical: 8), padding: const EdgeInsets.symmetric(vertical: 8),
child: Text( child: Text(
_isShiny ? "✨ SHINY POKÉMON DETECTED! ✨" : "WHO'S THAT POKÉMON?", state.isShiny ? "✨ SHINY POKÉMON DETECTED! ✨" : "WHO'S THAT POKÉMON?",
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: TextStyle( style: TextStyle(
color: _isShiny ? Colors.yellow[400] : Colors.white, color: state.isShiny ? Colors.yellow[400] : Colors.white,
fontSize: _isShiny ? 18 : 22, fontSize: state.isShiny ? 18 : 22,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
letterSpacing: 2, letterSpacing: 2,
), ),
@ -291,32 +164,28 @@ class _GuessPageState extends State<GuessPage> {
], ],
), ),
), ),
// Lives
// Lives display
Row( Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(3, (index) { children: List.generate(3, (index) {
return Icon( return Icon(
index < _lives ? Icons.favorite : Icons.favorite_border, index < state.lives ? Icons.favorite : Icons.favorite_border,
color: Colors.red, color: Colors.red,
size: 32, size: 32,
); );
}), }),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
// Guess section
// Guess Section
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0), padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const Text( const Text("IDENTIFICATION INPUT",
"IDENTIFICATION INPUT", style: TextStyle(color: Colors.black54, fontSize: 12, fontWeight: FontWeight.bold)),
style: TextStyle(color: Colors.black54, fontSize: 12, fontWeight: FontWeight.bold),
),
const SizedBox(height: 4), const SizedBox(height: 4),
if (_isHintUsed && _currentPokemon != null) if (state.isHintUsed)
Container( Container(
width: double.infinity, width: double.infinity,
margin: const EdgeInsets.only(bottom: 12), margin: const EdgeInsets.only(bottom: 12),
@ -327,7 +196,7 @@ class _GuessPageState extends State<GuessPage> {
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
), ),
child: Text( child: Text(
"HINT: ${_currentPokemon!.formatedName[0]}${List.filled(_currentPokemon!.formatedName.length - 2, '_').join()}${_currentPokemon!.formatedName[_currentPokemon!.formatedName.length - 1]}", "HINT: ${pokemon.formatedName[0]}${List.filled(pokemon.formatedName.length - 2, '_').join()}${pokemon.formatedName[pokemon.formatedName.length - 1]}",
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold, color: Colors.amber[900], letterSpacing: 4), style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold, color: Colors.amber[900], letterSpacing: 4),
), ),
@ -345,24 +214,22 @@ class _GuessPageState extends State<GuessPage> {
border: InputBorder.none, border: InputBorder.none,
hintText: 'Enter Pokémon name...', hintText: 'Enter Pokémon name...',
), ),
onSubmitted: (_) => _checkGuess(), onSubmitted: (_) => _onGuess(),
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
if (_isGuessed) if (isGuessed)
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
height: 60, height: 60,
child: ElevatedButton( child: ElevatedButton(
onPressed: _loadRandomPokemon, onPressed: () => ref.read(gameProvider.notifier).loadNextPokemon(),
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: Colors.green, backgroundColor: Colors.green,
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero), shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
), ),
child: const Text( child: const Text("CONTINUE",
"CONTINUE", style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2)),
style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2),
),
), ),
) )
else ...[ else ...[
@ -370,15 +237,13 @@ class _GuessPageState extends State<GuessPage> {
width: double.infinity, width: double.infinity,
height: 60, height: 60,
child: ElevatedButton( child: ElevatedButton(
onPressed: _checkGuess, onPressed: _onGuess,
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF3B6EE3), backgroundColor: const Color(0xFF3B6EE3),
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero), shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
), ),
child: const Text( child: const Text("GUESS!",
"GUESS!", style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2)),
style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2),
),
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
@ -386,9 +251,12 @@ class _GuessPageState extends State<GuessPage> {
children: [ children: [
Expanded( Expanded(
child: ElevatedButton.icon( child: ElevatedButton.icon(
onPressed: (_isHintUsed || _hints <= 0) ? null : _useHint, onPressed: (state.isHintUsed || state.hints <= 0)
? null
: () => ref.read(gameProvider.notifier).useHint(),
icon: const Icon(Icons.lightbulb, color: Colors.black87), icon: const Icon(Icons.lightbulb, color: Colors.black87),
label: Text("HINT ($_hints)", style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)), label: Text("HINT (${state.hints})",
style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)),
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: Colors.amber, backgroundColor: Colors.amber,
padding: const EdgeInsets.symmetric(vertical: 16), padding: const EdgeInsets.symmetric(vertical: 16),
@ -399,9 +267,12 @@ class _GuessPageState extends State<GuessPage> {
const SizedBox(width: 8), const SizedBox(width: 8),
Expanded( Expanded(
child: ElevatedButton.icon( child: ElevatedButton.icon(
onPressed: _skips > 0 ? _useSkip : null, onPressed: state.skips > 0
? () => ref.read(gameProvider.notifier).useSkip()
: null,
icon: const Icon(Icons.skip_next, color: Colors.black87), icon: const Icon(Icons.skip_next, color: Colors.black87),
label: Text("SKIP ($_skips)", style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)), label: Text("SKIP (${state.skips})",
style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)),
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[400], backgroundColor: Colors.grey[400],
padding: const EdgeInsets.symmetric(vertical: 16), padding: const EdgeInsets.symmetric(vertical: 16),
@ -413,8 +284,7 @@ class _GuessPageState extends State<GuessPage> {
), ),
], ],
const SizedBox(height: 24), const SizedBox(height: 24),
// Score
// Score Display
Container( Container(
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
@ -425,19 +295,13 @@ class _GuessPageState extends State<GuessPage> {
), ),
child: Column( child: Column(
children: [ children: [
const Text( const Text("CURRENT SCORE",
"CURRENT SCORE", style: TextStyle(color: Colors.black54, fontSize: 14, fontWeight: FontWeight.bold)),
style: TextStyle(color: Colors.black54, fontSize: 14, fontWeight: FontWeight.bold), Text("${state.currentScore}",
), style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Color(0xFF3B6EE3))),
Text(
"$_currentScore",
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Color(0xFF3B6EE3)),
),
const Divider(height: 24), const Divider(height: 24),
Text( Text("PERSONAL BEST: ${state.bestScore}",
"PERSONAL BEST: $_bestScore", style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black87)),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black87),
),
], ],
), ),
), ),