follow_setting_screen.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../../../core/l10n/app_localizations.dart';
  4. import '../../../core/utils/avatar_urls.dart';
  5. import '../../../core/theme/app_colors.dart';
  6. import '../../../core/utils/dialog_utils.dart';
  7. import '../../../core/utils/top_toast.dart';
  8. import '../../../data/repositories/copy_trading_repository.dart';
  9. class FollowSettingScreen extends ConsumerStatefulWidget {
  10. const FollowSettingScreen({super.key, required this.trader});
  11. /// 从 TraderDetailScreen 传入的完整 trader info map
  12. final Map<String, dynamic> trader;
  13. @override
  14. ConsumerState<FollowSettingScreen> createState() =>
  15. _FollowSettingScreenState();
  16. }
  17. class _FollowSettingScreenState extends ConsumerState<FollowSettingScreen> {
  18. List<Map<String, dynamic>> _symbols = [];
  19. bool _loadingSymbols = true;
  20. bool _submitting = false;
  21. static const _avatarColors = [
  22. Color(0xFFf7931a),
  23. Color(0xFF627eea),
  24. Color(0xFF9945ff),
  25. Color(0xFFf3ba2f),
  26. Color(0xFF2775ca),
  27. Color(0xFF00aae4),
  28. ];
  29. @override
  30. void initState() {
  31. super.initState();
  32. _loadSymbols();
  33. }
  34. Future<void> _loadSymbols() async {
  35. final traderId = widget.trader['id']?.toString() ?? '';
  36. if (traderId.isEmpty) {
  37. setState(() => _loadingSymbols = false);
  38. return;
  39. }
  40. try {
  41. final list = await ref
  42. .read(copyTradingRepositoryProvider)
  43. .getTraderSymbols(traderId);
  44. if (mounted) {
  45. setState(() {
  46. _symbols = list;
  47. _loadingSymbols = false;
  48. });
  49. }
  50. } catch (_) {
  51. if (mounted) setState(() => _loadingSymbols = false);
  52. }
  53. }
  54. Future<void> _submit() async {
  55. if (_submitting) return;
  56. setState(() => _submitting = true);
  57. final traderId = widget.trader['id']?.toString() ?? '';
  58. try {
  59. final allSymbolNames = _symbols
  60. .map((s) =>
  61. s['symbolName']?.toString() ?? s['symbol']?.toString() ?? '')
  62. .where((n) => n.isNotEmpty)
  63. .toList();
  64. await ref.read(copyTradingRepositoryProvider).followTrader({
  65. 'traderId': traderId,
  66. 'tradingMode': '30', // 按交易员比例
  67. 'tradingAmount': '100', // 100% 跟随
  68. 'symbols': allSymbolNames,
  69. });
  70. if (mounted) {
  71. showTopToast(
  72. context,
  73. message: AppLocalizations.of(context)!.copyTradingSuccess,
  74. backgroundColor: const Color(0xFF2ECC71),
  75. );
  76. Navigator.of(context).pop(true); // 返回 true 表示跟单成功
  77. }
  78. } catch (e) {
  79. if (mounted) {
  80. setState(() => _submitting = false);
  81. showTopToast(context, message: extractErrorMessage(e));
  82. }
  83. }
  84. }
  85. Color _avatarBg(String name) => _avatarColors[
  86. name.isEmpty ? 0 : name.codeUnitAt(0) % _avatarColors.length];
  87. @override
  88. Widget build(BuildContext context) {
  89. final cs = Theme.of(context).colorScheme;
  90. final isDark = Theme.of(context).brightness == Brightness.dark;
  91. final pageBg = isDark ? AppColors.darkBg : AppColors.lightBg;
  92. final nickname = widget.trader['nickname']?.toString() ?? '';
  93. final levelName = widget.trader['levelName']?.toString() ?? '';
  94. final avatarUrl =
  95. resolvedAvatarUrlFromRecord(Map<String, dynamic>.from(widget.trader));
  96. final letter = nickname.isNotEmpty ? nickname[0].toUpperCase() : '?';
  97. final moneyStrength = widget.trader['moneyStrength']?.toString() ?? '--';
  98. final registerDays = widget.trader['registerDays']?.toString() ?? '--';
  99. return Scaffold(
  100. backgroundColor: pageBg,
  101. appBar: AppBar(
  102. leading: IconButton(
  103. icon: const Icon(Icons.arrow_back_ios, size: 18),
  104. onPressed: () => Navigator.of(context).pop(),
  105. ),
  106. title: Text(AppLocalizations.of(context)!.copyTradingSettings,
  107. style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w600)),
  108. ),
  109. body: Column(
  110. children: [
  111. Expanded(
  112. child: SingleChildScrollView(
  113. padding: const EdgeInsets.all(16),
  114. child: Column(
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: [
  117. // ── 交易员信息卡片 ──────────────────────────────
  118. Container(
  119. padding: const EdgeInsets.all(14),
  120. decoration: BoxDecoration(
  121. color: isDark
  122. ? AppColors.darkBgSecondary
  123. : AppColors.lightBg,
  124. borderRadius: BorderRadius.circular(12),
  125. border: Border.all(
  126. color: isDark
  127. ? AppColors.darkDivider
  128. : AppColors.lightDivider,
  129. width: 0.5,
  130. ),
  131. ),
  132. child: Row(
  133. children: [
  134. // 头像
  135. _Avatar(
  136. letter: letter,
  137. avatarUrl: avatarUrl,
  138. bg: _avatarBg(nickname),
  139. size: 48,
  140. ),
  141. const SizedBox(width: 12),
  142. // 名称 + 等级
  143. Expanded(
  144. child: Column(
  145. crossAxisAlignment: CrossAxisAlignment.start,
  146. children: [
  147. Text(
  148. nickname,
  149. style: TextStyle(
  150. color: cs.onSurface,
  151. fontSize: 15,
  152. fontWeight: FontWeight.w700,
  153. ),
  154. ),
  155. if (levelName.isNotEmpty) ...[
  156. const SizedBox(height: 4),
  157. _LevelBadge(level: levelName),
  158. ],
  159. ],
  160. ),
  161. ),
  162. // 右侧:资金实力 / 入驻天数
  163. Builder(builder: (context) {
  164. final l10n = AppLocalizations.of(context)!;
  165. return Column(
  166. crossAxisAlignment: CrossAxisAlignment.end,
  167. children: [
  168. _RightStat(
  169. label: l10n.fundStrength,
  170. value: '≥$moneyStrength'),
  171. const SizedBox(height: 6),
  172. _RightStat(
  173. label: l10n.settledDaysTitle,
  174. value: registerDays),
  175. ],
  176. );
  177. }),
  178. ],
  179. ),
  180. ),
  181. const SizedBox(height: 20),
  182. // ── 跟单合约 ──────────────────────────────────
  183. Text(
  184. AppLocalizations.of(context)!.tradingContracts,
  185. style: TextStyle(
  186. color: cs.onSurface.withAlpha(153),
  187. fontSize: 13,
  188. ),
  189. ),
  190. const SizedBox(height: 10),
  191. if (_loadingSymbols)
  192. const Center(
  193. child:
  194. CircularProgressIndicator(color: AppColors.brand))
  195. else if (_symbols.isEmpty)
  196. Text(
  197. AppLocalizations.of(context)!.noCopyContracts,
  198. style: TextStyle(
  199. color: cs.onSurface.withAlpha(120), fontSize: 13),
  200. )
  201. else
  202. Wrap(
  203. spacing: 8,
  204. runSpacing: 8,
  205. children: _symbols.map((s) {
  206. final name = s['symbolName']?.toString() ??
  207. s['symbol']?.toString() ??
  208. '';
  209. return Container(
  210. padding: const EdgeInsets.symmetric(
  211. horizontal: 14, vertical: 7),
  212. decoration: BoxDecoration(
  213. color: AppColors.brand.withValues(alpha: 0.12),
  214. borderRadius: BorderRadius.circular(8),
  215. border: Border.all(
  216. color: AppColors.brand.withValues(alpha: 0.6),
  217. width: 1.5,
  218. ),
  219. ),
  220. child: Text(
  221. name,
  222. style: const TextStyle(
  223. color: AppColors.brand,
  224. fontSize: 13,
  225. fontWeight: FontWeight.w600,
  226. ),
  227. ),
  228. );
  229. }).toList(),
  230. ),
  231. ],
  232. ),
  233. ),
  234. ),
  235. // ── 底部按钮 ───────────────────────────────────
  236. Container(
  237. padding: EdgeInsets.fromLTRB(
  238. 16, 12, 16, 12 + MediaQuery.of(context).padding.bottom),
  239. decoration: BoxDecoration(
  240. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBg,
  241. border: Border(
  242. top: BorderSide(
  243. color:
  244. isDark ? AppColors.darkDivider : AppColors.lightDivider,
  245. ),
  246. ),
  247. ),
  248. child: SizedBox(
  249. width: double.infinity,
  250. height: 48,
  251. child: ElevatedButton(
  252. onPressed: _submitting ? null : _submit,
  253. style: ElevatedButton.styleFrom(
  254. backgroundColor: AppColors.brand,
  255. foregroundColor: Colors.black,
  256. shape: const StadiumBorder(),
  257. elevation: 0,
  258. disabledBackgroundColor: AppColors.brand.withAlpha(60),
  259. ),
  260. child: _submitting
  261. ? const SizedBox(
  262. width: 20,
  263. height: 20,
  264. child: CircularProgressIndicator(
  265. strokeWidth: 2, color: Colors.black),
  266. )
  267. : Text(AppLocalizations.of(context)!.startCopyTrading,
  268. style: const TextStyle(
  269. fontSize: 15, fontWeight: FontWeight.w600)),
  270. ),
  271. ),
  272. ),
  273. ],
  274. ),
  275. );
  276. }
  277. }
  278. // ── 头像 ──────────────────────────────────────────────────
  279. class _Avatar extends StatelessWidget {
  280. const _Avatar(
  281. {required this.letter,
  282. required this.bg,
  283. required this.size,
  284. this.avatarUrl});
  285. final String letter;
  286. final Color bg;
  287. final double size;
  288. final String? avatarUrl;
  289. @override
  290. Widget build(BuildContext context) {
  291. if (avatarUrl != null && avatarUrl!.isNotEmpty) {
  292. return ClipOval(
  293. child: Image.network(
  294. avatarUrl!,
  295. width: size,
  296. height: size,
  297. fit: BoxFit.cover,
  298. errorBuilder: (_, __, ___) => _fallback(),
  299. ),
  300. );
  301. }
  302. return _fallback();
  303. }
  304. Widget _fallback() => Container(
  305. width: size,
  306. height: size,
  307. decoration: BoxDecoration(color: bg, shape: BoxShape.circle),
  308. child: Center(
  309. child: Text(
  310. letter,
  311. style: TextStyle(
  312. color: Colors.white,
  313. fontSize: size * 0.4,
  314. fontWeight: FontWeight.w700,
  315. ),
  316. ),
  317. ),
  318. );
  319. }
  320. // ── 等级角标 ───────────────────────────────────────────────
  321. class _LevelBadge extends StatelessWidget {
  322. const _LevelBadge({required this.level});
  323. final String level;
  324. @override
  325. Widget build(BuildContext context) {
  326. return Container(
  327. padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
  328. decoration: BoxDecoration(
  329. color: AppColors.brand.withValues(alpha: 0.12),
  330. borderRadius: BorderRadius.circular(4),
  331. ),
  332. child: Row(
  333. mainAxisSize: MainAxisSize.min,
  334. children: [
  335. const Icon(Icons.access_time, size: 10, color: AppColors.brand),
  336. const SizedBox(width: 3),
  337. Text(level,
  338. style: const TextStyle(
  339. color: AppColors.brand,
  340. fontSize: 11,
  341. fontWeight: FontWeight.w600)),
  342. ],
  343. ),
  344. );
  345. }
  346. }
  347. // ── 右侧统计项 ─────────────────────────────────────────────
  348. class _RightStat extends StatelessWidget {
  349. const _RightStat({required this.label, required this.value});
  350. final String label;
  351. final String value;
  352. @override
  353. Widget build(BuildContext context) {
  354. final cs = Theme.of(context).colorScheme;
  355. return Column(
  356. crossAxisAlignment: CrossAxisAlignment.end,
  357. children: [
  358. Text(label,
  359. style: TextStyle(color: cs.onSurface.withAlpha(120), fontSize: 11)),
  360. const SizedBox(height: 2),
  361. Text(value,
  362. style: TextStyle(
  363. color: cs.onSurface,
  364. fontSize: 14,
  365. fontWeight: FontWeight.w700)),
  366. ],
  367. );
  368. }
  369. }