diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 2f2ba76..04e4adf 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -38,5 +38,13 @@ "play": "Play", "@play": { "description": "Play button label" + }, + "victory_title": "You win !", + "@victory_title": { + "description": "Victory title" + }, + "restart_btn": "Restart", + "@restart_btn": { + "description": "Restart button label" } } \ No newline at end of file diff --git a/lib/l10n/app_fr.arb b/lib/l10n/app_fr.arb index 7d854c1..cb6f7de 100644 --- a/lib/l10n/app_fr.arb +++ b/lib/l10n/app_fr.arb @@ -5,5 +5,7 @@ "solve": "Résoudre", "team_name": "Xpeho mobile", "moves": "Mouvements {count}", - "play": "Jouer" + "play": "Jouer", + "victory_title": "Victoire !", + "restart_btn": "Recommencer" } \ No newline at end of file diff --git a/lib/models/puzzle.dart b/lib/models/puzzle.dart index 344440f..7333251 100644 --- a/lib/models/puzzle.dart +++ b/lib/models/puzzle.dart @@ -40,6 +40,8 @@ class Puzzle { .map((tile) => tile.currentError(getXof(tile), getYof(tile))) .reduce((a, b) => a + b); + bool get isSolved => error == 0; + /// perform a swap between two tiles Puzzle move(int value) { final newData = List.from(data); diff --git a/lib/view/puzzle_page/puzzle_page.dart b/lib/view/puzzle_page/puzzle_page.dart index f3a0e37..0f71699 100644 --- a/lib/view/puzzle_page/puzzle_page.dart +++ b/lib/view/puzzle_page/puzzle_page.dart @@ -48,6 +48,11 @@ class _PuzzlePageState extends State { return BlocBuilder( builder: (context, state) => OrientationBuilder( builder: (context, orientation) { + if (state.puzzle.isSolved && state.moves > 0) { + WidgetsBinding.instance?.addPostFrameCallback( + (timeStamp) => _showVictoryScreen(context), + ); + } if (orientation == Orientation.portrait) { return _buildPortrait(context, state); } else { @@ -309,4 +314,14 @@ class _PuzzlePageState extends State { ), ); } + + Future _showVictoryScreen(BuildContext context) async { + await showDialog( + context: context, + builder: (context) => AlertDialog( + content: Victory(), + ), + ); + _reset(context); + } } diff --git a/lib/view/puzzle_page/widgets/widgets.dart b/lib/view/puzzle_page/widgets/widgets.dart index 37b27f2..415d9e6 100644 --- a/lib/view/puzzle_page/widgets/widgets.dart +++ b/lib/view/puzzle_page/widgets/widgets.dart @@ -5,3 +5,4 @@ export 'puzzle.dart'; export 'text_tile.dart'; export 'tile.dart'; export 'title.dart'; +export 'victory.dart'; diff --git a/lib/view/widgets/victory.dart b/lib/view/widgets/victory.dart new file mode 100644 index 0000000..3d54c2e --- /dev/null +++ b/lib/view/widgets/victory.dart @@ -0,0 +1,26 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +class Victory extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + Padding( + padding: const EdgeInsets.all(16.0), + child: Text( + AppLocalizations.of(context)!.victory_title, + style: Theme.of(context).textTheme.headline2!, + ), + ), + ElevatedButton( + child: Text(AppLocalizations.of(context)!.restart_btn), + onPressed: () { + Navigator.pop(context); + }, + ), + ], + ); + } +}