基础组件库
 
 
 
 
 

231 rinda
8.7 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. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  91. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  92. CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2');
  93. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2)) return Container();
  94. return IntrinsicHeight(
  95. child: Row(
  96. children: <Widget>[
  97. // Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  98. Flexible(flex: 1, child: _buildSingleWidthWidget(context, left1)),
  99. Flexible(
  100. flex: 1,
  101. child: Column(
  102. children: <Widget>[
  103. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  104. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))),
  105. ],
  106. ),
  107. ),
  108. ],
  109. ),
  110. );
  111. }
  112. /// 左2右1
  113. Widget _buildStyle4(BuildContext context, List<CustomBannerListStyle> listStyle) {
  114. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  115. CustomBannerListStyle left2 = listStyle.firstWhere((element) => element.locationType == 'left2');
  116. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  117. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(left2)) return Container();
  118. return IntrinsicHeight(
  119. child: Row(
  120. children: <Widget>[
  121. Flexible(
  122. flex: 1,
  123. child: Column(
  124. children: <Widget>[
  125. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  126. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left2), child: CachedNetworkImage(imageUrl: left2?.img ?? ''))),
  127. ],
  128. ),
  129. ),
  130. // Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  131. Flexible(flex: 1, child: _buildSingleWidthWidget(context, right1)),
  132. ],
  133. ),
  134. );
  135. }
  136. /// 左1右3
  137. Widget _buildStyle5(BuildContext context, List<CustomBannerListStyle> listStyle) {
  138. CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1');
  139. CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1');
  140. CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2');
  141. CustomBannerListStyle right3 = listStyle.firstWhere((element) => element.locationType == 'right3');
  142. if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2) || EmptyUtil.isEmpty(right3)) return Container();
  143. return IntrinsicHeight(
  144. child: Row(
  145. children: <Widget>[
  146. // Flexible(flex: 4, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))),
  147. Flexible(flex: 1, child: _buildSingleWidthWidget(context, left1)),
  148. Flexible(
  149. flex: 6,
  150. child: Column(
  151. children: <Widget>[
  152. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))),
  153. Row(
  154. children: <Widget>[
  155. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))),
  156. Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right3), child: CachedNetworkImage(imageUrl: right3?.img ?? ''))),
  157. ],
  158. )
  159. ],
  160. ),
  161. )
  162. ],
  163. ),
  164. );
  165. }
  166. /// 3列
  167. Widget _buildStyle6(BuildContext context, List<CustomBannerListStyle> listStyle) {
  168. return _buildColumnWidget(context, listStyle);
  169. }
  170. /// 4列
  171. Widget _buildStyle7(BuildContext context, List<CustomBannerListStyle> listStyle) {
  172. return _buildColumnWidget(context, listStyle);
  173. }
  174. /// 5列
  175. Widget _buildStyle8(BuildContext context, List<CustomBannerListStyle> listStyle) {
  176. return _buildColumnWidget(context, listStyle);
  177. }
  178. /// 列的通用
  179. Widget _buildColumnWidget(BuildContext context, List<CustomBannerListStyle> listStyle) {
  180. return Row(
  181. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  182. children: listStyle
  183. .map((e) => Flexible(
  184. flex: 1,
  185. child: GestureDetector(
  186. behavior: HitTestBehavior.opaque,
  187. onTap: () => _itemOnClick(context, e),
  188. child: Container(
  189. width: double.infinity,
  190. child: CachedNetworkImage(
  191. imageUrl: e?.img ?? '',
  192. )),
  193. ),
  194. ))
  195. .toList(),
  196. );
  197. }
  198. /// 单个
  199. Widget _buildSingleWidthWidget(BuildContext context, CustomBannerListStyle listStyle){
  200. return GestureDetector(
  201. behavior: HitTestBehavior.opaque,
  202. onTap: ()=> _itemOnClick(context, listStyle),
  203. child: Container(
  204. width: double.infinity,
  205. child: CachedNetworkImage(
  206. imageUrl: listStyle?.img ?? '',
  207. ),
  208. ),
  209. );
  210. }
  211. }