Skip to content

Commit

Permalink
feat(victory): show victory screen when user solve the puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrFLEURY committed Feb 18, 2022
1 parent a3dc8e4 commit dccc645
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 3 deletions.
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"
}
2 changes: 2 additions & 0 deletions lib/models/puzzle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tile>.from(data);
Expand Down
15 changes: 15 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,14 @@ class _PuzzlePageState extends State<PuzzlePage> {
),
);
}

Future<void> _showVictoryScreen(BuildContext context) async {
await showDialog(
context: context,
builder: (context) => const AlertDialog(
content: Victory(),
),
);
_reset(context);
}
}
28 changes: 28 additions & 0 deletions lib/view/puzzle_page/widgets/victory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

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);
},
),
],
);
}
}
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

0 comments on commit dccc645

Please sign in to comment.