topic_item_model.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import 'package:news_app/extension/base.dart';
  2. /// @author: bo.zeng
  3. /// @email: cnhbwds@gmail.com
  4. /// @date: 2025 2025/4/9 16:00
  5. /// @description:
  6. class TopicItemModel {
  7. final List<TopicRecordModel>? records;
  8. final int? total;
  9. final int? size;
  10. final int? current;
  11. final int? pages;
  12. TopicItemModel({
  13. this.records,
  14. this.total,
  15. this.size,
  16. this.current,
  17. this.pages,
  18. });
  19. TopicItemModel copyWith({
  20. int? total,
  21. int? size,
  22. int? current,
  23. int? pages,
  24. List<TopicRecordModel>? records,
  25. }) {
  26. return TopicItemModel(
  27. total: total ?? this.total,
  28. size: size ?? this.size,
  29. current: current ?? this.current,
  30. pages: pages ?? this.pages,
  31. records: records ?? this.records,
  32. );
  33. }
  34. factory TopicItemModel.fromJson(Map<String, dynamic> json) {
  35. return TopicItemModel(
  36. records:
  37. (json['records'] as List<dynamic>)
  38. .map((e) => TopicRecordModel.fromJson(e as Map<String, dynamic>))
  39. .toList(),
  40. total: json['total'].toString().convertInt,
  41. size: json['size'].toString().convertInt,
  42. current: json['current'].toString().convertInt,
  43. pages: json['pages'].toString().convertInt,
  44. );
  45. }
  46. }
  47. class TopicRecordModel {
  48. final Member? member;
  49. final String? contentId;
  50. final String? content;
  51. final List<Resource>? resourceList;
  52. final List<dynamic>? commentList; // 可以根据实际再建模型
  53. final int? likeNum;
  54. final int? commentNum;
  55. final Topic? topic;
  56. bool? isLiked;
  57. bool? isFavorite;
  58. int? favoriteCount;
  59. final String? shareUrl;
  60. final String? shareDesc;
  61. TopicRecordModel({
  62. this.member,
  63. this.contentId,
  64. this.content,
  65. this.resourceList,
  66. this.commentList,
  67. this.likeNum,
  68. this.commentNum,
  69. this.topic,
  70. this.isLiked,
  71. this.isFavorite,
  72. this.favoriteCount,
  73. this.shareUrl,
  74. this.shareDesc,
  75. });
  76. factory TopicRecordModel.fromJson(Map<String, dynamic> json) {
  77. return TopicRecordModel(
  78. member: Member.fromJson(json['member']),
  79. contentId: json['contentId'] ?? '',
  80. content: json['content'] ?? '',
  81. resourceList:
  82. (json['resourceList'] as List<dynamic>)
  83. .map((e) => Resource.fromJson(e as Map<String, dynamic>))
  84. .toList(),
  85. commentList: json['commentList'] ?? [],
  86. likeNum: json['likeNum'].toString().convertInt,
  87. commentNum: json['commentNum'].toString().convertInt,
  88. topic: Topic.fromJson(json['topic']),
  89. isLiked: json['isLiked'] ?? false,
  90. isFavorite: json['isFavorite'] ?? false,
  91. favoriteCount: json['favoriteCount'].toString().convertInt,
  92. shareUrl: json['shareUrl'] ?? '',
  93. shareDesc: json['shareDesc'] ?? ''
  94. );
  95. }
  96. //生成copyWith方法
  97. TopicRecordModel copyWith({
  98. Member? member,
  99. String? contentId,
  100. String? content,
  101. List<Resource>? resourceList,
  102. List<dynamic>? commentList,
  103. int? likeNum,
  104. int? commentNum,
  105. Topic? topic,
  106. bool? isLiked,
  107. bool? isFavorite,
  108. int? favoriteCount,
  109. String? shareUrl,
  110. String? shareDesc,
  111. }){
  112. return TopicRecordModel(
  113. member: member ?? this.member,
  114. contentId: contentId ?? this.contentId,
  115. content: content ?? this.content,
  116. resourceList: resourceList ?? this.resourceList,
  117. commentList: commentList ?? this.commentList,
  118. likeNum: likeNum ?? this.likeNum,
  119. commentNum: commentNum ?? this.commentNum,
  120. topic: topic ?? this.topic,
  121. isLiked: isLiked ?? this.isLiked,
  122. isFavorite: isFavorite ?? this.isFavorite,
  123. favoriteCount: favoriteCount ?? this.favoriteCount,
  124. shareUrl:shareUrl ?? this.shareUrl,
  125. shareDesc:shareDesc ?? this.shareDesc,
  126. );
  127. }
  128. }
  129. class Member {
  130. String? memberId;
  131. String? avatar;
  132. String? nickname;
  133. Member({this.memberId, this.avatar, this.nickname});
  134. factory Member.fromJson(Map<String, dynamic> json) {
  135. return Member(
  136. memberId: json['memberId'] ?? '',
  137. avatar: json['avatar'] ?? '',
  138. nickname: json['nickname'] ?? '',
  139. );
  140. }
  141. }
  142. class Topic {
  143. final String content;
  144. final String contentId;
  145. Topic({required this.content, required this.contentId});
  146. factory Topic.fromJson(Map<String, dynamic> json) {
  147. return Topic(
  148. content: json['content'] ?? '',
  149. contentId: json['contentId'] ?? '',
  150. );
  151. }
  152. }
  153. class Resource {
  154. final String type;
  155. final String url;
  156. Resource({required this.type, required this.url});
  157. factory Resource.fromJson(Map<String, dynamic> json) {
  158. return Resource(type: json['type'] ?? '', url: json['url'] ?? '');
  159. }
  160. }