my_invitations_screen.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:go_router/go_router.dart';
  5. import '../../../core/l10n/app_localizations.dart';
  6. import '../../../core/theme/app_colors.dart';
  7. import '../../../core/utils/dialog_utils.dart' show extractErrorMessage;
  8. import '../../../core/utils/top_toast.dart';
  9. import '../../../data/repositories/broker_repository.dart';
  10. final _rewardSetListProvider = FutureProvider.autoDispose<List<Map<String, dynamic>>>((ref) {
  11. return ref.read(brokerRepositoryProvider).getRewardSetList();
  12. });
  13. class MyInvitationsScreen extends ConsumerWidget {
  14. const MyInvitationsScreen({super.key});
  15. @override
  16. Widget build(BuildContext context, WidgetRef ref) {
  17. final cs = Theme.of(context).colorScheme;
  18. final listAsync = ref.watch(_rewardSetListProvider);
  19. return Scaffold(
  20. backgroundColor: cs.surface,
  21. appBar: AppBar(
  22. backgroundColor: cs.surface,
  23. elevation: 0,
  24. leading: IconButton(
  25. icon: const Icon(Icons.arrow_back_ios, size: 18),
  26. onPressed: () => context.pop(),
  27. ),
  28. title: Text(AppLocalizations.of(context)!.myInvitations, style: TextStyle(color: cs.onSurface, fontSize: 17, fontWeight: FontWeight.w600)),
  29. centerTitle: true,
  30. ),
  31. body: listAsync.when(
  32. loading: () => const Center(child: CircularProgressIndicator()),
  33. error: (e, _) => Center(child: Text('${AppLocalizations.of(context)!.loadFailed}: $e')),
  34. data: (list) {
  35. if (list.isEmpty) {
  36. return Center(child: Text(AppLocalizations.of(context)!.noInviteRecord, style: TextStyle(color: cs.onSurface.withAlpha(120))));
  37. }
  38. return Column(
  39. children: [
  40. // 表头
  41. Container(
  42. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  43. decoration: BoxDecoration(
  44. color: cs.surfaceContainerHighest.withAlpha(60),
  45. border: Border(bottom: BorderSide(color: cs.outlineVariant.withAlpha(40))),
  46. ),
  47. child: Row(children: [
  48. Expanded(child: Text(AppLocalizations.of(context)!.accountLabel, style: TextStyle(fontSize: 12, color: cs.onSurface.withAlpha(130)))),
  49. SizedBox(width: 80, child: Text(AppLocalizations.of(context)!.levelLabel, style: TextStyle(fontSize: 12, color: cs.onSurface.withAlpha(130)))),
  50. SizedBox(width: 50, child: Text(AppLocalizations.of(context)!.perpetual, textAlign: TextAlign.center,
  51. style: TextStyle(fontSize: 12, color: cs.onSurface.withAlpha(130)))),
  52. SizedBox(width: 50, child: Text(AppLocalizations.of(context)!.copyTrading, textAlign: TextAlign.center,
  53. style: TextStyle(fontSize: 12, color: cs.onSurface.withAlpha(130)))),
  54. ]),
  55. ),
  56. Expanded(
  57. child: ListView.builder(
  58. itemCount: list.length,
  59. itemBuilder: (context, i) => _InvitationRow(item: list[i], onRefresh: () => ref.invalidate(_rewardSetListProvider)),
  60. ),
  61. ),
  62. ],
  63. );
  64. },
  65. ),
  66. );
  67. }
  68. }
  69. class _InvitationRow extends StatelessWidget {
  70. const _InvitationRow({required this.item, required this.onRefresh});
  71. final Map<String, dynamic> item;
  72. final VoidCallback onRefresh;
  73. @override
  74. Widget build(BuildContext context) {
  75. final cs = Theme.of(context).colorScheme;
  76. // id = 账号标识(安卓用 id 字段显示账户列)
  77. final accountId = item['id']?.toString() ?? '--';
  78. final superPartner = item['superPartner']?.toString() ?? '';
  79. final l10n = AppLocalizations.of(context)!;
  80. final levelName = superPartner == '1' ? l10n.brokerLevel : l10n.regularLevel;
  81. // rate is a plain number from API, append "%"
  82. final rateVal = item['rate']?.toString() ?? '0';
  83. final rateStr = rateVal.contains('%') ? rateVal : '$rateVal%';
  84. final followRate = item['followRate'];
  85. final followRateStr = (followRate != null && followRate.toString() != '0') ? '$followRate%' : '--';
  86. return Container(
  87. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
  88. decoration: BoxDecoration(border: Border(bottom: BorderSide(color: cs.outlineVariant.withAlpha(30)))),
  89. child: Row(children: [
  90. Expanded(child: Text(accountId, style: TextStyle(fontSize: 14, color: cs.onSurface, fontWeight: FontWeight.w500))),
  91. SizedBox(
  92. width: 80,
  93. child: Row(children: [
  94. Text(levelName, style: TextStyle(fontSize: 13, color: cs.onSurface)),
  95. const SizedBox(width: 4),
  96. GestureDetector(
  97. onTap: () => _showEditDialog(context, item, onRefresh),
  98. child: Icon(Icons.edit_outlined, size: 15, color: cs.primary),
  99. ),
  100. ]),
  101. ),
  102. SizedBox(width: 50, child: Text(rateStr, textAlign: TextAlign.center,
  103. style: TextStyle(fontSize: 13, color: cs.onSurface))),
  104. SizedBox(width: 50, child: Text(followRateStr, textAlign: TextAlign.center,
  105. style: TextStyle(fontSize: 13, color: cs.onSurface))),
  106. ]),
  107. );
  108. }
  109. void _showEditDialog(BuildContext context, Map<String, dynamic> item, VoidCallback onRefresh) {
  110. showDialog(
  111. context: context,
  112. builder: (_) => _EditRateDialog(item: item, onSuccess: onRefresh),
  113. );
  114. }
  115. }
  116. class _EditRateDialog extends ConsumerStatefulWidget {
  117. const _EditRateDialog({required this.item, required this.onSuccess});
  118. final Map<String, dynamic> item;
  119. final VoidCallback onSuccess;
  120. @override
  121. ConsumerState<_EditRateDialog> createState() => _EditRateDialogState();
  122. }
  123. class _EditRateDialogState extends ConsumerState<_EditRateDialog> {
  124. late TextEditingController _rateCtrl;
  125. late TextEditingController _wcRateCtrl;
  126. bool _loading = false;
  127. @override
  128. void initState() {
  129. super.initState();
  130. final rateRaw = widget.item['rate']?.toString() ?? '';
  131. _rateCtrl = TextEditingController(text: rateRaw.replaceAll('%', ''));
  132. _wcRateCtrl = TextEditingController(text: widget.item['followRate']?.toString() ?? '');
  133. }
  134. @override
  135. void dispose() {
  136. _rateCtrl.dispose();
  137. _wcRateCtrl.dispose();
  138. super.dispose();
  139. }
  140. Future<void> _submit() async {
  141. final id = widget.item['id']?.toString() ?? '';
  142. final rate = int.tryParse(_rateCtrl.text.trim());
  143. final wcRate = int.tryParse(_wcRateCtrl.text.trim());
  144. if (rate == null) {
  145. showTopToast(context, message: AppLocalizations.of(context)!.enterValidPerpRate, backgroundColor: AppColors.fall);
  146. return;
  147. }
  148. setState(() => _loading = true);
  149. try {
  150. await ref.read(brokerRepositoryProvider).setReward(memberId: id, rate: rate, followRate: wcRate);
  151. if (context.mounted) {
  152. Navigator.of(context).pop();
  153. widget.onSuccess();
  154. showTopToast(context, message: AppLocalizations.of(context)!.setSuccess, backgroundColor: AppColors.rise);
  155. }
  156. } catch (e) {
  157. if (context.mounted) {
  158. setState(() => _loading = false);
  159. showTopToast(context, message: extractErrorMessage(e), backgroundColor: AppColors.fall);
  160. }
  161. }
  162. }
  163. @override
  164. Widget build(BuildContext context) {
  165. final cs = Theme.of(context).colorScheme;
  166. final memberId = widget.item['memberId']?.toString() ?? widget.item['id']?.toString() ?? '--';
  167. return Dialog(
  168. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
  169. clipBehavior: Clip.hardEdge,
  170. insetPadding: const EdgeInsets.symmetric(horizontal: 16),
  171. child: Column(
  172. mainAxisSize: MainAxisSize.min,
  173. crossAxisAlignment: CrossAxisAlignment.start,
  174. children: [
  175. // ── Body ──
  176. Padding(
  177. padding: const EdgeInsets.fromLTRB(20, 20, 20, 16),
  178. child: Column(
  179. mainAxisSize: MainAxisSize.min,
  180. crossAxisAlignment: CrossAxisAlignment.start,
  181. children: [
  182. Row(children: [
  183. Text(AppLocalizations.of(context)!.editCommissionRate, style: TextStyle(fontSize: 14, color: cs.onSurface.withAlpha(140))),
  184. const Spacer(),
  185. GestureDetector(
  186. onTap: () => Navigator.of(context).pop(),
  187. child: Icon(Icons.close, size: 20, color: cs.onSurface.withAlpha(140)),
  188. ),
  189. ]),
  190. const SizedBox(height: 6),
  191. Text(memberId, style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700, color: cs.onSurface)),
  192. const SizedBox(height: 20),
  193. _RateInput(controller: _rateCtrl, hint: AppLocalizations.of(context)!.perpRebateRate),
  194. const SizedBox(height: 10),
  195. _RateInput(controller: _wcRateCtrl, hint: AppLocalizations.of(context)!.copyRebateRate),
  196. const SizedBox(height: 8),
  197. Text(AppLocalizations.of(context)!.commissionRateWarning,
  198. style: TextStyle(fontSize: 11, color: cs.onSurface.withAlpha(120), height: 1.5)),
  199. ],
  200. ),
  201. ),
  202. // ── Footer ──
  203. Divider(height: 1, thickness: 1, color: cs.outlineVariant.withAlpha(60)),
  204. SizedBox(
  205. width: double.infinity,
  206. height: 54,
  207. child: TextButton(
  208. style: TextButton.styleFrom(
  209. backgroundColor: Colors.black,
  210. foregroundColor: Colors.white,
  211. shape: const RoundedRectangleBorder(),
  212. padding: EdgeInsets.zero,
  213. ),
  214. onPressed: _loading ? null : _submit,
  215. child: _loading
  216. ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white))
  217. : Text(AppLocalizations.of(context)!.confirm, style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w600)),
  218. ),
  219. ),
  220. ],
  221. ),
  222. );
  223. }
  224. }
  225. class _RateInput extends StatelessWidget {
  226. const _RateInput({required this.controller, required this.hint});
  227. final TextEditingController controller;
  228. final String hint;
  229. @override
  230. Widget build(BuildContext context) {
  231. final cs = Theme.of(context).colorScheme;
  232. return Container(
  233. height: 56,
  234. decoration: BoxDecoration(
  235. color: cs.surfaceContainerHighest.withAlpha(50),
  236. borderRadius: BorderRadius.circular(12),
  237. ),
  238. padding: const EdgeInsets.symmetric(horizontal: 16),
  239. child: Row(children: [
  240. Expanded(
  241. child: TextField(
  242. controller: controller,
  243. keyboardType: TextInputType.number,
  244. style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500, color: cs.onSurface),
  245. decoration: InputDecoration(
  246. hintText: hint,
  247. hintStyle: TextStyle(color: cs.onSurface.withAlpha(80), fontSize: 14, fontWeight: FontWeight.w400),
  248. border: InputBorder.none,
  249. enabledBorder: InputBorder.none,
  250. focusedBorder: InputBorder.none,
  251. isDense: true,
  252. contentPadding: EdgeInsets.zero,
  253. ),
  254. ),
  255. ),
  256. Container(
  257. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
  258. decoration: BoxDecoration(
  259. color: cs.onSurface.withAlpha(25),
  260. borderRadius: BorderRadius.circular(8),
  261. ),
  262. child: Text('%', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: cs.onSurface.withAlpha(200))),
  263. ),
  264. ]),
  265. );
  266. }
  267. }