基础组件库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

248 Zeilen
9.3 KiB

  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:zhiying_base_widget/widgets/custom/banner/model/custom_banner_model.dart';
  4. import 'package:zhiying_comm/zhiying_comm.dart';
  5. ///
  6. /// 通用的BannerWidget
  7. ///
  8. class CustomBannerWidget extends StatelessWidget {
  9. final Map<String, dynamic> data;
  10. CustomBannerModel _model;
  11. CustomBannerWidget(this.data, {Key key}) : super(key: key) {
  12. try {
  13. _model = CustomBannerModel.fromJson(jsonDecode(data['data']));
  14. } catch (e, s) {
  15. Logger.warn(e, s);
  16. }
  17. }
  18. /// 点击事件
  19. void _itemOnClick(BuildContext context, CustomBannerListStyle model) {
  20. print('${model?.skipIdentifier}');
  21. RouterUtil.route(model, model.toJson(), context);
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. /// 空视图
  26. if (EmptyUtil.isEmpty(_model)) {
  27. return Container();
  28. }
  29. return Container(
  30. width: double.infinity,
  31. // height: 98,
  32. margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(_model?.topMargin)),
  33. padding: EdgeInsets.symmetric(horizontal: ParseUtil.stringParseDouble(_model?.leftRightMargin, defVal: 12.5)),
  34. decoration: BoxDecoration(
  35. // color: Colors.red,
  36. borderRadius: BorderRadius.only(
  37. bottomLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomLeftRadius)),
  38. bottomRight: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomRightRadius)),
  39. topLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.topLeftRadius)),
  40. topRight: Radius.circular(ParseUtil.stringParseDouble(_model?.topRightRadius)),
  41. )),
  42. child: _buildMainWidget(context, _model?.moduleType, _model?.listStyle),
  43. );
  44. }
  45. /// 主视图
  46. Widget _buildMainWidget(BuildContext context, String moduleType, List<CustomBannerListStyle> listStyle) {
  47. if (EmptyUtil.isEmpty(listStyle)) return Container();
  48. Widget rlt;
  49. switch (moduleType) {
  50. case 'banner1':
  51. rlt = _buildStyle1(context, listStyle);
  52. break;
  53. case 'banner2':
  54. rlt = _buildStyle2(context, listStyle);
  55. break;
  56. case 'banner3':
  57. rlt = _buildStyle3(context, listStyle);
  58. break;
  59. case 'banner4':
  60. rlt = _buildStyle4(context, listStyle);
  61. break;
  62. case 'banner5':
  63. rlt = _buildStyle5(context, listStyle);
  64. break;
  65. case 'banner6':
  66. rlt = _buildStyle6(context, listStyle);
  67. break;
  68. case 'banner7':
  69. rlt = _buildStyle7(context, listStyle);
  70. break;
  71. case 'banner8':
  72. rlt = _buildStyle8(context, listStyle);
  73. break;
  74. default:
  75. rlt = Container();
  76. break;
  77. }
  78. return rlt;
  79. }
  80. /// 1张图
  81. Widget _buildStyle1(BuildContext context, List<CustomBannerListStyle> listStyle) {
  82. return _buildColumnWidget(context, listStyle);
  83. }
  84. /// 左1右1
  85. Widget _buildStyle2(BuildContext context, List<CustomBannerListStyle> listStyle) {
  86. return _buildColumnWidget(context, listStyle);
  87. }
  88. /// 左1右2
  89. Widget _buildStyle3(BuildContext context, List<CustomBannerListStyle> listStyle) {
  90. const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  91. // if(!inProduction )return Container();
  92. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  93. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  94. CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2');
  95. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2)) return Container();
  96. return Row(
  97. children: <Widget>[
  98. // Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  99. Flexible(flex: 1, child: _buildSingleWidthWidget(context, left1)),
  100. Flexible(
  101. flex: 1,
  102. child: Visibility(
  103. replacement: Container(),
  104. visible: inProduction,
  105. child: Column(
  106. children: <Widget>[
  107. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  108. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))),
  109. ],
  110. ),
  111. ),
  112. ),
  113. ],
  114. );
  115. }
  116. /// 左2右1
  117. Widget _buildStyle4(BuildContext context, List<CustomBannerListStyle> listStyle) {
  118. const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  119. // if(!inProduction )return Container();
  120. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  121. CustomBannerListStyle left2 = listStyle.firstWhere((element) => element.locationType == 'left2');
  122. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  123. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(left2)) return Container();
  124. return Row(
  125. children: <Widget>[
  126. Flexible(
  127. flex: 1,
  128. child: Visibility(
  129. replacement: Container(),
  130. visible: inProduction,
  131. child: Column(
  132. children: <Widget>[
  133. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  134. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left2), child: CachedNetworkImage(imageUrl: left2?.img ?? ''))),
  135. ],
  136. ),
  137. ),
  138. ),
  139. // Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  140. Flexible(flex: 1, child: _buildSingleWidthWidget(context, right1)),
  141. ],
  142. );
  143. }
  144. /// 左1右3
  145. Widget _buildStyle5(BuildContext context, List<CustomBannerListStyle> listStyle) {
  146. const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  147. // if(!inProduction )return Container();
  148. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  149. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  150. CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2');
  151. CustomBannerListStyle right3 = listStyle.firstWhere((element) => element.locationType == 'right3');
  152. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2) || EmptyUtil.isEmpty(right3)) return Container();
  153. return Row(
  154. children: <Widget>[
  155. // Flexible(flex: 4, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  156. Flexible(flex: 4, child: _buildSingleWidthWidget(context, left1)),
  157. Flexible(
  158. flex: 6,
  159. child: Visibility(
  160. replacement: Container(),
  161. visible: inProduction,
  162. child: Column(
  163. children: <Widget>[
  164. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  165. Row(
  166. children: <Widget>[
  167. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))),
  168. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right3), child: CachedNetworkImage(imageUrl: right3?.img ?? ''))),
  169. ],
  170. )
  171. ],
  172. ),
  173. ),
  174. )
  175. ],
  176. );
  177. }
  178. /// 3列
  179. Widget _buildStyle6(BuildContext context, List<CustomBannerListStyle> listStyle) {
  180. return _buildColumnWidget(context, listStyle);
  181. }
  182. /// 4列
  183. Widget _buildStyle7(BuildContext context, List<CustomBannerListStyle> listStyle) {
  184. return _buildColumnWidget(context, listStyle);
  185. }
  186. /// 5列
  187. Widget _buildStyle8(BuildContext context, List<CustomBannerListStyle> listStyle) {
  188. return _buildColumnWidget(context, listStyle);
  189. }
  190. /// 列的通用
  191. Widget _buildColumnWidget(BuildContext context, List<CustomBannerListStyle> listStyle) {
  192. return Row(
  193. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  194. children: listStyle
  195. .map((e) => Flexible(
  196. flex: 1,
  197. child: GestureDetector(
  198. behavior: HitTestBehavior.opaque,
  199. onTap: () => _itemOnClick(context, e),
  200. child: Container(
  201. width: double.infinity,
  202. child: CachedNetworkImage(
  203. imageUrl: e?.img ?? '',
  204. )),
  205. ),
  206. ))
  207. .toList(),
  208. );
  209. }
  210. /// 单个
  211. Widget _buildSingleWidthWidget(BuildContext context, CustomBannerListStyle listStyle){
  212. return GestureDetector(
  213. behavior: HitTestBehavior.opaque,
  214. onTap: ()=> _itemOnClick(context, listStyle),
  215. child: Container(
  216. width: double.infinity,
  217. child: CachedNetworkImage(
  218. imageUrl: listStyle?.img ?? '',
  219. ),
  220. ),
  221. );
  222. }
  223. }