garage_card.dart 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:cs_resources/theme/app_colors_theme.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/widgets.dart';
  5. import 'package:flutter_hooks/flutter_hooks.dart';
  6. import 'package:hooks_riverpod/hooks_riverpod.dart';
  7. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  8. import 'package:shared/utils/color_utils.dart';
  9. import 'package:shared/utils/ext_dart.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. import 'package:widgets/ext/ex_widget.dart';
  12. import 'package:widgets/my_load_image.dart';
  13. import 'package:widgets/my_text_view.dart';
  14. import '../modules/garage/for_sale/for_sale_vm.dart';
  15. // 'id':1,
  16. // 'goods_img': 'https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500',
  17. // 'title': 'Electronic keyboard',
  18. // 'price': '\$66',
  19. // 'isCollection': true,
  20. // 'collection_num': '12',
  21. // 'publisher': 'William Jefferson',
  22. // 'publish_time': 'June 17,2016 at 7:23 p.m.',
  23. // 'publisher_avator': Assets.communityCamera,'
  24. // 定义一个 使用场景的 枚举
  25. enum GarageCardUseType {
  26. // 默认
  27. forSale,
  28. forRent,
  29. myPostsForSale,
  30. myPostsForRent,
  31. }
  32. class GarageCard extends StatelessWidget {
  33. GarageCardUseType? useType;
  34. Map<String, dynamic> itemObj;
  35. double? cardHeight;
  36. final Function()? onTap;
  37. final Function(dynamic)? onClickColleciotn;
  38. GarageCard({
  39. Key? key,
  40. this.useType = GarageCardUseType.forSale,
  41. required this.itemObj,
  42. this.onTap,
  43. this.onClickColleciotn,
  44. double? cardHeight,
  45. }) : super(key: key) {
  46. this.cardHeight ??= 214;
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. List? card_resources = itemObj.getValue<List>("resources", []) ?? [];
  51. String card_img = card_resources.length > 0 ? card_resources[0] : "";
  52. String card_title = itemObj.getValue("title", "");
  53. int card_price = itemObj.getValue("price", "");
  54. String card_created_at = itemObj.getValue("created_at", "");
  55. Map<String, dynamic>? card_account = itemObj.getValue<Map<String, dynamic>>("account", null);
  56. String card_avatar = card_account?['avatar'] ?? "";
  57. String card_publish_name = card_account?['name'] ?? "";
  58. bool card_liked = itemObj.getValue("liked", false);
  59. int card_likes_count = itemObj.getValue("likes_count", 0);
  60. return Column(
  61. mainAxisSize: MainAxisSize.max,
  62. children: [
  63. // 图片
  64. Row(
  65. mainAxisAlignment: MainAxisAlignment.center,
  66. crossAxisAlignment: CrossAxisAlignment.center,
  67. children: [
  68. Expanded(
  69. child: ClipRRect(
  70. borderRadius: const BorderRadius.only(
  71. topLeft: Radius.circular(8),
  72. topRight: Radius.circular(8),
  73. ),
  74. child: MyLoadImage(
  75. card_img,
  76. width: 166.5,
  77. height: 102.5.ap,
  78. isCircle: false,
  79. fit: BoxFit.cover,
  80. ),
  81. ),
  82. ),
  83. ],
  84. ),
  85. // 标题
  86. Padding(
  87. padding: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 5),
  88. child: Row(
  89. mainAxisAlignment: MainAxisAlignment.center,
  90. children: [
  91. Expanded(
  92. child: MyTextView(
  93. card_title,
  94. maxLines: 1,
  95. isTextEllipsis: true,
  96. textAlign: TextAlign.left,
  97. textColor: context.appColors.textBlack,
  98. fontSize: 16,
  99. isFontRegular: true,
  100. ),
  101. ),
  102. ],
  103. ),
  104. ),
  105. // 价格 及 收藏
  106. Padding(
  107. padding: const EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 0),
  108. child: Row(
  109. mainAxisAlignment: MainAxisAlignment.spaceAround,
  110. crossAxisAlignment: CrossAxisAlignment.center,
  111. children: [
  112. Expanded(
  113. child: MyTextView(
  114. '$card_price',
  115. maxLines: 1,
  116. isTextEllipsis: true,
  117. textAlign: TextAlign.start,
  118. textColor: ColorUtils.string2Color('#4161D0'),
  119. fontSize: 18,
  120. isFontMedium: true,
  121. ),
  122. ),
  123. // 动态的 收藏数
  124. CollectionWidget(
  125. collectionNum: card_likes_count,
  126. isCollection: card_liked,
  127. onClickColleciotn: onClickColleciotn,
  128. ),
  129. ],
  130. ),
  131. ),
  132. // 发布人信息
  133. Expanded(
  134. child: Padding(
  135. padding: EdgeInsets.only(
  136. left: (useType == GarageCardUseType.forSale || useType == GarageCardUseType.forRent) ? 10 : 0,
  137. right: 10,
  138. bottom: (useType == GarageCardUseType.forSale || useType == GarageCardUseType.forRent) ? 10.5 :0,
  139. ),
  140. child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.center, children: [
  141. if (useType == GarageCardUseType.forSale || useType == GarageCardUseType.forRent)
  142. MyLoadImage(
  143. card_avatar,
  144. width: 30,
  145. height: 30,
  146. isCircle: true,
  147. )
  148. else
  149. const SizedBox.shrink(),
  150. Expanded(
  151. child: Column(
  152. mainAxisAlignment: MainAxisAlignment.center,
  153. crossAxisAlignment: CrossAxisAlignment.start,
  154. mainAxisSize: MainAxisSize.min,
  155. children: [
  156. if (useType == GarageCardUseType.forSale || useType == GarageCardUseType.forRent)
  157. MyTextView(
  158. card_publish_name,
  159. maxLines: 1,
  160. isTextEllipsis: true,
  161. textAlign: TextAlign.start,
  162. marginLeft: 13,
  163. fontSize: 12,
  164. textColor: ColorUtils.string2Color('#2956B7'),
  165. isFontRegular: true,
  166. )
  167. else
  168. const SizedBox.shrink(),
  169. MyTextView(
  170. card_created_at,
  171. maxLines: 1,
  172. isTextEllipsis: true,
  173. textAlign: TextAlign.start,
  174. marginLeft: 13,
  175. marginTop: 2.5,
  176. marginBottom: 2.5,
  177. fontSize: 10,
  178. textColor: context.appColors.textBlack,
  179. isFontRegular: true,
  180. ),
  181. ])),
  182. ])),
  183. )
  184. ],
  185. ).constrained(height: cardHeight);
  186. }
  187. }
  188. class CollectionWidget extends HookConsumerWidget {
  189. int collectionNum = 0;
  190. bool isCollection = false;
  191. final Function(dynamic)? onClickColleciotn;
  192. CollectionWidget({
  193. Key? key,
  194. required this.collectionNum,
  195. required this.isCollection,
  196. this.onClickColleciotn,
  197. }) : super(key: key);
  198. @override
  199. Widget build(BuildContext context, WidgetRef ref) {
  200. final collectionNumState = useState(collectionNum);
  201. final isCollectionState = useState(isCollection);
  202. return Container(
  203. width: 60,
  204. height: 30,
  205. alignment: Alignment.center,
  206. // decoration: BoxDecoration(
  207. // color: ColorUtils.string2Color('#E5E5E5'),
  208. // borderRadius: BorderRadius.circular(15),
  209. // ),
  210. child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
  211. MyTextView(
  212. '${collectionNumState.value}',
  213. textColor: context.appColors.textBlack,
  214. fontSize: 14,
  215. isFontRegular: true,
  216. marginRight: 7,
  217. ),
  218. MyLoadImage(
  219. isCollectionState.value ? Assets.communityLikeActive : Assets.communityLike,
  220. width: 14.1,
  221. height: 14,
  222. )
  223. ]
  224. // 点击 收餐/取消收藏
  225. ).onTap(() async {
  226. // Log.d("点击了收藏按钮 ${isCollectionState.value}");
  227. // ToastEngine.show("点击了收藏按钮 ${isCollectionState.value}");
  228. final result = await onClickColleciotn?.call(isCollectionState.value);
  229. if (result != null && result) {
  230. isCollectionState.value = !isCollectionState.value;
  231. collectionNumState.value =
  232. (collectionNumState.value + (isCollectionState.value ? 1 : -1)) < 0 ? 0 : (collectionNumState.value + (isCollectionState.value ? 1 : -1));
  233. if (isCollectionState.value) {
  234. // ToastEngine.show("Collect Success");
  235. } else {
  236. // ToastEngine.show("Cancel Collect Success");
  237. }
  238. }
  239. }));
  240. }
  241. }