product_item.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'package:flutter/material.dart';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:twong/utils/index.dart';
  4. import 'package:twong/router/index.dart';
  5. import 'package:twong/models/index.dart';
  6. class ProductItem extends StatelessWidget {
  7. final Product data;
  8. final bool _isVip =
  9. Cache.user?.vip_level != null && Cache.user.vip_level > 0;
  10. ProductItem(this.data);
  11. @override
  12. Widget build(BuildContext context) {
  13. return GestureDetector(
  14. onTap: () {
  15. Navigator.pushNamed(context,
  16. RouteNames.productDetails, arguments: data.id);
  17. },
  18. child: Container(
  19. height: 118.px,
  20. padding: EdgeInsets.all(6.px),
  21. margin: EdgeInsets.only(top: 6.px, left: 8.px, right: 8.px),
  22. decoration: BoxDecoration(
  23. color: Colors.white,
  24. borderRadius: BorderRadius.circular(6.px)
  25. ),
  26. child: Row(
  27. children: <Widget>[
  28. CachedNetworkImage(
  29. width: 118.px,
  30. height: 118.px,
  31. fit: BoxFit.scaleDown,
  32. imageUrl: data.image,
  33. imageBuilder: (BuildContext context,
  34. ImageProvider<dynamic> provider) {
  35. return Container(
  36. decoration: BoxDecoration(
  37. image: DecorationImage(
  38. image: provider,
  39. fit: BoxFit.cover
  40. ),
  41. ),
  42. );
  43. },
  44. placeholder: (BuildContext context, String str) {
  45. return Center(child: Utils.loadingWidget);
  46. },
  47. ),
  48. Expanded(child: Container(
  49. margin: EdgeInsets.only(left: 10.px, right: 10.px),
  50. child: Column(
  51. crossAxisAlignment: CrossAxisAlignment.start,
  52. children: [
  53. Container(
  54. height: 32.px,
  55. child: Text(data.store_name,
  56. style: TextStyle(
  57. fontWeight: FontWeight.bold, fontSize: 12.px)),
  58. ),
  59. Container(child: Row(
  60. children: [
  61. Icon(IconFonts.done_outline, color: Colors.grey, size: 12.px),
  62. Text(" 官方直营 信誉保障",
  63. style: TextStyle(color: Colors.grey, fontSize: 10.px)),
  64. ],
  65. )),
  66. Spacer(),
  67. Row(
  68. crossAxisAlignment: CrossAxisAlignment.end,
  69. children: [
  70. Expanded(child: _genPrice(data)),
  71. Text("最近销量: ${data.sales}", style:
  72. TextStyle(color: Colors.grey, fontSize: 10.px))
  73. ],)
  74. ],
  75. )),
  76. ),
  77. ],
  78. ),
  79. ),
  80. );
  81. }
  82. Widget _getVipPrice(dynamic price, { Color color, Widget leading }) {
  83. return Row(
  84. crossAxisAlignment: CrossAxisAlignment.center,
  85. children: [
  86. Container(
  87. child: leading,
  88. margin: EdgeInsets.only(right: 4.px),
  89. ),
  90. RichText(
  91. text: TextSpan(
  92. text: I18n.$, style: TextStyle(color: color, fontSize: 10.px),
  93. children: [
  94. TextSpan(text: Utils.formatRMB(price),
  95. style: TextStyle(color: color, fontSize: 12.px),
  96. )
  97. ])
  98. ),
  99. ]);
  100. }
  101. Widget _genPrice(Product data) {
  102. if (_isVip) {
  103. return Container(
  104. margin: EdgeInsets.only(left: 6.px),
  105. child: Column(
  106. children: [
  107. _getVipPrice(data.vip_price, color: Colors.red, leading: Text("会:",
  108. style: TextStyle(fontSize: 12.px))),
  109. _getVipPrice(data.price, color: Colors.black87, leading: Text("购:",
  110. style: TextStyle(fontSize: 12.px))),
  111. _getVipPrice(num.parse(data.price) -
  112. (data.vip_price == null ? 0 : num.parse(data.vip_price)),
  113. color: Colors.green, leading: Text("赚:",
  114. style: TextStyle(fontSize: 12.px))),
  115. ],
  116. ),
  117. );
  118. }
  119. return Container(
  120. child: RichText(
  121. text: TextSpan(
  122. text: I18n.$,
  123. style: TextStyle(color: Colors.red, fontSize: 12.px),
  124. children: [
  125. TextSpan(text: data.price,
  126. style: TextStyle(color: Colors.red, fontSize: 16.px),
  127. children: [
  128. TextSpan(text: " ${I18n.$}",
  129. style: TextStyle(
  130. color: Colors.grey, fontSize: 12.px)
  131. ),
  132. TextSpan(text: Utils.formatRMB(data.price),
  133. style: TextStyle(
  134. color: Colors.grey, fontSize: 14.px,
  135. decoration: TextDecoration.lineThrough)
  136. )
  137. ])
  138. ])
  139. ));
  140. }
  141. }