garage_card.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. String 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_avator = card_account?['avator']?? "";
  57. String card_publish_name = card_account?['name']?? "";
  58. // bool card_followed = card_account?['followed']??false;
  59. // String card_content = itemObj.getValue("content", "");
  60. bool card_liked = itemObj.getValue("liked", false);
  61. int card_likes_count = itemObj.getValue("likes_count", 0);
  62. // int card_comments_count = itemObj.getValue("comments_count", 0);
  63. return Column(
  64. children: [
  65. // 图片
  66. Row(
  67. mainAxisAlignment: MainAxisAlignment.center,
  68. crossAxisAlignment: CrossAxisAlignment.center,
  69. children: [
  70. Expanded(
  71. child: ClipRRect(
  72. borderRadius: const BorderRadius.only(topLeft: Radius.circular(8), topRight: Radius.circular(8),),
  73. child: MyLoadImage(
  74. card_img,
  75. width: 166.5,
  76. height: 102.5,
  77. isCircle: false,
  78. fit: BoxFit.cover,
  79. ).onTap(() {
  80. // 点击头像
  81. // onTap?.call();
  82. }),
  83. ),
  84. ),
  85. ],
  86. ),
  87. // 标题
  88. Padding(
  89. padding: const EdgeInsets.only(left: 10, right: 10, top: 12, bottom: 12),
  90. child: Row(
  91. mainAxisAlignment: MainAxisAlignment.center,
  92. children: [
  93. Expanded(
  94. child: MyTextView(
  95. card_title,
  96. maxLines: 1,
  97. isTextEllipsis: true,
  98. textAlign: TextAlign.left,
  99. textColor: context.appColors.textBlack,
  100. fontSize: 16,
  101. isFontRegular: true,
  102. ),
  103. ),
  104. ],
  105. ),
  106. ),
  107. // 价格 及 收藏
  108. Padding(
  109. padding: const EdgeInsets.only(left: 10, right: 10,top: 10, bottom: 10),
  110. child: Row(
  111. mainAxisAlignment: MainAxisAlignment.spaceAround,
  112. crossAxisAlignment: CrossAxisAlignment.center,
  113. children: [
  114. Expanded(
  115. child: MyTextView(
  116. card_price,
  117. maxLines: 1,
  118. isTextEllipsis: true,
  119. textAlign: TextAlign.start,
  120. textColor: ColorUtils.string2Color('#4161D0'),
  121. fontSize: 18,
  122. isFontMedium: true,
  123. ),
  124. ),
  125. // 动态的 收藏数
  126. CollectionWidget(
  127. collectionNum: card_likes_count,
  128. isCollection: card_liked,
  129. onClickColleciotn: onClickColleciotn,
  130. ),
  131. ],
  132. ),
  133. ),
  134. // 发布人信息
  135. Expanded(
  136. child: Padding(
  137. padding: EdgeInsets.only(left: (useType == GarageCardUseType.forSale || useType == GarageCardUseType.forRent)?10:0, right: 10),
  138. child: Row(
  139. mainAxisAlignment: MainAxisAlignment.spaceAround,
  140. crossAxisAlignment: CrossAxisAlignment.center,
  141. children: [
  142. if (useType == GarageCardUseType.forSale || useType == GarageCardUseType.forRent)
  143. MyLoadImage(
  144. card_avator,
  145. width: 30,
  146. height: 30,
  147. isCircle: true,
  148. )
  149. else
  150. const SizedBox.shrink(),
  151. Expanded(
  152. child: Column(
  153. mainAxisAlignment: MainAxisAlignment.center,
  154. crossAxisAlignment: CrossAxisAlignment.start,
  155. mainAxisSize: MainAxisSize.max,
  156. children: [
  157. if (useType == GarageCardUseType.forSale || useType == GarageCardUseType.forRent)
  158. MyTextView(
  159. card_publish_name,
  160. maxLines: 1,
  161. isTextEllipsis: true,
  162. textAlign: TextAlign.start,
  163. marginLeft: 13,
  164. fontSize: 12,
  165. textColor: ColorUtils.string2Color('#2956B7'),
  166. isFontRegular: true,
  167. )
  168. else
  169. const SizedBox.shrink(),
  170. MyTextView(
  171. card_created_at,
  172. maxLines: 1,
  173. isTextEllipsis: true,
  174. textAlign: TextAlign.start,
  175. marginLeft: 13,
  176. marginTop: 5,
  177. fontSize: 10,
  178. textColor: context.appColors.textBlack,
  179. isFontRegular: true,
  180. ),
  181. ]
  182. )
  183. ),
  184. ]
  185. )
  186. ),
  187. )
  188. ],
  189. );
  190. }
  191. }
  192. class CollectionWidget extends HookConsumerWidget {
  193. int collectionNum = 0;
  194. bool isCollection = false;
  195. final Function(dynamic)? onClickColleciotn;
  196. CollectionWidget({
  197. Key? key,
  198. required this.collectionNum,
  199. required this.isCollection,
  200. this.onClickColleciotn,
  201. }) : super(key: key);
  202. @override
  203. Widget build(BuildContext context, WidgetRef ref) {
  204. final collectionNumState = useState(collectionNum);
  205. final isCollectionState = useState(isCollection);
  206. return Container(
  207. width: 60,
  208. height: 30,
  209. alignment: Alignment.center,
  210. // decoration: BoxDecoration(
  211. // color: ColorUtils.string2Color('#E5E5E5'),
  212. // borderRadius: BorderRadius.circular(15),
  213. // ),
  214. child: Row(
  215. mainAxisAlignment: MainAxisAlignment.center,
  216. children: [
  217. MyTextView(
  218. '${collectionNumState.value}',
  219. textColor: context.appColors.textBlack,
  220. fontSize: 14,
  221. isFontRegular: true,
  222. marginRight: 7,
  223. ),
  224. MyLoadImage(
  225. isCollectionState.value? Assets.communityLikeActive: Assets.communityLike,
  226. width: 15,
  227. height: 14,
  228. )
  229. ]
  230. // 点击 收餐/取消收藏
  231. ).onTap(() async{
  232. // Log.d("点击了收藏按钮 ${isCollectionState.value}");
  233. // ToastEngine.show("点击了收藏按钮 ${isCollectionState.value}");
  234. bool result = await onClickColleciotn?.call(isCollectionState.value);
  235. if(result){
  236. isCollectionState.value = !isCollectionState.value;
  237. collectionNumState.value = (collectionNumState.value + (isCollectionState.value? 1: -1))<0? 0: (collectionNumState.value + (isCollectionState.value? 1: -1));
  238. if(isCollectionState.value){
  239. ToastEngine.show("Collect Success");
  240. }else {
  241. ToastEngine.show("Cancel Collect Success");
  242. }
  243. }
  244. })
  245. );
  246. }
  247. }