challenge_view_model.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import 'package:flutter/material.dart';
  2. import 'package:japp_flutter/core/models/challenge_model.dart';
  3. class ChallengeListViewModel extends ChangeNotifier {
  4. List<ChallengeModel> _challenges = [];
  5. bool _isLoading = false;
  6. String _error = '';
  7. List<ChallengeModel> get challenges => _challenges;
  8. bool get isLoading => _isLoading;
  9. String get error => _error;
  10. // 模拟数据加载
  11. Future<void> fetchChallenges() async {
  12. try {
  13. _isLoading = true;
  14. notifyListeners();
  15. // 模拟网络请求延迟
  16. await Future.delayed(const Duration(seconds: 1));
  17. // 生成模拟数据
  18. _challenges = [
  19. ChallengeModel(
  20. id: '1',
  21. title: '30天健身挑战',
  22. description: '每天坚持30分钟有氧运动,塑造健康体魄',
  23. startDate: DateTime.now().subtract(const Duration(days: 5)),
  24. endDate: DateTime.now().add(const Duration(days: 25)),
  25. participants: 2541,
  26. completed: false,
  27. difficulty: '中等',
  28. ),
  29. ChallengeModel(
  30. id: '2',
  31. title: '每日阅读打卡',
  32. description: '连续21天每天阅读至少30分钟',
  33. startDate: DateTime.now().subtract(const Duration(days: 10)),
  34. endDate: DateTime.now().add(const Duration(days: 11)),
  35. participants: 4217,
  36. completed: false,
  37. difficulty: '简单',
  38. ),
  39. ChallengeModel(
  40. id: '3',
  41. title: '编程马拉松',
  42. description: '7天内完成一个完整的Flutter应用',
  43. startDate: DateTime.now().subtract(const Duration(days: 2)),
  44. endDate: DateTime.now().add(const Duration(days: 5)),
  45. participants: 876,
  46. completed: false,
  47. difficulty: '困难',
  48. ),
  49. ChallengeModel(
  50. id: '4',
  51. title: '素食30天挑战',
  52. description: '连续30天坚持素食饮食',
  53. startDate: DateTime.now().add(const Duration(days: 2)),
  54. endDate: DateTime.now().add(const Duration(days: 32)),
  55. participants: 1589,
  56. completed: false,
  57. difficulty: '中等',
  58. ),
  59. ];
  60. _error = '';
  61. } catch (e) {
  62. _error = '加载数据失败: $e';
  63. } finally {
  64. _isLoading = false;
  65. notifyListeners();
  66. }
  67. }
  68. }