google_play_games_widget.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import 'package:flutter/material.dart';
  2. import '../utils/google_play_games_manager.dart';
  3. import '../utils/colors.dart';
  4. class GooglePlayGamesWidget extends StatefulWidget {
  5. const GooglePlayGamesWidget({super.key});
  6. @override
  7. State<GooglePlayGamesWidget> createState() => _GooglePlayGamesWidgetState();
  8. }
  9. class _GooglePlayGamesWidgetState extends State<GooglePlayGamesWidget> {
  10. final GooglePlayGamesManager _gamesManager = GooglePlayGamesManager.instance;
  11. bool _isInitializing = false;
  12. @override
  13. Widget build(BuildContext context) {
  14. return Card(
  15. margin: const EdgeInsets.all(16),
  16. child: Padding(
  17. padding: const EdgeInsets.all(16),
  18. child: Column(
  19. crossAxisAlignment: CrossAxisAlignment.start,
  20. mainAxisSize: MainAxisSize.min,
  21. children: [
  22. Row(
  23. children: [
  24. Icon(
  25. Icons.videogame_asset,
  26. color: _gamesManager.isSignedIn
  27. ? ZenColors.defaultLink
  28. : ZenColors.secondaryText,
  29. ),
  30. const SizedBox(width: 8),
  31. Text(
  32. 'Google Play Games',
  33. style: TextStyle(
  34. fontSize: 18,
  35. fontWeight: FontWeight.bold,
  36. color: _gamesManager.isSignedIn
  37. ? ZenColors.primaryText
  38. : ZenColors.secondaryText,
  39. ),
  40. ),
  41. ],
  42. ),
  43. const SizedBox(height: 12),
  44. if (_gamesManager.isSignedIn) ...[
  45. Text(
  46. 'Signed in as: ${_gamesManager.playerName ?? 'Player'}',
  47. style: const TextStyle(
  48. color: ZenColors.primaryText,
  49. fontSize: 14,
  50. ),
  51. ),
  52. const SizedBox(height: 12),
  53. Row(
  54. children: [
  55. Expanded(
  56. child: ElevatedButton.icon(
  57. onPressed: _showLeaderboards,
  58. icon: const Icon(Icons.leaderboard),
  59. label: const Text('Leaderboards'),
  60. style: ElevatedButton.styleFrom(
  61. backgroundColor: ZenColors.buttonBackground,
  62. foregroundColor: ZenColors.buttonText,
  63. ),
  64. ),
  65. ),
  66. const SizedBox(width: 8),
  67. Expanded(
  68. child: ElevatedButton.icon(
  69. onPressed: _showAchievements,
  70. icon: const Icon(Icons.emoji_events),
  71. label: const Text('Achievements'),
  72. style: ElevatedButton.styleFrom(
  73. backgroundColor: ZenColors.buttonBackground,
  74. foregroundColor: ZenColors.buttonText,
  75. ),
  76. ),
  77. ),
  78. ],
  79. ),
  80. const SizedBox(height: 8),
  81. TextButton(
  82. onPressed: _signOut,
  83. child: const Text(
  84. 'Sign Out',
  85. style: TextStyle(color: ZenColors.secondaryText),
  86. ),
  87. ),
  88. ] else ...[
  89. Text(
  90. 'Sign in to save your achievements and compete on leaderboards!',
  91. style: const TextStyle(
  92. color: ZenColors.secondaryText,
  93. fontSize: 14,
  94. ),
  95. ),
  96. const SizedBox(height: 12),
  97. SizedBox(
  98. width: double.infinity,
  99. child: ElevatedButton.icon(
  100. onPressed: _isInitializing ? null : _signIn,
  101. icon: _isInitializing
  102. ? const SizedBox(
  103. width: 16,
  104. height: 16,
  105. child: CircularProgressIndicator(strokeWidth: 2),
  106. )
  107. : const Icon(Icons.login),
  108. label: Text(_isInitializing ? 'Signing In...' : 'Sign In'),
  109. style: ElevatedButton.styleFrom(
  110. backgroundColor: ZenColors.buttonBackground,
  111. foregroundColor: ZenColors.buttonText,
  112. ),
  113. ),
  114. ),
  115. ],
  116. ],
  117. ),
  118. ),
  119. );
  120. }
  121. Future<void> _signIn() async {
  122. setState(() {
  123. _isInitializing = true;
  124. });
  125. try {
  126. final success = await _gamesManager.signIn();
  127. if (success) {
  128. ScaffoldMessenger.of(context).showSnackBar(
  129. const SnackBar(
  130. content: Text('Successfully signed in to Google Play Games!'),
  131. backgroundColor: ZenColors.defaultLink,
  132. ),
  133. );
  134. } else {
  135. ScaffoldMessenger.of(context).showSnackBar(
  136. const SnackBar(
  137. content: Text('Failed to sign in to Google Play Games'),
  138. backgroundColor: Colors.red,
  139. ),
  140. );
  141. }
  142. } catch (e) {
  143. ScaffoldMessenger.of(context).showSnackBar(
  144. SnackBar(
  145. content: Text('Error signing in: $e'),
  146. backgroundColor: Colors.red,
  147. ),
  148. );
  149. } finally {
  150. if (mounted) {
  151. setState(() {
  152. _isInitializing = false;
  153. });
  154. }
  155. }
  156. }
  157. Future<void> _signOut() async {
  158. await _gamesManager.signOut();
  159. setState(() {});
  160. ScaffoldMessenger.of(context).showSnackBar(
  161. const SnackBar(
  162. content: Text('Signed out from Google Play Games'),
  163. backgroundColor: ZenColors.secondaryText,
  164. ),
  165. );
  166. }
  167. Future<void> _showLeaderboards() async {
  168. try {
  169. await _gamesManager.showLeaderboards();
  170. } catch (e) {
  171. ScaffoldMessenger.of(context).showSnackBar(
  172. SnackBar(
  173. content: Text('Error showing leaderboards: $e'),
  174. backgroundColor: Colors.red,
  175. ),
  176. );
  177. }
  178. }
  179. Future<void> _showAchievements() async {
  180. try {
  181. await _gamesManager.showAchievements();
  182. } catch (e) {
  183. ScaffoldMessenger.of(context).showSnackBar(
  184. SnackBar(
  185. content: Text('Error showing achievements: $e'),
  186. backgroundColor: Colors.red,
  187. ),
  188. );
  189. }
  190. }
  191. }