product_item.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import 'package:flutter/material.dart';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter_html/style.dart';
  4. import 'package:twong/router/index.dart';
  5. import 'package:twong/config/style.dart';
  6. import 'package:twong/models/index.dart';
  7. import 'package:twong/utils/index.dart';
  8. class ProductItem extends StatelessWidget {
  9. final int _index;
  10. final List<Product> _data;
  11. bool _isVip = false;
  12. ProductItem(this._index, this._data, {Key key}) :super(key: key);
  13. @override
  14. Widget build(BuildContext context) {
  15. _isVip = Cache.user?.vip_level != null && Cache.user.vip_level > 0;
  16. var data = _data[_index];
  17. return GestureDetector(
  18. onTap: () {
  19. Navigator.pushNamed(context, RouteNames.productDetails, arguments: data.id);
  20. },
  21. child: Container(
  22. width: 150.px,
  23. height: 150.px,
  24. color: Colors.white,
  25. margin: EdgeInsets.only(bottom: 10),
  26. child: Row(
  27. children: <Widget>[
  28. CachedNetworkImage(
  29. width: 150.px,
  30. height: 150.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 Container(decoration: BoxDecoration(
  46. color: Colors.grey
  47. ));
  48. },
  49. ),
  50. Expanded(child: Container(
  51. margin: EdgeInsets.only(left: 10.px, right: 10.px),
  52. child: Column(
  53. crossAxisAlignment: CrossAxisAlignment.start,
  54. children: [
  55. Container(
  56. height: 40.px,
  57. child: Text(data.store_name, style: TextStyle(fontWeight: FontWeight.bold)),
  58. ),
  59. Container(margin: EdgeInsets.only(top: 4.px), child: Text("官方正品 信誉保障", style: TextStyle(color: Colors.grey, fontSize: 12))),
  60. Spacer(),
  61. _genPrice(data)
  62. ],
  63. )),
  64. ),
  65. ],
  66. ),
  67. ),
  68. );
  69. }
  70. Widget _getVipPrice(dynamic price, { Color color, Widget leading }) {
  71. return Row(
  72. crossAxisAlignment: CrossAxisAlignment.center,
  73. children: [
  74. Container(
  75. child: leading,
  76. padding: EdgeInsets.only(right: 4.px),
  77. ),
  78. RichText(
  79. text: TextSpan(
  80. text: I18n.$, style: TextStyle(color: color, fontSize: 10.px),
  81. children: [
  82. TextSpan(text: price,
  83. style: TextStyle(color: color, fontSize: 14.px),
  84. )
  85. ]
  86. )
  87. ),
  88. ],
  89. );
  90. }
  91. Widget _genPrice(Product data) {
  92. if (_isVip) {
  93. return Container(
  94. margin: EdgeInsets.only(bottom: 4.px, left: 6.px),
  95. child: Column(
  96. children: [
  97. _getVipPrice(data.price, color: Colors.red, leading: Text("会:")),
  98. _getVipPrice(data.price, color: Colors.black87, leading: Text("购:")),
  99. _getVipPrice(data.price, color: Colors.green, leading: Text("赚:")),
  100. ],
  101. ),
  102. );
  103. }
  104. return Container(
  105. margin: EdgeInsets.only(bottom: 4.px, left: 6.px),
  106. child: RichText(
  107. text: TextSpan(
  108. text: I18n.$, style: TextStyle(color: Colors.red, fontSize: 14.px),
  109. children: [
  110. TextSpan(text: data.price,
  111. style: TextStyle(color: Colors.red, fontSize: 20.px),
  112. children: [
  113. TextSpan(text: " "),
  114. TextSpan(text: Utils.formatRMB(data.price, show: true),
  115. style: TextStyle(color: Colors.grey, fontSize: 14.px,
  116. decoration: TextDecoration.lineThrough)
  117. )
  118. ]
  119. )
  120. ]
  121. )
  122. )
  123. );
  124. }
  125. }