google_play_games_widget.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // Show disabled state if feature is not enabled
  15. if (!_gamesManager.isEnabled) {
  16. return Card(
  17. margin: const EdgeInsets.all(16),
  18. child: Padding(
  19. padding: const EdgeInsets.all(16),
  20. child: Column(
  21. crossAxisAlignment: CrossAxisAlignment.start,
  22. mainAxisSize: MainAxisSize.min,
  23. children: [
  24. Row(
  25. children: [
  26. Icon(
  27. Icons.videogame_asset_off,
  28. color: 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: ZenColors.secondaryText,
  37. ),
  38. ),
  39. ],
  40. ),
  41. const SizedBox(height: 12),
  42. Text(
  43. 'Google Play Games Services are currently disabled. This feature can be enabled by developers in future updates.',
  44. style: const TextStyle(
  45. color: ZenColors.secondaryText,
  46. fontSize: 14,
  47. ),
  48. ),
  49. ],
  50. ),
  51. ),
  52. );
  53. }
  54. return Card(
  55. margin: const EdgeInsets.all(16),
  56. child: Padding(
  57. padding: const EdgeInsets.all(16),
  58. child: Column(
  59. crossAxisAlignment: CrossAxisAlignment.start,
  60. mainAxisSize: MainAxisSize.min,
  61. children: [
  62. Row(
  63. children: [
  64. Icon(
  65. Icons.videogame_asset,
  66. color: _gamesManager.isSignedIn
  67. ? ZenColors.defaultLink
  68. : ZenColors.secondaryText,
  69. ),
  70. const SizedBox(width: 8),
  71. Text(
  72. 'Google Play Games',
  73. style: TextStyle(
  74. fontSize: 18,
  75. fontWeight: FontWeight.bold,
  76. color: _gamesManager.isSignedIn
  77. ? ZenColors.primaryText
  78. : ZenColors.secondaryText,
  79. ),
  80. ),
  81. ],
  82. ),
  83. const SizedBox(height: 12),
  84. if (_gamesManager.isSignedIn) ...[
  85. Text(
  86. 'Signed in as: ${_gamesManager.playerName ?? 'Player'}',
  87. style: const TextStyle(
  88. color: ZenColors.primaryText,
  89. fontSize: 14,
  90. ),
  91. ),
  92. const SizedBox(height: 12),
  93. Row(
  94. children: [
  95. Expanded(
  96. child: ElevatedButton.icon(
  97. onPressed: _showLeaderboards,
  98. icon: const Icon(Icons.leaderboard),
  99. label: const Text('Leaderboards'),
  100. style: ElevatedButton.styleFrom(
  101. backgroundColor: ZenColors.buttonBackground,
  102. foregroundColor: ZenColors.buttonText,
  103. ),
  104. ),
  105. ),
  106. const SizedBox(width: 8),
  107. Expanded(
  108. child: ElevatedButton.icon(
  109. onPressed: _showAchievements,
  110. icon: const Icon(Icons.emoji_events),
  111. label: const Text('Achievements'),
  112. style: ElevatedButton.styleFrom(
  113. backgroundColor: ZenColors.buttonBackground,
  114. foregroundColor: ZenColors.buttonText,
  115. ),
  116. ),
  117. ),
  118. ],
  119. ),
  120. const SizedBox(height: 8),
  121. TextButton(
  122. onPressed: _signOut,
  123. child: const Text(
  124. 'Sign Out',
  125. style: TextStyle(color: ZenColors.secondaryText),
  126. ),
  127. ),
  128. ] else ...[
  129. Text(
  130. 'Sign in to save your achievements and compete on leaderboards!',
  131. style: const TextStyle(
  132. color: ZenColors.secondaryText,
  133. fontSize: 14,
  134. ),
  135. ),
  136. const SizedBox(height: 12),
  137. SizedBox(
  138. width: double.infinity,
  139. child: ElevatedButton.icon(
  140. onPressed: _isInitializing ? null : _signIn,
  141. icon: _isInitializing
  142. ? const SizedBox(
  143. width: 16,
  144. height: 16,
  145. child: CircularProgressIndicator(strokeWidth: 2),
  146. )
  147. : const Icon(Icons.login),
  148. label: Text(_isInitializing ? 'Signing In...' : 'Sign In'),
  149. style: ElevatedButton.styleFrom(
  150. backgroundColor: ZenColors.buttonBackground,
  151. foregroundColor: ZenColors.buttonText,
  152. ),
  153. ),
  154. ),
  155. ],
  156. ],
  157. ),
  158. ),
  159. );
  160. }
  161. Future<void> _signIn() async {
  162. setState(() {
  163. _isInitializing = true;
  164. });
  165. try {
  166. final success = await _gamesManager.signIn();
  167. if (!mounted) return;
  168. if (success) {
  169. ScaffoldMessenger.of(context).showSnackBar(
  170. const SnackBar(
  171. content: Text('Successfully signed in to Google Play Games!'),
  172. backgroundColor: ZenColors.defaultLink,
  173. ),
  174. );
  175. } else {
  176. ScaffoldMessenger.of(context).showSnackBar(
  177. const SnackBar(
  178. content: Text('Failed to sign in to Google Play Games'),
  179. backgroundColor: Colors.red,
  180. ),
  181. );
  182. }
  183. } catch (e) {
  184. if (!mounted) return;
  185. ScaffoldMessenger.of(context).showSnackBar(
  186. SnackBar(
  187. content: Text('Error signing in: $e'),
  188. backgroundColor: Colors.red,
  189. ),
  190. );
  191. } finally {
  192. if (mounted) {
  193. setState(() {
  194. _isInitializing = false;
  195. });
  196. }
  197. }
  198. }
  199. Future<void> _signOut() async {
  200. await _gamesManager.signOut();
  201. if (!mounted) return;
  202. setState(() {});
  203. ScaffoldMessenger.of(context).showSnackBar(
  204. const SnackBar(
  205. content: Text('Signed out from Google Play Games'),
  206. backgroundColor: ZenColors.secondaryText,
  207. ),
  208. );
  209. }
  210. Future<void> _showLeaderboards() async {
  211. try {
  212. await _gamesManager.showLeaderboards();
  213. } catch (e) {
  214. if (!mounted) return;
  215. ScaffoldMessenger.of(context).showSnackBar(
  216. SnackBar(
  217. content: Text('Error showing leaderboards: $e'),
  218. backgroundColor: Colors.red,
  219. ),
  220. );
  221. }
  222. }
  223. Future<void> _showAchievements() async {
  224. try {
  225. await _gamesManager.showAchievements();
  226. } catch (e) {
  227. if (!mounted) return;
  228. ScaffoldMessenger.of(context).showSnackBar(
  229. SnackBar(
  230. content: Text('Error showing achievements: $e'),
  231. backgroundColor: Colors.red,
  232. ),
  233. );
  234. }
  235. }
  236. }