challenge_view.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import 'package:flutter/material.dart';
  2. import 'package:intl/intl.dart';
  3. import 'package:japp_flutter/core/models/challenge_model.dart';
  4. import 'package:japp_flutter/features/challenge/view_models/challenge_view_model.dart';
  5. import 'package:provider/provider.dart';
  6. class ChallengeListScreen extends StatefulWidget {
  7. const ChallengeListScreen({super.key});
  8. @override
  9. State<ChallengeListScreen> createState() => _ChallengeListScreenState();
  10. }
  11. class _ChallengeListScreenState extends State<ChallengeListScreen> {
  12. @override
  13. void initState() {
  14. super.initState();
  15. WidgetsBinding.instance.addPostFrameCallback((_) {
  16. Provider.of<ChallengeListViewModel>(context, listen: false).fetchChallenges();
  17. });
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. final viewModel = Provider.of<ChallengeListViewModel>(context);
  22. return Scaffold(
  23. appBar: AppBar(
  24. title: const Text('挑战列表'),
  25. actions: [
  26. IconButton(
  27. icon: const Icon(Icons.refresh),
  28. onPressed: viewModel.fetchChallenges,
  29. ),
  30. ],
  31. ),
  32. body: _buildBody(viewModel),
  33. );
  34. }
  35. Widget _buildBody(ChallengeListViewModel viewModel) {
  36. if (viewModel.isLoading) {
  37. return const Center(child: CircularProgressIndicator());
  38. }
  39. if (viewModel.error.isNotEmpty) {
  40. return Center(
  41. child: Column(
  42. mainAxisAlignment: MainAxisAlignment.center,
  43. children: [
  44. const Icon(Icons.error, color: Colors.red, size: 48),
  45. const SizedBox(height: 16),
  46. Text(viewModel.error, style: const TextStyle(color: Colors.red)),
  47. const SizedBox(height: 16),
  48. ElevatedButton(
  49. onPressed: viewModel.fetchChallenges,
  50. child: const Text('重试'),
  51. ),
  52. ],
  53. ),
  54. );
  55. }
  56. if (viewModel.challenges.isEmpty) {
  57. return const Center(
  58. child: Text('暂无挑战', style: TextStyle(fontSize: 18)),
  59. );
  60. }
  61. return RefreshIndicator(
  62. onRefresh: viewModel.fetchChallenges,
  63. child: ListView.builder(
  64. padding: const EdgeInsets.all(16),
  65. itemCount: viewModel.challenges.length,
  66. itemBuilder: (context, index) {
  67. return ChallengeCard(challenge: viewModel.challenges[index]);
  68. },
  69. ),
  70. );
  71. }
  72. }
  73. class ChallengeCard extends StatelessWidget {
  74. final ChallengeModel challenge;
  75. const ChallengeCard({super.key, required this.challenge});
  76. Color _getDifficultyColor() {
  77. switch (challenge.difficulty) {
  78. case '简单':
  79. return Colors.green;
  80. case '中等':
  81. return Colors.orange;
  82. case '困难':
  83. return Colors.red;
  84. default:
  85. return Colors.grey;
  86. }
  87. }
  88. @override
  89. Widget build(BuildContext context) {
  90. final theme = Theme.of(context); // 获取当前主题
  91. return Card(
  92. elevation: 2,
  93. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
  94. margin: const EdgeInsets.only(bottom: 16),
  95. child: Padding(
  96. padding: const EdgeInsets.all(16),
  97. child: Column(
  98. crossAxisAlignment: CrossAxisAlignment.start,
  99. children: [
  100. Row(
  101. children: [
  102. Expanded(
  103. child: Text(
  104. challenge.title,
  105. style: const TextStyle(
  106. fontSize: 18,
  107. fontWeight: FontWeight.bold,
  108. ),
  109. ),
  110. ),
  111. Chip(
  112. label: Text(
  113. challenge.difficulty,
  114. style: const TextStyle(color: Colors.white),
  115. ),
  116. backgroundColor: _getDifficultyColor(),
  117. ),
  118. ],
  119. ),
  120. const SizedBox(height: 8),
  121. Text(
  122. challenge.description,
  123. style: TextStyle(color: Colors.grey[700], fontSize: 14),
  124. ),
  125. const SizedBox(height: 16),
  126. _buildProgressBar(context),
  127. const SizedBox(height: 12),
  128. Row(
  129. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  130. children: [
  131. _buildInfoChip(
  132. Icons.people,
  133. '${challenge.participants}人参加',
  134. ),
  135. _buildInfoChip(
  136. Icons.calendar_today,
  137. '剩余${challenge.remainingDays}天',
  138. ),
  139. ],
  140. ),
  141. ],
  142. ),
  143. ),
  144. );
  145. }
  146. Widget _buildProgressBar(BuildContext context) {
  147. final theme = Theme.of(context);
  148. final totalDays = challenge.endDate.difference(challenge.startDate).inDays;
  149. final passedDays = totalDays - challenge.remainingDays;
  150. final progress = passedDays / totalDays;
  151. return Column(
  152. crossAxisAlignment: CrossAxisAlignment.start,
  153. children: [
  154. Text(
  155. '${(progress * 100).toStringAsFixed(0)}%',
  156. style: const TextStyle(fontSize: 12),
  157. ),
  158. const SizedBox(height: 4),
  159. LinearProgressIndicator(
  160. value: progress,
  161. backgroundColor: Colors.grey[200],
  162. borderRadius: BorderRadius.circular(10),
  163. minHeight: 8,
  164. color: theme.primaryColor, // 使用主题色
  165. ),
  166. const SizedBox(height: 4),
  167. Row(
  168. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  169. children: [
  170. Text(
  171. DateFormat('MM/dd').format(challenge.startDate),
  172. style: const TextStyle(fontSize: 12),
  173. ),
  174. Text(
  175. DateFormat('MM/dd').format(challenge.endDate),
  176. style: const TextStyle(fontSize: 12),
  177. ),
  178. ],
  179. ),
  180. ],
  181. );
  182. }
  183. Widget _buildInfoChip(IconData icon, String text) {
  184. return Container(
  185. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
  186. decoration: BoxDecoration(
  187. color: Colors.grey[100],
  188. borderRadius: BorderRadius.circular(20),
  189. ),
  190. child: Row(
  191. children: [
  192. Icon(icon, size: 16, color: Colors.grey[600]),
  193. const SizedBox(width: 4),
  194. Text(text, style: TextStyle(color: Colors.grey[700])),
  195. ],
  196. ),
  197. );
  198. }
  199. }