import 'package:flutter/material.dart'; import 'package:fluttergamebox/api/api_service.dart'; import 'package:fluttergamebox/common/theme.dart'; import 'package:fluttergamebox/store/app_state.dart'; import 'package:fluttergamebox/utils/util.dart'; /// 会员激活弹窗 void showActivateDialog(BuildContext context) { showDialog( context: context, barrierColor: Colors.black54, builder: (context) => const _ActivateDialog(), ); } class _ActivateDialog extends StatefulWidget { const _ActivateDialog(); @override State<_ActivateDialog> createState() => _ActivateDialogState(); } class _ActivateDialogState extends State<_ActivateDialog> { final _codeCtrl = TextEditingController(); bool _loading = false; Future _activate() async { final code = _codeCtrl.text.trim(); if (code.isEmpty) { _tip('请输入您的激活码!', error: true); return; } setState(() => _loading = true); try { final machine = await getMachineCode(); final res = await ApiService.activateKey(code, machine); if (!mounted) return; if (res['code'] == 0) { await AppState.instance.refreshUserInfo(); if (mounted) { _tip(res['msg'] ?? '激活成功'); Future.delayed(const Duration(milliseconds: 1200), () { if (mounted) Navigator.pop(context); }); } } else { _tip(res['msg'] ?? '激活失败', error: true); } } catch (e) { _tip('网络错误,请稍后重试', error: true); } finally { if (mounted) setState(() => _loading = false); } } void _tip(String msg, {bool error = false}) { final overlay = Overlay.of(context); final entry = OverlayEntry( builder: (context) => Positioned( top: 60, left: MediaQuery.of(context).size.width / 2 - 140, child: Material( color: Colors.transparent, child: Container( width: 280, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), decoration: BoxDecoration( color: error ? AppColors.red : AppColors.green, borderRadius: BorderRadius.circular(6), ), child: Row( children: [ Icon(error ? Icons.error_outline : Icons.check_circle, color: Colors.white, size: 18), const SizedBox(width: 8), Expanded( child: Text(msg, style: const TextStyle(color: Colors.white, fontSize: 13)), ), ], ), ), ), ), ); overlay.insert(entry); Future.delayed(const Duration(seconds: 2), () => entry.remove()); } @override Widget build(BuildContext context) { return Dialog( backgroundColor: Colors.white, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)), child: Container( width: 400, padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Container( padding: const EdgeInsets.all(4), decoration: BoxDecoration( color: AppColors.accent, shape: BoxShape.circle, ), child: const Icon(Icons.vpn_key, color: Colors.white, size: 14), ), const SizedBox(width: 8), const Text('会员激活', style: TextStyle( color: Colors.black, fontSize: 16, fontWeight: FontWeight.bold)), const Spacer(), GestureDetector( onTap: () => Navigator.pop(context), child: const Icon(Icons.close, color: Colors.grey, size: 18), ), ], ), const SizedBox(height: 18), Container( width: double.infinity, padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10), decoration: BoxDecoration( color: const Color(0xFFE8F1FF), borderRadius: BorderRadius.circular(4), ), child: const Text('请输入您的激活码!', style: TextStyle( color: Color(0xFF2B7FFF), fontSize: 13)), ), const SizedBox(height: 16), const Text('激活码', style: TextStyle(color: Color(0xFF666666), fontSize: 13)), const SizedBox(height: 8), TextField( controller: _codeCtrl, style: const TextStyle(color: Color(0xFF333333), fontSize: 14), decoration: InputDecoration( hintText: 'XXXX-XXXX-XXXX-XXXX (右键粘贴)', hintStyle: const TextStyle( color: Color(0xFFAAAAAA), fontSize: 13), border: OutlineInputBorder( borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Color(0xFFDDDDDD)), ), contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12), ), ), const SizedBox(height: 24), Row( children: [ Expanded( child: OutlinedButton( onPressed: () => Navigator.pop(context), style: OutlinedButton.styleFrom( foregroundColor: Color(0xFF666666), side: const BorderSide(color: Color(0xFFDDDDDD)), padding: const EdgeInsets.symmetric(vertical: 12), ), child: const Text('取消'), ), ), const SizedBox(width: 12), Expanded( child: ElevatedButton( onPressed: _loading ? null : _activate, style: ElevatedButton.styleFrom( backgroundColor: AppColors.accent, foregroundColor: Colors.white, padding: const EdgeInsets.symmetric(vertical: 12), ), child: _loading ? const SizedBox( width: 16, height: 16, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white)) : const Text('激活'), ), ), ], ), ], ), ), ); } }