import 'package:flutter/material.dart'; import 'package:maggs_victory_voyage/services/db_helper.dart'; import 'package:maggs_victory_voyage/widgets/games_popup.dart'; import 'package:provider/provider.dart'; import '../models/game.dart'; import '../models/profile.dart'; import '../services/games_provider.dart'; import '../services/profiles_provider.dart'; import '../widgets/games_button.dart'; class GamesPage extends StatefulWidget { const GamesPage({super.key}); @override State createState() => _GamesPageState(); } class _GamesPageState extends State { Profile? userProfile; List games = []; @override Widget build(BuildContext context) { userProfile = Provider.of(context, listen: true).own; games = Provider.of(context, listen: true).games; return Center( child: Column( children: _getGames(), // _getWidgets(games, context), ), ); } List _getWidgets( List> games, BuildContext context) { final width = MediaQuery.of(context).size.width * 0.8; List 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 game) { return ElevatedButton( style: ElevatedButton.styleFrom(shape: const BeveledRectangleBorder()), onPressed: () => _onPressed(game), child: _getText(game['name'], context), ); } void _onPressed(Map game) { DbHelper.pushGameWin(userProfile!.points, game['points']); DbHelper.pushFeedEntry( '${userProfile!.username} just won at ${game['name']}'); } List _getGames() { if (games.isEmpty) { return [const CircularProgressIndicator()]; } else { final List list = []; for (final game in games) { list.add( GamesButton( title: game.name, buttonPressed: () { showDialog( context: context, builder: (context) => GamesPopup( withPlacements: game.rewards.length > 1, game: game, onSubmitted: (_) {}, ), ); }, ), ); } return list; } } }