Reworked ugly code
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'reward.dart';
|
||||
|
||||
class Game {
|
||||
final int id;
|
||||
final String name;
|
||||
final List<Map<String, dynamic>> rewards;
|
||||
final List<Reward> rewards;
|
||||
|
||||
Game.fromMap(Map<String, dynamic> map, Iterable<Map<String, dynamic>> rewards)
|
||||
: id = map['id'],
|
||||
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() {
|
||||
if (games.isEmpty) {
|
||||
return [const CircularProgressIndicator()];
|
||||
@@ -89,7 +48,7 @@ class _GamesPageState extends State<GamesPage> {
|
||||
builder: (context) => GamesPopup(
|
||||
withPlacements: game.rewards.length > 1,
|
||||
game: game,
|
||||
onSubmitted: (_) {},
|
||||
onSubmitted: (placement) => _winConfirmed(placement, game),
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -99,4 +58,26 @@ class _GamesPageState extends State<GamesPage> {
|
||||
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);
|
||||
|
||||
class GamesPopup extends StatelessWidget {
|
||||
class GamesPopup extends StatefulWidget {
|
||||
const GamesPopup({
|
||||
super.key,
|
||||
this.withPlacements = false,
|
||||
@@ -15,30 +15,79 @@ class GamesPopup extends StatelessWidget {
|
||||
final IntCallback onSubmitted;
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
return SizedBox(
|
||||
height: 400,
|
||||
return AlertDialog(
|
||||
title: _getTitle(widget.withPlacements),
|
||||
content: SizedBox(
|
||||
width: 400,
|
||||
child: AlertDialog(
|
||||
title: _getTitle(withPlacements),
|
||||
content: _getContent(withPlacements),
|
||||
child: _getContent(widget.withPlacements),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: _onConfirmPressed,
|
||||
child: const Text('Confirm'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _getTitle(bool withPlacements) {
|
||||
if (withPlacements) {
|
||||
return const Text('Which place did you achieve?');
|
||||
}
|
||||
return Text('Confirm you won at ${game.name}');
|
||||
return Text('Congratulations,\nyou won at ${widget.game.name}!');
|
||||
}
|
||||
|
||||
Widget _getContent(bool withPlacements) {
|
||||
if (withPlacements) {
|
||||
return const Text('');
|
||||
} else {
|
||||
return const Text('');
|
||||
}
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
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