Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(victory): show victory screen when user solve the puzzle #28

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 3 additions & 1 deletion lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"solve": "Résoudre",
"team_name": "Xpeho mobile",
"moves": "Mouvements {count}",
"play": "Jouer"
"play": "Jouer",
"victory_title": "Victoire !",
"restart_btn": "Recommencer"
}
3 changes: 3 additions & 0 deletions lib/models/puzzle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
PiotrFLEURY marked this conversation as resolved.
Show resolved Hide resolved

/// perform a swap between two tiles
Puzzle move(int value) {
final newData = List<Tile>.from(data);
Expand Down
16 changes: 16 additions & 0 deletions lib/view/puzzle_page/puzzle_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class _PuzzlePageState extends State<PuzzlePage> {
return BlocBuilder<PuzzleCubit, PuzzleState>(
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 {
Expand Down Expand Up @@ -309,4 +314,15 @@ class _PuzzlePageState extends State<PuzzlePage> {
),
);
}

/// Show a dialog to the user to celebrate victory
Future<void> _showVictoryScreen(BuildContext context) async {
PiotrFLEURY marked this conversation as resolved.
Show resolved Hide resolved
await showDialog(
context: context,
builder: (context) => const AlertDialog(
content: Victory(),
),
);
_reset(context);
}
}
29 changes: 29 additions & 0 deletions lib/view/puzzle_page/widgets/victory.dart
Original file line number Diff line number Diff line change
@@ -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 {
PiotrFLEURY marked this conversation as resolved.
Show resolved Hide resolved
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);
},
),
],
);
}
}
1 change: 1 addition & 0 deletions lib/view/puzzle_page/widgets/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export 'puzzle.dart';
export 'text_tile.dart';
export 'tile.dart';
export 'title.dart';
export 'victory.dart';
2 changes: 1 addition & 1 deletion lib/view/view.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library view;

export 'puzzle_page/puzzle_page.dart';
export 'homepage/homepage.dart';
export 'puzzle_page/puzzle_page.dart';
9 changes: 8 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down