基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_banner_widget.dart 7.9 KiB

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