基础组件库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

211 行
7.6 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. double _convert(String val) {
  24. double result = 0.0;
  25. try {
  26. result = double.parse(val);
  27. } catch (e) {
  28. result = 0;
  29. }
  30. return result;
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. /// 空视图
  35. if (EmptyUtil.isEmpty(_model)) {
  36. return Container();
  37. }
  38. return Container(
  39. width: double.infinity,
  40. height: 90,
  41. // color: Colors.red,
  42. padding: EdgeInsets.only(top: _convert(_model?.topMargin), left: 12.5, right: 12.5),
  43. child: _buildMainWidget(context, _model?.moduleType, _model?.listStyle),
  44. );
  45. }
  46. /// 主视图
  47. Widget _buildMainWidget(BuildContext context, String moduleType, List<CustomBannerListStyle> listStyle) {
  48. if (EmptyUtil.isEmpty(listStyle)) return Container();
  49. Widget rlt;
  50. switch (moduleType) {
  51. case 'banner1':
  52. rlt = _buildStyle1(context, listStyle);
  53. break;
  54. case 'banner2':
  55. rlt = _buildStyle2(context, listStyle);
  56. break;
  57. case 'banner3':
  58. rlt = _buildStyle3(context, listStyle);
  59. break;
  60. case 'banner4':
  61. rlt = _buildStyle4(context, listStyle);
  62. break;
  63. case 'banner5':
  64. rlt = _buildStyle5(context, listStyle);
  65. break;
  66. case 'banner6':
  67. rlt = _buildStyle6(context, listStyle);
  68. break;
  69. case 'banner7':
  70. rlt = _buildStyle7(context, listStyle);
  71. break;
  72. case 'banner8':
  73. rlt = _buildStyle8(context, listStyle);
  74. break;
  75. default:
  76. rlt = Container();
  77. break;
  78. }
  79. return rlt;
  80. }
  81. /// 1张图
  82. Widget _buildStyle1(BuildContext context, List<CustomBannerListStyle> listStyle) {
  83. return _buildColumnWidget(context, listStyle);
  84. }
  85. /// 左1右1
  86. Widget _buildStyle2(BuildContext context, List<CustomBannerListStyle> listStyle) {
  87. return _buildColumnWidget(context, listStyle);
  88. }
  89. /// 左1右2
  90. Widget _buildStyle3(BuildContext context, List<CustomBannerListStyle> listStyle) {
  91. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  92. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  93. CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2');
  94. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2)) return Container();
  95. return Row(
  96. children: <Widget>[
  97. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  98. Flexible(
  99. flex: 1,
  100. child: Column(
  101. children: <Widget>[
  102. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  103. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))),
  104. ],
  105. ),
  106. ),
  107. ],
  108. );
  109. }
  110. /// 左2右1
  111. Widget _buildStyle4(BuildContext context, List<CustomBannerListStyle> listStyle) {
  112. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  113. CustomBannerListStyle left2 = listStyle.firstWhere((element) => element.locationType == 'left2');
  114. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  115. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(left2)) return Container();
  116. return Row(
  117. children: <Widget>[
  118. Flexible(
  119. flex: 1,
  120. child: Column(
  121. children: <Widget>[
  122. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  123. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left2), child: CachedNetworkImage(imageUrl: left2?.img ?? ''))),
  124. ],
  125. ),
  126. ),
  127. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  128. ],
  129. );
  130. }
  131. /// 左1右3
  132. Widget _buildStyle5(BuildContext context, List<CustomBannerListStyle> listStyle) {
  133. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  134. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  135. CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2');
  136. CustomBannerListStyle right3 = listStyle.firstWhere((element) => element.locationType == 'right3');
  137. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2) || EmptyUtil.isEmpty(right3)) return Container();
  138. return Row(
  139. children: <Widget>[
  140. Flexible(flex: 4, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  141. Flexible(
  142. flex: 6,
  143. child: Column(
  144. children: <Widget>[
  145. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  146. Row(
  147. children: <Widget>[
  148. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))),
  149. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right3), child: CachedNetworkImage(imageUrl: right3?.img ?? ''))),
  150. ],
  151. )
  152. ],
  153. ),
  154. )
  155. ],
  156. );
  157. }
  158. /// 3列
  159. Widget _buildStyle6(BuildContext context, List<CustomBannerListStyle> listStyle) {
  160. return _buildColumnWidget(context, listStyle);
  161. }
  162. /// 4列
  163. Widget _buildStyle7(BuildContext context, List<CustomBannerListStyle> listStyle) {
  164. return _buildColumnWidget(context, listStyle);
  165. }
  166. /// 5列
  167. Widget _buildStyle8(BuildContext context, List<CustomBannerListStyle> listStyle) {
  168. return _buildColumnWidget(context, listStyle);
  169. }
  170. /// 列的通用
  171. Widget _buildColumnWidget(BuildContext context, List<CustomBannerListStyle> listStyle) {
  172. return Row(
  173. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  174. children: listStyle
  175. .map((e) => Flexible(
  176. flex: 1,
  177. child: GestureDetector(
  178. behavior: HitTestBehavior.opaque,
  179. onTap: () => _itemOnClick(context, e),
  180. child: Container(
  181. height: double.infinity,
  182. width: double.infinity,
  183. child: CachedNetworkImage(
  184. imageUrl: e?.img ?? '',
  185. fit: BoxFit.fitWidth,
  186. )),
  187. ),
  188. ))
  189. .toList(),
  190. );
  191. }
  192. }