From 7c1d8cf9401e068190a4cdaec9321583d07dc10d Mon Sep 17 00:00:00 2001 From: PiotrFLEURY Date: Fri, 18 Feb 2022 11:25:26 +0100 Subject: [PATCH] feat(victory): show victory screen when user solve the puzzle --- lib/l10n/app_en.arb | 8 +++++++ lib/l10n/app_fr.arb | 4 +++- lib/models/puzzle.dart | 3 +++ lib/view/puzzle_page/puzzle_page.dart | 16 +++++++++++++ lib/view/puzzle_page/widgets/victory.dart | 29 +++++++++++++++++++++++ lib/view/puzzle_page/widgets/widgets.dart | 1 + lib/view/view.dart | 2 +- pubspec.lock | 9 ++++++- 8 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 lib/view/puzzle_page/widgets/victory.dart 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..1c6432d 100644 --- a/lib/models/puzzle.dart +++ b/lib/models/puzzle.dart @@ -40,6 +40,9 @@ class Puzzle { .map((tile) => tile.currentError(getXof(tile), getYof(tile))) .reduce((a, b) => a + b); + /// Puzzle is solved if all tiles are in their target positions + 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..8fbc061 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,15 @@ class _PuzzlePageState extends State { ), ); } + + /// Show a dialog to the user to celebrate victory + Future _showVictoryScreen(BuildContext context) async { + await showDialog( + context: context, + builder: (context) => const AlertDialog( + content: Victory(), + ), + ); + _reset(context); + } } diff --git a/lib/view/puzzle_page/widgets/victory.dart b/lib/view/puzzle_page/widgets/victory.dart new file mode 100644 index 0000000..bbff319 --- /dev/null +++ b/lib/view/puzzle_page/widgets/victory.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +/// Victory widget to celebrate victory with user. +class Victory extends StatelessWidget { + const Victory({Key? key}) : super(key: key); + + @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); + }, + ), + ], + ); + } +} 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/view.dart b/lib/view/view.dart index 22f8518..ae2e5fc 100644 --- a/lib/view/view.dart +++ b/lib/view/view.dart @@ -1,4 +1,4 @@ library view; -export 'puzzle_page/puzzle_page.dart'; export 'homepage/homepage.dart'; +export 'puzzle_page/puzzle_page.dart'; diff --git a/pubspec.lock b/pubspec.lock index e0701e6..64c1a43 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -434,6 +434,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" meta: dependency: transitive description: @@ -697,7 +704,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.3" + version: "0.4.8" timing: dependency: transitive description: