import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:zhiying_base_widget/widgets/custom/banner/model/custom_banner_model.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; /// /// 通用的BannerWidget /// class CustomBannerWidget extends StatelessWidget { final Map data; CustomBannerModel _model; CustomBannerWidget(this.data, {Key key}) : super(key: key) { try { _model = CustomBannerModel.fromJson(jsonDecode(data['data'])); } catch (e, s) { Logger.warn(e, s); } } /// 点击事件 void _itemOnClick(BuildContext context, CustomBannerListStyle model) { print('${model?.skipIdentifier}'); RouterUtil.route(model, model.toJson(), context); } @override Widget build(BuildContext context) { /// 空视图 if (EmptyUtil.isEmpty(_model)) { return Container(); } return Container( width: double.infinity, // height: 98, margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(_model?.topMargin)), padding: EdgeInsets.symmetric(horizontal: ParseUtil.stringParseDouble(_model?.leftRightMargin, defVal: 12.5)), decoration: BoxDecoration( // color: Colors.red, borderRadius: BorderRadius.only( bottomLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomLeftRadius)), bottomRight: Radius.circular(ParseUtil.stringParseDouble(_model?.bottomRightRadius)), topLeft: Radius.circular(ParseUtil.stringParseDouble(_model?.topLeftRadius)), topRight: Radius.circular(ParseUtil.stringParseDouble(_model?.topRightRadius)), )), child: _buildMainWidget(context, _model?.moduleType, _model?.listStyle), ); } /// 主视图 Widget _buildMainWidget(BuildContext context, String moduleType, List listStyle) { if (EmptyUtil.isEmpty(listStyle)) return Container(); Widget rlt; switch (moduleType) { case 'banner1': rlt = _buildStyle1(context, listStyle); break; case 'banner2': rlt = _buildStyle2(context, listStyle); break; case 'banner3': rlt = _buildStyle3(context, listStyle); break; case 'banner4': rlt = _buildStyle4(context, listStyle); break; case 'banner5': rlt = _buildStyle5(context, listStyle); break; case 'banner6': rlt = _buildStyle6(context, listStyle); break; case 'banner7': rlt = _buildStyle7(context, listStyle); break; case 'banner8': rlt = _buildStyle8(context, listStyle); break; default: rlt = Container(); break; } return rlt; } /// 1张图 Widget _buildStyle1(BuildContext context, List listStyle) { return _buildColumnWidget(context, listStyle); } /// 左1右1 Widget _buildStyle2(BuildContext context, List listStyle) { return _buildColumnWidget(context, listStyle); } /// 左1右2 Widget _buildStyle3(BuildContext context, List listStyle) { const bool inProduction = const bool.fromEnvironment("dart.vm.product"); // if(!inProduction )return Container(); CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1'); CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1'); CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2'); if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2)) return Container(); return Row( children: [ // Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))), Flexible(flex: 1, child: _buildSingleWidthWidget(context, left1)), Flexible( flex: 1, child: Visibility( replacement: Container(), visible: inProduction, child: Column( children: [ Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))), Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))), ], ), ), ), ], ); } /// 左2右1 Widget _buildStyle4(BuildContext context, List listStyle) { const bool inProduction = const bool.fromEnvironment("dart.vm.product"); // if(!inProduction )return Container(); CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1'); CustomBannerListStyle left2 = listStyle.firstWhere((element) => element.locationType == 'left2'); CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1'); if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(left2)) return Container(); return Row( children: [ Flexible( flex: 1, child: Visibility( replacement: Container(), visible: inProduction, child: Column( children: [ Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))), Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, left2), child: CachedNetworkImage(imageUrl: left2?.img ?? ''))), ], ), ), ), // Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))), Flexible(flex: 1, child: _buildSingleWidthWidget(context, right1)), ], ); } /// 左1右3 Widget _buildStyle5(BuildContext context, List listStyle) { const bool inProduction = const bool.fromEnvironment("dart.vm.product"); // if(!inProduction )return Container(); CustomBannerListStyle left1 = listStyle.firstWhere((element) => element.locationType == 'left1'); CustomBannerListStyle right1 = listStyle.firstWhere((element) => element.locationType == 'right1'); CustomBannerListStyle right2 = listStyle.firstWhere((element) => element.locationType == 'right2'); CustomBannerListStyle right3 = listStyle.firstWhere((element) => element.locationType == 'right3'); if (EmptyUtil.isEmpty(left1) || EmptyUtil.isEmpty(right1) || EmptyUtil.isEmpty(right2) || EmptyUtil.isEmpty(right3)) return Container(); return Row( children: [ // Flexible(flex: 4, child: GestureDetector(onTap: () => _itemOnClick(context, left1), child: CachedNetworkImage(imageUrl: left1?.img ?? ''))), Flexible(flex: 4, child: _buildSingleWidthWidget(context, left1)), Flexible( flex: 6, child: Visibility( replacement: Container(), visible: inProduction, child: Column( children: [ Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right1), child: CachedNetworkImage(imageUrl: right1?.img ?? ''))), Row( children: [ Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right2), child: CachedNetworkImage(imageUrl: right2?.img ?? ''))), Flexible(flex: 1, child: GestureDetector(onTap: () => _itemOnClick(context, right3), child: CachedNetworkImage(imageUrl: right3?.img ?? ''))), ], ) ], ), ), ) ], ); } /// 3列 Widget _buildStyle6(BuildContext context, List listStyle) { return _buildColumnWidget(context, listStyle); } /// 4列 Widget _buildStyle7(BuildContext context, List listStyle) { return _buildColumnWidget(context, listStyle); } /// 5列 Widget _buildStyle8(BuildContext context, List listStyle) { return _buildColumnWidget(context, listStyle); } /// 列的通用 Widget _buildColumnWidget(BuildContext context, List listStyle) { return Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: listStyle .map((e) => Flexible( flex: 1, child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () => _itemOnClick(context, e), child: Container( width: double.infinity, child: CachedNetworkImage( imageUrl: e?.img ?? '', )), ), )) .toList(), ); } /// 单个 Widget _buildSingleWidthWidget(BuildContext context, CustomBannerListStyle listStyle){ return GestureDetector( behavior: HitTestBehavior.opaque, onTap: ()=> _itemOnClick(context, listStyle), child: Container( width: double.infinity, child: CachedNetworkImage( imageUrl: listStyle?.img ?? '', ), ), ); } }