Reworked ugly code
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
|
import 'reward.dart';
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
final int id;
|
final int id;
|
||||||
final String name;
|
final String name;
|
||||||
final List<Map<String, dynamic>> rewards;
|
final List<Reward> rewards;
|
||||||
|
|
||||||
Game.fromMap(Map<String, dynamic> map, Iterable<Map<String, dynamic>> rewards)
|
Game.fromMap(Map<String, dynamic> map, Iterable<Map<String, dynamic>> rewards)
|
||||||
: id = map['id'],
|
: id = map['id'],
|
||||||
name = map['name'],
|
name = map['name'],
|
||||||
rewards = rewards.toList();
|
rewards = rewards.map((e) => Reward.fromMap(e)).toList();
|
||||||
}
|
}
|
||||||
|
|||||||
8
lib/models/reward.dart
Normal file
8
lib/models/reward.dart
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
class Reward {
|
||||||
|
final int placement;
|
||||||
|
final int reward;
|
||||||
|
|
||||||
|
Reward.fromMap(Map<String, dynamic> map)
|
||||||
|
: placement = map['placement'],
|
||||||
|
reward = map['reward'];
|
||||||
|
}
|
||||||
@@ -33,47 +33,6 @@ class _GamesPageState extends State<GamesPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Widget> _getWidgets(
|
|
||||||
List<Map<String, dynamic>> games, BuildContext context) {
|
|
||||||
final width = MediaQuery.of(context).size.width * 0.8;
|
|
||||||
List<Widget> widgets = [];
|
|
||||||
for (final game in games) {
|
|
||||||
widgets.add(
|
|
||||||
Expanded(
|
|
||||||
child: SizedBox(
|
|
||||||
width: width,
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
||||||
child: _getGamesButton(context, game)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return widgets;
|
|
||||||
}
|
|
||||||
|
|
||||||
Text _getText(String text, BuildContext context) {
|
|
||||||
return Text(
|
|
||||||
text,
|
|
||||||
style: Theme.of(context).textTheme.headlineSmall,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
ElevatedButton _getGamesButton(
|
|
||||||
BuildContext context, Map<String, dynamic> game) {
|
|
||||||
return ElevatedButton(
|
|
||||||
style: ElevatedButton.styleFrom(shape: const BeveledRectangleBorder()),
|
|
||||||
onPressed: () => _onPressed(game),
|
|
||||||
child: _getText(game['name'], context),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _onPressed(Map<String, dynamic> game) {
|
|
||||||
DbHelper.pushGameWin(userProfile!.points, game['points']);
|
|
||||||
DbHelper.pushFeedEntry(
|
|
||||||
'${userProfile!.username} just won at ${game['name']}');
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Widget> _getGames() {
|
List<Widget> _getGames() {
|
||||||
if (games.isEmpty) {
|
if (games.isEmpty) {
|
||||||
return [const CircularProgressIndicator()];
|
return [const CircularProgressIndicator()];
|
||||||
@@ -89,7 +48,7 @@ class _GamesPageState extends State<GamesPage> {
|
|||||||
builder: (context) => GamesPopup(
|
builder: (context) => GamesPopup(
|
||||||
withPlacements: game.rewards.length > 1,
|
withPlacements: game.rewards.length > 1,
|
||||||
game: game,
|
game: game,
|
||||||
onSubmitted: (_) {},
|
onSubmitted: (placement) => _winConfirmed(placement, game),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -99,4 +58,26 @@ class _GamesPageState extends State<GamesPage> {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_winConfirmed(int placement, Game game) {
|
||||||
|
DbHelper.pushGameWin(
|
||||||
|
userProfile!.points,
|
||||||
|
game.rewards
|
||||||
|
.singleWhere((element) => element.placement == placement)
|
||||||
|
.reward);
|
||||||
|
|
||||||
|
String feedText = '';
|
||||||
|
if (placement == 1) {
|
||||||
|
feedText = '${userProfile!.username} just won at ${game.name}';
|
||||||
|
} else if (placement == 2) {
|
||||||
|
feedText = '${userProfile!.username} just got 2nd place in ${game.name}';
|
||||||
|
} else if (placement == 3) {
|
||||||
|
feedText = '${userProfile!.username} just got 3rd place in ${game.name}';
|
||||||
|
} else {
|
||||||
|
feedText =
|
||||||
|
'${userProfile!.username} just got ${placement}th place in ${game.name}';
|
||||||
|
}
|
||||||
|
|
||||||
|
DbHelper.pushFeedEntry(feedText);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import '../models/game.dart';
|
|||||||
|
|
||||||
typedef IntCallback = Function(int);
|
typedef IntCallback = Function(int);
|
||||||
|
|
||||||
class GamesPopup extends StatelessWidget {
|
class GamesPopup extends StatefulWidget {
|
||||||
const GamesPopup({
|
const GamesPopup({
|
||||||
super.key,
|
super.key,
|
||||||
this.withPlacements = false,
|
this.withPlacements = false,
|
||||||
@@ -15,30 +15,79 @@ class GamesPopup extends StatelessWidget {
|
|||||||
final IntCallback onSubmitted;
|
final IntCallback onSubmitted;
|
||||||
final Game game;
|
final Game game;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<GamesPopup> createState() => _GamesPopupState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _GamesPopupState extends State<GamesPopup> {
|
||||||
|
int selectedPlacement = 0;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
if (!widget.withPlacements) {
|
||||||
|
selectedPlacement = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return SizedBox(
|
return AlertDialog(
|
||||||
height: 400,
|
title: _getTitle(widget.withPlacements),
|
||||||
width: 400,
|
content: SizedBox(
|
||||||
child: AlertDialog(
|
width: 400,
|
||||||
title: _getTitle(withPlacements),
|
child: _getContent(widget.withPlacements),
|
||||||
content: _getContent(withPlacements),
|
|
||||||
),
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(),
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: _onConfirmPressed,
|
||||||
|
child: const Text('Confirm'),
|
||||||
|
),
|
||||||
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _getTitle(bool withPlacements) {
|
Widget _getTitle(bool withPlacements) {
|
||||||
if (withPlacements) {
|
return Text('Congratulations,\nyou won at ${widget.game.name}!');
|
||||||
return const Text('Which place did you achieve?');
|
|
||||||
}
|
|
||||||
return Text('Confirm you won at ${game.name}');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _getContent(bool withPlacements) {
|
Widget _getContent(bool withPlacements) {
|
||||||
if (withPlacements) {
|
if (withPlacements) {
|
||||||
return const Text('');
|
return Column(
|
||||||
} else {
|
mainAxisSize: MainAxisSize.min,
|
||||||
return const Text('');
|
children: [
|
||||||
|
const Text('Which placement did you achieve?'),
|
||||||
|
const Padding(padding: EdgeInsets.symmetric(vertical: 10)),
|
||||||
|
DropdownMenu<int>(
|
||||||
|
dropdownMenuEntries: widget.game.rewards
|
||||||
|
.map(
|
||||||
|
(e) => DropdownMenuEntry<int>(
|
||||||
|
value: e.placement,
|
||||||
|
label: e.placement.toString(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
onSelected: (value) => setState(() {
|
||||||
|
selectedPlacement = value ?? 0;
|
||||||
|
}),
|
||||||
|
enableSearch: true,
|
||||||
|
enableFilter: false,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
return const Text('Do you want to confirm your win?');
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onConfirmPressed() {
|
||||||
|
if (selectedPlacement == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
widget.onSubmitted(selectedPlacement);
|
||||||
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user