google_play_games_widget.dart 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import 'package:flutter/material.dart';
  2. import '../utils/google_play_games_manager.dart';
  3. import '../utils/colors.dart';
  4. import '../l10n/app_localizations.dart';
  5. class GooglePlayGamesWidget extends StatefulWidget {
  6. const GooglePlayGamesWidget({super.key});
  7. @override
  8. State<GooglePlayGamesWidget> createState() => _GooglePlayGamesWidgetState();
  9. }
  10. class _GooglePlayGamesWidgetState extends State<GooglePlayGamesWidget> {
  11. final GooglePlayGamesManager _gamesManager = GooglePlayGamesManager.instance;
  12. bool _isInitializing = false;
  13. @override
  14. Widget build(BuildContext context) {
  15. final l10n = AppLocalizations.of(context)!;
  16. // Show disabled state if feature is not enabled
  17. if (!_gamesManager.isEnabled) {
  18. return Card(
  19. margin: const EdgeInsets.all(16),
  20. child: Padding(
  21. padding: const EdgeInsets.all(16),
  22. child: Column(
  23. crossAxisAlignment: CrossAxisAlignment.start,
  24. mainAxisSize: MainAxisSize.min,
  25. children: [
  26. Row(
  27. children: [
  28. Icon(
  29. Icons.videogame_asset_off,
  30. color: ZenColors.secondaryText,
  31. ),
  32. const SizedBox(width: 8),
  33. Text(
  34. l10n.googlePlayGames,
  35. style: TextStyle(
  36. fontSize: 18,
  37. fontWeight: FontWeight.bold,
  38. color: ZenColors.secondaryText,
  39. ),
  40. ),
  41. ],
  42. ),
  43. const SizedBox(height: 12),
  44. Text(
  45. l10n.googlePlayGamesServiceDisabled,
  46. style: const TextStyle(
  47. color: ZenColors.secondaryText,
  48. fontSize: 14,
  49. ),
  50. ),
  51. ],
  52. ),
  53. ),
  54. );
  55. }
  56. return Card(
  57. margin: const EdgeInsets.all(16),
  58. child: Padding(
  59. padding: const EdgeInsets.all(16),
  60. child: Column(
  61. crossAxisAlignment: CrossAxisAlignment.start,
  62. mainAxisSize: MainAxisSize.min,
  63. children: [
  64. Row(
  65. children: [
  66. Icon(
  67. Icons.videogame_asset,
  68. color:
  69. _gamesManager.isSignedIn
  70. ? ZenColors.defaultLink
  71. : ZenColors.secondaryText,
  72. ),
  73. const SizedBox(width: 8),
  74. Text(
  75. l10n.googlePlayGames,
  76. style: TextStyle(
  77. fontSize: 18,
  78. fontWeight: FontWeight.bold,
  79. color:
  80. _gamesManager.isSignedIn
  81. ? ZenColors.primaryText
  82. : ZenColors.secondaryText,
  83. ),
  84. ),
  85. ],
  86. ),
  87. const SizedBox(height: 12),
  88. if (_gamesManager.isSignedIn) ...[
  89. Text(
  90. l10n.signedInAs(_gamesManager.playerName ?? 'Player'),
  91. style: const TextStyle(
  92. color: ZenColors.primaryText,
  93. fontSize: 14,
  94. ),
  95. ),
  96. const SizedBox(height: 12),
  97. Row(
  98. children: [
  99. Expanded(
  100. child: ElevatedButton.icon(
  101. onPressed: _showLeaderboards,
  102. icon: const Icon(Icons.leaderboard),
  103. label: Text(l10n.leaderboards),
  104. style: ElevatedButton.styleFrom(
  105. backgroundColor: ZenColors.buttonBackground,
  106. foregroundColor: ZenColors.buttonText,
  107. ),
  108. ),
  109. ),
  110. const SizedBox(width: 8),
  111. Expanded(
  112. child: ElevatedButton.icon(
  113. onPressed: _showAchievements,
  114. icon: const Icon(Icons.emoji_events),
  115. label: Text(l10n.achievements),
  116. style: ElevatedButton.styleFrom(
  117. backgroundColor: ZenColors.buttonBackground,
  118. foregroundColor: ZenColors.buttonText,
  119. ),
  120. ),
  121. ),
  122. ],
  123. ),
  124. const SizedBox(height: 8),
  125. TextButton(
  126. onPressed: _signOut,
  127. child: Text(
  128. l10n.signOut,
  129. style: const TextStyle(color: ZenColors.secondaryText),
  130. ),
  131. ),
  132. ] else ...[
  133. Text(
  134. l10n.signInPrompt,
  135. style: const TextStyle(
  136. color: ZenColors.secondaryText,
  137. fontSize: 14,
  138. ),
  139. ),
  140. const SizedBox(height: 12),
  141. SizedBox(
  142. width: double.infinity,
  143. child: ElevatedButton.icon(
  144. onPressed: _isInitializing ? null : _signIn,
  145. icon:
  146. _isInitializing
  147. ? const SizedBox(
  148. width: 16,
  149. height: 16,
  150. child: CircularProgressIndicator(strokeWidth: 2),
  151. )
  152. : const Icon(Icons.login),
  153. label: Text(_isInitializing ? l10n.signingIn : l10n.signIn),
  154. style: ElevatedButton.styleFrom(
  155. backgroundColor: ZenColors.buttonBackground,
  156. foregroundColor: ZenColors.buttonText,
  157. ),
  158. ),
  159. ),
  160. ],
  161. ],
  162. ),
  163. ),
  164. );
  165. }
  166. Future<void> _signIn() async {
  167. setState(() {
  168. _isInitializing = true;
  169. });
  170. try {
  171. final success = await _gamesManager.signIn();
  172. if (!mounted) return;
  173. if (success) {
  174. ScaffoldMessenger.of(context).showSnackBar(
  175. SnackBar(
  176. content: Text(AppLocalizations.of(context)!.signInSuccess),
  177. backgroundColor: ZenColors.defaultLink,
  178. ),
  179. );
  180. } else {
  181. ScaffoldMessenger.of(context).showSnackBar(
  182. SnackBar(
  183. content: Text(AppLocalizations.of(context)!.signInFailed),
  184. backgroundColor: Colors.red,
  185. ),
  186. );
  187. }
  188. } catch (e) {
  189. if (!mounted) return;
  190. ScaffoldMessenger.of(context).showSnackBar(
  191. SnackBar(
  192. content: Text(AppLocalizations.of(context)!.errorSigningIn(e.toString())),
  193. backgroundColor: Colors.red,
  194. ),
  195. );
  196. } finally {
  197. if (mounted) {
  198. setState(() {
  199. _isInitializing = false;
  200. });
  201. }
  202. }
  203. }
  204. Future<void> _signOut() async {
  205. await _gamesManager.signOut();
  206. if (!mounted) return;
  207. setState(() {});
  208. ScaffoldMessenger.of(context).showSnackBar(
  209. SnackBar(
  210. content: Text(AppLocalizations.of(context)!.signOutSuccess),
  211. backgroundColor: ZenColors.secondaryText,
  212. ),
  213. );
  214. }
  215. Future<void> _showLeaderboards() async {
  216. try {
  217. await _gamesManager.showLeaderboards();
  218. } catch (e) {
  219. if (!mounted) return;
  220. ScaffoldMessenger.of(context).showSnackBar(
  221. SnackBar(
  222. content: Text(AppLocalizations.of(context)!.errorShowingLeaderboards(e.toString())),
  223. backgroundColor: Colors.red,
  224. ),
  225. );
  226. }
  227. }
  228. Future<void> _showAchievements() async {
  229. try {
  230. await _gamesManager.showAchievements();
  231. } catch (e) {
  232. if (!mounted) return;
  233. ScaffoldMessenger.of(context).showSnackBar(
  234. SnackBar(
  235. content: Text(AppLocalizations.of(context)!.errorShowingAchievements(e.toString())),
  236. backgroundColor: Colors.red,
  237. ),
  238. );
  239. }
  240. }
  241. }