invite_friends_screen.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import 'dart:io';
  2. import 'dart:ui' as ui;
  3. import 'package:app_settings/app_settings.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:flutter/services.dart';
  7. import '../../../core/utils/top_toast.dart';
  8. import 'package:flutter_riverpod/flutter_riverpod.dart';
  9. import 'package:go_router/go_router.dart';
  10. import 'package:path_provider/path_provider.dart';
  11. import 'package:qr_flutter/qr_flutter.dart';
  12. import 'package:gal/gal.dart';
  13. import '../../../core/l10n/app_localizations.dart';
  14. import '../../../core/network/dio_client.dart';
  15. import '../../../core/theme/app_colors.dart';
  16. import '../../../data/services/auth_service.dart';
  17. import '../../../providers/app_provider.dart';
  18. /// 邀请好友页面
  19. class InviteFriendsScreen extends ConsumerStatefulWidget {
  20. const InviteFriendsScreen({super.key});
  21. @override
  22. ConsumerState<InviteFriendsScreen> createState() =>
  23. _InviteFriendsScreenState();
  24. }
  25. class _InviteFriendsScreenState extends ConsumerState<InviteFriendsScreen> {
  26. String? _inviteCode;
  27. String? _inviteUrl;
  28. bool _loaded = false;
  29. int _currentPage = 0;
  30. final _pageController = PageController();
  31. // 每张海报单独一个 key 用于截图
  32. final _repaintKeys = [GlobalKey(), GlobalKey(), GlobalKey()];
  33. bool _saving = false;
  34. static const _posterCount = 3;
  35. /// 各域名/语种素材缺失时的统一兜底(英文海报 1)
  36. static const String invitePosterUniversalEnFallback =
  37. 'assets/images/invite_poster_1_EN.png';
  38. @override
  39. void initState() {
  40. super.initState();
  41. _loadInviteData();
  42. }
  43. @override
  44. void dispose() {
  45. _pageController.dispose();
  46. super.dispose();
  47. }
  48. Future<void> _loadInviteData() async {
  49. try {
  50. final dio = ref.read(dioClientProvider);
  51. final data = await AuthService(dio).getMyInfo();
  52. final prefix = data['promotionPrefix']?.toString() ?? '';
  53. final code = data['promotionCode']?.toString() ?? '';
  54. if (mounted) {
  55. setState(() {
  56. _inviteCode = code.isNotEmpty ? code : null;
  57. _inviteUrl = prefix.isNotEmpty || code.isNotEmpty
  58. ? '$prefix$code'
  59. : null;
  60. _loaded = true;
  61. });
  62. }
  63. } catch (_) {
  64. if (mounted) setState(() => _loaded = true);
  65. }
  66. }
  67. void _copy(String text, String label) {
  68. Clipboard.setData(ClipboardData(text: text));
  69. showTopToast(context, message: AppLocalizations.of(context)!.labelCopied(label), backgroundColor: Colors.black87);
  70. }
  71. Future<bool> _ensurePhotoPermission() async {
  72. if (await Gal.hasAccess(toAlbum: true)) return true;
  73. final granted = await Gal.requestAccess(toAlbum: true);
  74. if (granted) return true;
  75. if (!mounted) return false;
  76. final l10n = AppLocalizations.of(context)!;
  77. await showDialog<void>(
  78. context: context,
  79. builder: (ctx) => AlertDialog(
  80. title: Text(l10n.photoPermissionTitle,
  81. style: const TextStyle(color: AppColors.brand)),
  82. content: Text(l10n.photoPermissionContent),
  83. actions: [
  84. TextButton(
  85. onPressed: () => Navigator.of(ctx).pop(),
  86. child: Text(l10n.cancel),
  87. ),
  88. TextButton(
  89. onPressed: () {
  90. Navigator.of(ctx).pop();
  91. AppSettings.openAppSettings();
  92. },
  93. child: Text(l10n.goToSettings),
  94. ),
  95. ],
  96. ),
  97. );
  98. return false;
  99. }
  100. Future<void> _saveCurrentPoster() async {
  101. if (_saving) return;
  102. setState(() => _saving = true);
  103. try {
  104. if (!await _ensurePhotoPermission()) {
  105. return;
  106. }
  107. // 渲染海报为 PNG
  108. await WidgetsBinding.instance.endOfFrame;
  109. final key = _repaintKeys[_currentPage];
  110. final boundary =
  111. key.currentContext!.findRenderObject() as RenderRepaintBoundary;
  112. final image = await boundary.toImage(pixelRatio: 3.0);
  113. final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  114. final bytes = byteData!.buffer.asUint8List();
  115. // 写临时文件后存入相册
  116. final tempDir = await getTemporaryDirectory();
  117. final file = File(
  118. '${tempDir.path}/invite_poster_${DateTime.now().millisecondsSinceEpoch}.png');
  119. await file.writeAsBytes(bytes);
  120. await Gal.putImage(file.path);
  121. if (mounted) {
  122. showTopToast(context,
  123. message: AppLocalizations.of(context)!.saveSuccess,
  124. backgroundColor: Colors.black87);
  125. }
  126. } catch (e) {
  127. if (mounted) {
  128. showTopToast(context,
  129. message: AppLocalizations.of(context)!.saveFailed,
  130. backgroundColor: Colors.black87);
  131. }
  132. } finally {
  133. if (mounted) setState(() => _saving = false);
  134. }
  135. }
  136. String _posterSuffix() {
  137. final locale = ref.read(localeProvider);
  138. if (locale.languageCode == 'zh' && locale.countryCode == 'TW') return 'HK';
  139. if (locale.languageCode == 'zh') return 'CN';
  140. if (locale.languageCode == 'ja') return 'jp';
  141. if (locale.languageCode == 'ko') return 'KR';
  142. return 'EN';
  143. }
  144. @override
  145. Widget build(BuildContext context) {
  146. ref.watch(localeProvider); // 语言切换时触发 rebuild
  147. final safeBottom = MediaQuery.of(context).padding.bottom;
  148. final cs = Theme.of(context).colorScheme;
  149. return Scaffold(
  150. appBar: AppBar(
  151. elevation: 0,
  152. leading: IconButton(
  153. icon: Icon(Icons.chevron_left, color: cs.onSurface, size: 28),
  154. onPressed: () => context.pop(),
  155. ),
  156. title: Text(AppLocalizations.of(context)!.inviteFriends,
  157. style: TextStyle(color: cs.onSurface, fontSize: 17, fontWeight: FontWeight.w600)),
  158. centerTitle: true,
  159. ),
  160. body: Column(
  161. children: [
  162. // ── 海报轮播 ──────────────────────────────────
  163. Expanded(
  164. child: PageView.builder(
  165. controller: _pageController,
  166. itemCount: _posterCount,
  167. onPageChanged: (i) => setState(() => _currentPage = i),
  168. itemBuilder: (context, index) {
  169. return Padding(
  170. padding: const EdgeInsets.symmetric(horizontal: 24),
  171. child: RepaintBoundary(
  172. key: _repaintKeys[index],
  173. child: _PosterPage(
  174. posterAsset:
  175. 'assets/images/invite_poster_${index + 1}_${_posterSuffix()}.png',
  176. posterAssetEn:
  177. 'assets/images/invite_poster_${index + 1}_EN.png',
  178. posterAssetUniversalEn: invitePosterUniversalEnFallback,
  179. inviteUrl: _inviteUrl,
  180. inviteCode: _inviteCode,
  181. loaded: _loaded,
  182. ),
  183. ),
  184. );
  185. },
  186. ),
  187. ),
  188. const SizedBox(height: 16),
  189. // ── 圆点指示器 ────────────────────────────────
  190. Row(
  191. mainAxisAlignment: MainAxisAlignment.center,
  192. children: List.generate(_posterCount, (i) {
  193. final active = i == _currentPage;
  194. return AnimatedContainer(
  195. duration: const Duration(milliseconds: 250),
  196. margin: const EdgeInsets.symmetric(horizontal: 4),
  197. width: active ? 20 : 8,
  198. height: 8,
  199. decoration: BoxDecoration(
  200. color: active ? AppColors.brand : cs.onSurface.withAlpha(60),
  201. borderRadius: BorderRadius.circular(4),
  202. ),
  203. );
  204. }),
  205. ),
  206. const SizedBox(height: 24),
  207. // ── 操作按钮 ──────────────────────────────────
  208. Padding(
  209. padding: EdgeInsets.fromLTRB(24, 0, 24, safeBottom + 16),
  210. child: Row(
  211. children: [
  212. Expanded(
  213. child: _ActionButton(
  214. icon: Icons.save_alt_outlined,
  215. label: AppLocalizations.of(context)!.savePoster,
  216. loading: _saving,
  217. onTap: _inviteUrl != null ? _saveCurrentPoster : null,
  218. ),
  219. ),
  220. const SizedBox(width: 12),
  221. Expanded(
  222. child: _ActionButton(
  223. icon: Icons.copy_outlined,
  224. label: AppLocalizations.of(context)!.copyInviteCode,
  225. onTap: _inviteCode != null
  226. ? () => _copy(_inviteCode!, AppLocalizations.of(context)!.inviteCode)
  227. : null,
  228. ),
  229. ),
  230. const SizedBox(width: 12),
  231. Expanded(
  232. child: _ActionButton(
  233. icon: Icons.link_outlined,
  234. label: AppLocalizations.of(context)!.copyLink,
  235. onTap: _inviteUrl != null
  236. ? () => _copy(_inviteUrl!, AppLocalizations.of(context)!.inviteLink)
  237. : null,
  238. ),
  239. ),
  240. ],
  241. ),
  242. ),
  243. ],
  244. ),
  245. );
  246. }
  247. }
  248. // ── 单张海报页面 ───────────────────────────────────────────────
  249. // 海报原始尺寸 1443×2778,QR 占位白框坐标(原图像素):
  250. // x: 981-1286, y: 2291-2596,约 305×305
  251. // 转为比例:
  252. // fromRight = (1443-1286)/1443 ≈ 0.1088
  253. // fromBottom = (2778-2596)/2778 ≈ 0.0655
  254. // qrSize/W = 305/1443 ≈ 0.2114
  255. class _PosterPage extends StatelessWidget {
  256. const _PosterPage({
  257. required this.posterAsset,
  258. required this.posterAssetEn,
  259. required this.posterAssetUniversalEn,
  260. required this.inviteUrl,
  261. required this.inviteCode,
  262. required this.loaded,
  263. });
  264. final String posterAsset;
  265. final String posterAssetEn;
  266. final String posterAssetUniversalEn;
  267. final String? inviteUrl;
  268. final String? inviteCode;
  269. final bool loaded;
  270. @override
  271. Widget build(BuildContext context) {
  272. return ClipRRect(
  273. borderRadius: BorderRadius.circular(16),
  274. child: LayoutBuilder(
  275. builder: (context, constraints) {
  276. final w = constraints.maxWidth;
  277. final h = constraints.maxHeight;
  278. // QR 码尺寸和位置按原图比例换算(海报 1443×2778,QR 占位白框 305×305)
  279. final qrSize = w * 0.2114;
  280. final right = w * 0.1088;
  281. final bottom = h * 0.0655;
  282. return Stack(
  283. children: [
  284. // 海报背景:当前语种/域名图 → 同序号 EN → 统一 invite_poster_1_EN
  285. Image.asset(
  286. posterAsset,
  287. width: w,
  288. height: h,
  289. fit: BoxFit.fill,
  290. errorBuilder: (_, __, ___) => Image.asset(
  291. posterAssetEn,
  292. width: w,
  293. height: h,
  294. fit: BoxFit.fill,
  295. errorBuilder: (_, __, ___) => Image.asset(
  296. posterAssetUniversalEn,
  297. width: w,
  298. height: h,
  299. fit: BoxFit.fill,
  300. errorBuilder: (_, __, ___) => Container(
  301. color: const Color(0xFF111111),
  302. child: const Center(
  303. child: Icon(Icons.image_outlined,
  304. color: Colors.white30, size: 48),
  305. ),
  306. ),
  307. ),
  308. ),
  309. ),
  310. // 精准覆盖海报右下角 QR 占位框
  311. Positioned(
  312. right: right,
  313. bottom: bottom,
  314. width: qrSize,
  315. height: qrSize,
  316. child: ClipRRect(
  317. borderRadius: BorderRadius.circular(8),
  318. child: inviteUrl != null
  319. ? QrImageView(
  320. data: inviteUrl!,
  321. version: QrVersions.auto,
  322. backgroundColor: Colors.white,
  323. padding: const EdgeInsets.all(6),
  324. )
  325. : loaded
  326. ? const SizedBox.shrink()
  327. : const ColoredBox(
  328. color: Colors.white,
  329. child: Center(
  330. child: SizedBox(
  331. width: 20,
  332. height: 20,
  333. child: CircularProgressIndicator(strokeWidth: 2),
  334. ),
  335. ),
  336. ),
  337. ),
  338. ),
  339. ],
  340. );
  341. },
  342. ),
  343. );
  344. }
  345. }
  346. // ── 底部操作按钮 ──────────────────────────────────────────────
  347. class _ActionButton extends StatelessWidget {
  348. const _ActionButton({
  349. required this.icon,
  350. required this.label,
  351. this.onTap,
  352. this.loading = false,
  353. });
  354. final IconData icon;
  355. final String label;
  356. final VoidCallback? onTap;
  357. final bool loading;
  358. @override
  359. Widget build(BuildContext context) {
  360. final cs = Theme.of(context).colorScheme;
  361. final enabled = onTap != null;
  362. final iconColor = enabled ? cs.onSurface : cs.onSurface.withAlpha(80);
  363. return GestureDetector(
  364. onTap: onTap,
  365. child: Container(
  366. height: 52,
  367. decoration: BoxDecoration(
  368. color: enabled ? cs.surfaceContainerHighest : cs.surfaceContainerHighest.withAlpha(120),
  369. borderRadius: BorderRadius.circular(12),
  370. ),
  371. child: Column(
  372. mainAxisAlignment: MainAxisAlignment.center,
  373. children: [
  374. if (loading)
  375. SizedBox(
  376. width: 18,
  377. height: 18,
  378. child: CircularProgressIndicator(strokeWidth: 2, color: cs.onSurface.withAlpha(140)),
  379. )
  380. else
  381. Icon(icon, color: iconColor, size: 20),
  382. const SizedBox(height: 3),
  383. Text(
  384. label,
  385. style: TextStyle(
  386. color: enabled ? cs.onSurface.withAlpha(180) : cs.onSurface.withAlpha(80),
  387. fontSize: 11,
  388. ),
  389. ),
  390. ],
  391. ),
  392. ),
  393. );
  394. }
  395. }