import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flutter/material.dart'; import 'package:match_magic/game/sprite_loader.dart'; import 'package:match_magic/main.dart'; import 'package:provider/provider.dart'; import 'board.dart'; import 'swap_notifier.dart'; class MatchMagicGameScreen extends StatefulWidget { const MatchMagicGameScreen({Key? key}) : super(key: key); @override _MatchMagicGameScreenState createState() => _MatchMagicGameScreenState(); } class _MatchMagicGameScreenState extends State { late Future> _spritesFuture; Board? board; @override void initState() { super.initState(); _spritesFuture = SpriteLoader.loadSprites(); } void _restartGame() { context.read().resetScore(); setState(() { board = null; _spritesFuture = SpriteLoader.loadSprites(); }); } @override Widget build(BuildContext context) { return FutureBuilder>( future: _spritesFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { if (snapshot.hasError) { return Center(child: Text('Error: ${snapshot.error}')); } else if (!snapshot.hasData || snapshot.data == null) { return const Center(child: Text('No sprites found')); } else { final sprites = snapshot.data!; board ??= Board( sprites: sprites, swapNotifier: context.read()); return Column( children: [ const ScoreDisplay(), Expanded( child: Center( child: AspectRatio( aspectRatio: 1, child: GameWidget(game: board!), ), ), ), ElevatedButton( onPressed: _restartGame, child: const Text('Restart'), ), ], ); } } else { return const Center(child: CircularProgressIndicator()); } }, ); } } class MatchMagicGame extends StatelessWidget { final List sprites; const MatchMagicGame({required this.sprites, Key? key}) : super(key: key); @override Widget build(BuildContext context) { return GameWidget( game: Board(sprites: sprites, swapNotifier: context.read()), ); } } class ScoreDisplay extends StatelessWidget { const ScoreDisplay({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.all(8.0), child: Consumer( builder: (context, notifier, child) { return Text( 'Score: ${notifier.score}', style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ); }, ), ); } }