top_traders_section.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import '../../../core/l10n/app_localizations.dart';
  5. import '../../../core/theme/app_colors.dart';
  6. import '../../../core/utils/number_format.dart';
  7. import '../../../data/models/home/top_trader.dart';
  8. import '../../../providers/auth_provider.dart';
  9. import '../../../providers/top_trader_provider.dart';
  10. /// 顶级交易专家横向卡片区块
  11. class TopTradersSection extends ConsumerWidget {
  12. const TopTradersSection({super.key});
  13. @override
  14. Widget build(BuildContext context, WidgetRef ref) {
  15. final cs = Theme.of(context).colorScheme;
  16. final state = ref.watch(topTraderProvider);
  17. if (state.isLoading) {
  18. return const Padding(
  19. padding: EdgeInsets.symmetric(vertical: 24),
  20. child: Center(child: CircularProgressIndicator(strokeWidth: 2)),
  21. );
  22. }
  23. if (state.traders.isEmpty) return const SizedBox.shrink();
  24. return Column(
  25. children: [
  26. // 标题
  27. Padding(
  28. padding: const EdgeInsets.fromLTRB(16, 8, 16, 12),
  29. child: Row(
  30. children: [
  31. Text(
  32. AppLocalizations.of(context)!.topTraders,
  33. style: TextStyle(
  34. color: cs.onSurface,
  35. fontSize: 17,
  36. fontWeight: FontWeight.w700,
  37. ),
  38. ),
  39. const Spacer(),
  40. GestureDetector(
  41. onTap: () => context.go('/copy-trading'),
  42. child: Icon(Icons.chevron_right,
  43. color: cs.onSurface.withAlpha(100), size: 22),
  44. ),
  45. ],
  46. ),
  47. ),
  48. // 交易专家卡片列表
  49. SizedBox(
  50. height: 240,
  51. child: ListView.separated(
  52. scrollDirection: Axis.horizontal,
  53. padding: const EdgeInsets.symmetric(horizontal: 16),
  54. itemCount: state.traders.length,
  55. separatorBuilder: (_, __) => const SizedBox(width: 12),
  56. itemBuilder: (context, index) =>
  57. _TraderCard(trader: state.traders[index]),
  58. ),
  59. ),
  60. const SizedBox(height: 16),
  61. ],
  62. );
  63. }
  64. }
  65. // ── 交易员卡片 ─────────────────────────────────────────────
  66. class _TraderCard extends ConsumerWidget {
  67. const _TraderCard({required this.trader});
  68. final TopTrader trader;
  69. @override
  70. Widget build(BuildContext context, WidgetRef ref) {
  71. final cs = Theme.of(context).colorScheme;
  72. final isDark = Theme.of(context).brightness == Brightness.dark;
  73. final screenWidth = MediaQuery.of(context).size.width;
  74. // 左边距16 + 右侧留出约36px的下一张预览 + 间距12
  75. final cardWidth = screenWidth - 16 - 12 - 36;
  76. return Container(
  77. width: cardWidth,
  78. padding: const EdgeInsets.fromLTRB(16, 20, 16, 14),
  79. decoration: BoxDecoration(
  80. color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
  81. borderRadius: BorderRadius.circular(16),
  82. border: Border.all(color: cs.outline.withAlpha(30)),
  83. ),
  84. child: Column(
  85. children: [
  86. // 头像:深色圆形底 + 头像/字母
  87. Container(
  88. width: 60,
  89. height: 60,
  90. decoration: BoxDecoration(
  91. color: AppColors.darkAvatarBg,
  92. shape: BoxShape.circle,
  93. border: Border.all(color: AppColors.brand, width: 2.5),
  94. ),
  95. child: trader.avatar.isNotEmpty
  96. ? ClipOval(
  97. child: Image.network(
  98. trader.avatar,
  99. fit: BoxFit.cover,
  100. errorBuilder: (_, __, ___) => _AvatarLetter(trader: trader),
  101. ),
  102. )
  103. : _AvatarLetter(trader: trader),
  104. ),
  105. const SizedBox(height: 10),
  106. // 昵称
  107. Text(
  108. trader.nickname,
  109. style: TextStyle(
  110. color: cs.onSurface,
  111. fontSize: 15,
  112. fontWeight: FontWeight.w600,
  113. ),
  114. maxLines: 1,
  115. overflow: TextOverflow.ellipsis,
  116. ),
  117. const SizedBox(height: 14),
  118. // 数据行
  119. Row(
  120. children: [
  121. Expanded(
  122. child: Column(
  123. crossAxisAlignment: CrossAxisAlignment.start,
  124. children: [
  125. Text(
  126. AppLocalizations.of(context)!.twoWeekReturn,
  127. style: TextStyle(
  128. color: cs.onSurface.withAlpha(120), fontSize: 11),
  129. ),
  130. const SizedBox(height: 4),
  131. Text(
  132. '${trader.dayYield30 >= 0 ? '+' : ''}${formatPrice(trader.dayYield30, decimalPlaces: 2)}%',
  133. style: TextStyle(
  134. color: AppColors.changeColor(trader.dayYield30),
  135. fontSize: 16,
  136. fontWeight: FontWeight.w700,
  137. ),
  138. ),
  139. ],
  140. ),
  141. ),
  142. Expanded(
  143. child: Column(
  144. crossAxisAlignment: CrossAxisAlignment.end,
  145. children: [
  146. Text(
  147. AppLocalizations.of(context)!.twoWeekCopyIncome,
  148. style: TextStyle(
  149. color: cs.onSurface.withAlpha(120), fontSize: 11),
  150. ),
  151. const SizedBox(height: 4),
  152. Text(
  153. trader.profitAmount != 0
  154. ? '${trader.profitAmount >= 0 ? '+' : ''}${formatPrice(trader.profitAmount, decimalPlaces: 2)}'
  155. : '--',
  156. style: TextStyle(
  157. color: trader.profitAmount != 0
  158. ? AppColors.changeColor(trader.profitAmount)
  159. : cs.onSurface,
  160. fontSize: 14,
  161. fontWeight: FontWeight.w600,
  162. ),
  163. ),
  164. ],
  165. ),
  166. ),
  167. ],
  168. ),
  169. const Spacer(),
  170. // 跟单按钮
  171. SizedBox(
  172. width: double.infinity,
  173. height: 40,
  174. child: ElevatedButton(
  175. onPressed: () {
  176. if (!ref.read(isLoggedInProvider)) {
  177. context.push('/login');
  178. return;
  179. }
  180. if (trader.id.isNotEmpty) {
  181. context.push('/trader-detail/${trader.id}');
  182. } else {
  183. context.push('/copy-trading');
  184. }
  185. },
  186. style: ElevatedButton.styleFrom(
  187. backgroundColor: AppColors.brand,
  188. foregroundColor: Colors.black,
  189. shape: RoundedRectangleBorder(
  190. borderRadius: BorderRadius.circular(10),
  191. ),
  192. elevation: 0,
  193. ),
  194. child: Text(
  195. AppLocalizations.of(context)!.copyTrading,
  196. style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
  197. ),
  198. ),
  199. ),
  200. ],
  201. ),
  202. );
  203. }
  204. }
  205. class _AvatarLetter extends StatelessWidget {
  206. const _AvatarLetter({required this.trader});
  207. final TopTrader trader;
  208. @override
  209. Widget build(BuildContext context) {
  210. return Center(
  211. child: Text(
  212. trader.avatarLetter,
  213. style: const TextStyle(
  214. color: Colors.white,
  215. fontSize: 22,
  216. fontWeight: FontWeight.w700,
  217. ),
  218. ),
  219. );
  220. }
  221. }