garage_card.dart 9.1 KB

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