garage_card.dart 8.8 KB

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