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 ad1f679
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
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) => AlertDialog(
content: Victory(),
),
);
_reset(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';
26 changes: 26 additions & 0 deletions lib/view/widgets/victory.dart
Original file line number Diff line number Diff line change
@@ -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);
},
),
],
);
}
}

0 comments on commit ad1f679

Please sign in to comment.