| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import 'package:flutter/material.dart';
- import '../utils/google_play_games_manager.dart';
- import '../utils/colors.dart';
- import '../l10n/app_localizations.dart';
- class GooglePlayGamesWidget extends StatefulWidget {
- const GooglePlayGamesWidget({super.key});
- @override
- State<GooglePlayGamesWidget> createState() => _GooglePlayGamesWidgetState();
- }
- class _GooglePlayGamesWidgetState extends State<GooglePlayGamesWidget> {
- final GooglePlayGamesManager _gamesManager = GooglePlayGamesManager.instance;
- bool _isInitializing = false;
- @override
- Widget build(BuildContext context) {
- final l10n = AppLocalizations.of(context)!;
- // Show disabled state if feature is not enabled
- if (!_gamesManager.isEnabled) {
- return Card(
- margin: const EdgeInsets.all(16),
- child: Padding(
- padding: const EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: [
- Row(
- children: [
- Icon(
- Icons.videogame_asset_off,
- color: ZenColors.secondaryText,
- ),
- const SizedBox(width: 8),
- Text(
- l10n.googlePlayGames,
- style: TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.bold,
- color: ZenColors.secondaryText,
- ),
- ),
- ],
- ),
- const SizedBox(height: 12),
- Text(
- l10n.googlePlayGamesServiceDisabled,
- style: const TextStyle(
- color: ZenColors.secondaryText,
- fontSize: 14,
- ),
- ),
- ],
- ),
- ),
- );
- }
- return Card(
- margin: const EdgeInsets.all(16),
- child: Padding(
- padding: const EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: [
- Row(
- children: [
- Icon(
- Icons.videogame_asset,
- color:
- _gamesManager.isSignedIn
- ? ZenColors.defaultLink
- : ZenColors.secondaryText,
- ),
- const SizedBox(width: 8),
- Text(
- l10n.googlePlayGames,
- style: TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.bold,
- color:
- _gamesManager.isSignedIn
- ? ZenColors.primaryText
- : ZenColors.secondaryText,
- ),
- ),
- ],
- ),
- const SizedBox(height: 12),
- if (_gamesManager.isSignedIn) ...[
- Text(
- l10n.signedInAs(_gamesManager.playerName ?? 'Player'),
- style: const TextStyle(
- color: ZenColors.primaryText,
- fontSize: 14,
- ),
- ),
- const SizedBox(height: 12),
- Row(
- children: [
- Expanded(
- child: ElevatedButton.icon(
- onPressed: _showLeaderboards,
- icon: const Icon(Icons.leaderboard),
- label: Text(l10n.leaderboards),
- style: ElevatedButton.styleFrom(
- backgroundColor: ZenColors.buttonBackground,
- foregroundColor: ZenColors.buttonText,
- ),
- ),
- ),
- const SizedBox(width: 8),
- Expanded(
- child: ElevatedButton.icon(
- onPressed: _showAchievements,
- icon: const Icon(Icons.emoji_events),
- label: Text(l10n.achievements),
- style: ElevatedButton.styleFrom(
- backgroundColor: ZenColors.buttonBackground,
- foregroundColor: ZenColors.buttonText,
- ),
- ),
- ),
- ],
- ),
- const SizedBox(height: 8),
- TextButton(
- onPressed: _signOut,
- child: Text(
- l10n.signOut,
- style: const TextStyle(color: ZenColors.secondaryText),
- ),
- ),
- ] else ...[
- Text(
- l10n.signInPrompt,
- style: const TextStyle(
- color: ZenColors.secondaryText,
- fontSize: 14,
- ),
- ),
- const SizedBox(height: 12),
- SizedBox(
- width: double.infinity,
- child: ElevatedButton.icon(
- onPressed: _isInitializing ? null : _signIn,
- icon:
- _isInitializing
- ? const SizedBox(
- width: 16,
- height: 16,
- child: CircularProgressIndicator(strokeWidth: 2),
- )
- : const Icon(Icons.login),
- label: Text(_isInitializing ? l10n.signingIn : l10n.signIn),
- style: ElevatedButton.styleFrom(
- backgroundColor: ZenColors.buttonBackground,
- foregroundColor: ZenColors.buttonText,
- ),
- ),
- ),
- ],
- ],
- ),
- ),
- );
- }
- Future<void> _signIn() async {
- setState(() {
- _isInitializing = true;
- });
- try {
- final success = await _gamesManager.signIn();
- if (!mounted) return;
- if (success) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text(AppLocalizations.of(context)!.signInSuccess),
- backgroundColor: ZenColors.defaultLink,
- ),
- );
- } else {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text(AppLocalizations.of(context)!.signInFailed),
- backgroundColor: Colors.red,
- ),
- );
- }
- } catch (e) {
- if (!mounted) return;
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text(AppLocalizations.of(context)!.errorSigningIn(e.toString())),
- backgroundColor: Colors.red,
- ),
- );
- } finally {
- if (mounted) {
- setState(() {
- _isInitializing = false;
- });
- }
- }
- }
- Future<void> _signOut() async {
- await _gamesManager.signOut();
- if (!mounted) return;
- setState(() {});
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text(AppLocalizations.of(context)!.signOutSuccess),
- backgroundColor: ZenColors.secondaryText,
- ),
- );
- }
- Future<void> _showLeaderboards() async {
- try {
- await _gamesManager.showLeaderboards();
- } catch (e) {
- if (!mounted) return;
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text(AppLocalizations.of(context)!.errorShowingLeaderboards(e.toString())),
- backgroundColor: Colors.red,
- ),
- );
- }
- }
- Future<void> _showAchievements() async {
- try {
- await _gamesManager.showAchievements();
- } catch (e) {
- if (!mounted) return;
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text(AppLocalizations.of(context)!.errorShowingAchievements(e.toString())),
- backgroundColor: Colors.red,
- ),
- );
- }
- }
- }
|