基础组件库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

253 rader
9.3 KiB

  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:zhiying_base_widget/widgets/custom/search/model/custom_search_model.dart';
  4. import 'package:zhiying_comm/zhiying_comm.dart';
  5. ///
  6. /// 通用模块的搜索栏
  7. ///
  8. class CustomSearchWidget extends StatelessWidget {
  9. final Map<String, dynamic> data;
  10. CustomSearchModel model;
  11. CustomSearchWidget(this.data, {Key key}) : super(key: key) {
  12. try {
  13. model = CustomSearchModel.fromJson(jsonDecode(data['data']));
  14. } catch (e, s) {
  15. Logger.warn(e, s);
  16. }
  17. }
  18. // 点击事件
  19. void _onClickListener(BuildContext context, SkipModel skipModel) {
  20. if (!EmptyUtil.isEmpty(skipModel)) {
  21. RouterUtil.route(skipModel, model.toJson(), context);
  22. }
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. return Container(
  27. width: double.infinity,
  28. padding: EdgeInsets.symmetric(horizontal: ParseUtil.stringParseDouble(model?.leftRightMargin, defVal: 12.5), vertical: 4),
  29. margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(model?.topMargin)),
  30. decoration: BoxDecoration(
  31. color: HexColor.fromHex(model?.bgColor),
  32. // color: Colors.blueAccent,
  33. borderRadius: BorderRadius.only(
  34. bottomRight: Radius.circular(ParseUtil.stringParseDouble(model?.bottomRightRadius)),
  35. bottomLeft: Radius.circular(ParseUtil.stringParseDouble(model?.bottomLeftRadius)),
  36. topLeft: Radius.circular(ParseUtil.stringParseDouble(model?.topLeftRadius)),
  37. topRight: Radius.circular(ParseUtil.stringParseDouble(model?.topRightRadius)),
  38. )),
  39. child: _buildMainWidget(context));
  40. }
  41. Widget _buildMainWidget(BuildContext context) {
  42. if (EmptyUtil.isEmpty(model)) return Container();
  43. Widget rlt;
  44. switch (model.moduleType) {
  45. case 'search_1': // 右1图标
  46. rlt = _buildStyle1Widget(context);
  47. break;
  48. case 'search_2': // 无图标
  49. rlt = _buildStyle2Widget(context);
  50. break;
  51. case 'search_3': // 左1右1图标
  52. rlt = _buildStyle3Widget(context);
  53. break;
  54. case 'search_4': // 右按钮
  55. rlt = _buildStyle4Widget(context);
  56. break;
  57. default:
  58. rlt = Container();
  59. break;
  60. }
  61. return rlt;
  62. }
  63. /// 左1右1图标
  64. Widget _buildStyle3Widget(BuildContext context) {
  65. return Row(
  66. mainAxisAlignment: MainAxisAlignment.center,
  67. children: [
  68. GestureDetector(
  69. onTap: () => _onClickListener(context, model?.listStyle?.leftCss),
  70. child: CachedNetworkImage(
  71. width: 30,
  72. height: 30,
  73. imageUrl: model?.listStyle?.leftCss?.image ?? '',
  74. )),
  75. // 图标1
  76. SizedBox(width: 10),
  77. // 中间
  78. Expanded(
  79. child: GestureDetector(
  80. onTap: () => _onClickListener(context, model?.listStyle?.searchCss),
  81. behavior: HitTestBehavior.opaque,
  82. child: Container(
  83. // height: 30,
  84. width: double.infinity,
  85. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 9),
  86. decoration: BoxDecoration(borderRadius: BorderRadius.circular(60 / 2), color: HexColor.fromHex(model?.listStyle?.searchCss?.bgColor)),
  87. child: Row(
  88. children: <Widget>[
  89. CachedNetworkImage(
  90. width: 15,
  91. height: 15,
  92. imageUrl: model?.listStyle?.searchCss?.image ?? '',
  93. ),
  94. SizedBox(width: 4),
  95. Text(model?.listStyle?.searchCss?.text ?? '输入搜索内容,领券省钱', style: TextStyle(fontSize: 13, color: HexColor.fromHex(model?.listStyle?.searchCss?.textColor??"#FFFFFF")))
  96. ],
  97. ),
  98. ),
  99. )),
  100. SizedBox(width: 10),
  101. // 图标2
  102. GestureDetector(
  103. onTap: () => _onClickListener(context, model?.listStyle?.rightCss),
  104. child: CachedNetworkImage(
  105. width: 30,
  106. height: 30,
  107. imageUrl: model?.listStyle?.rightCss?.image ?? '',
  108. )),
  109. ],
  110. );
  111. }
  112. /// 右按钮
  113. Widget _buildStyle4Widget(BuildContext context) {
  114. return GestureDetector(
  115. onTap: () => _onClickListener(context, model?.listStyle?.rightBtnCss),
  116. child: Container(
  117. height: 38,
  118. padding: EdgeInsets.only(left: 10.5),
  119. decoration: BoxDecoration(
  120. border: Border.all(color: HexColor.fromHex(model?.listStyle?.searchCss?.borderColor ?? "")),
  121. borderRadius: BorderRadius.only(
  122. bottomRight: Radius.circular(ParseUtil.stringParseDouble(model?.bottomRightRadius)),
  123. bottomLeft: Radius.circular(ParseUtil.stringParseDouble(model?.bottomLeftRadius)),
  124. topLeft: Radius.circular(ParseUtil.stringParseDouble(model?.topLeftRadius)),
  125. topRight: Radius.circular(ParseUtil.stringParseDouble(model?.topRightRadius)),
  126. )),
  127. child: Row(
  128. mainAxisAlignment: MainAxisAlignment.start,
  129. crossAxisAlignment: CrossAxisAlignment.center,
  130. children: <Widget>[
  131. ///搜索图标
  132. CachedNetworkImage(
  133. imageUrl: model?.listStyle?.searchCss?.image ?? '',
  134. width: 16,
  135. fit: BoxFit.fitWidth,
  136. ),
  137. SizedBox(
  138. width: 4,
  139. ),
  140. Expanded(
  141. child: TextField(
  142. style: TextStyle(fontSize: 13),
  143. decoration: InputDecoration(
  144. isDense: true,
  145. enabled: false,
  146. border: InputBorder.none,
  147. hintText: model?.listStyle?.searchCss?.text ?? "",
  148. hintStyle: TextStyle(color: HexColor.fromHex(model?.listStyle?.searchCss?.textColor ?? ""))),
  149. ),
  150. ),
  151. Container(
  152. padding: EdgeInsets.only(left: 16, right: 16),
  153. decoration: BoxDecoration(
  154. image: DecorationImage(image: CachedNetworkImageProvider(model?.listStyle?.rightBtnCss?.bgImg ?? ""), fit: BoxFit.fitHeight),
  155. borderRadius: BorderRadius.circular(2)),
  156. height: double.infinity,
  157. child: Center(
  158. child: Text(
  159. model?.listStyle?.rightBtnCss?.text ?? "",
  160. style: TextStyle(color: HexColor.fromHex(model?.listStyle?.rightBtnCss?.textColor ?? "#FFFFFF"), fontWeight: FontWeight.w500, fontSize: 13),
  161. )),
  162. )
  163. ],
  164. ),
  165. ),
  166. );
  167. }
  168. /// 右1图标
  169. Widget _buildStyle1Widget(BuildContext context) {
  170. return Row(
  171. children: <Widget>[
  172. Expanded(
  173. child: GestureDetector(
  174. onTap: () => _onClickListener(context, model?.listStyle?.searchCss),
  175. behavior: HitTestBehavior.opaque,
  176. child: Container(
  177. // height: 30,
  178. width: double.infinity,
  179. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 9),
  180. decoration: BoxDecoration(borderRadius: BorderRadius.circular(60 / 2), color: HexColor.fromHex(model?.listStyle?.searchCss?.bgColor)),
  181. child: Row(
  182. children: <Widget>[
  183. CachedNetworkImage(
  184. width: 15,
  185. height: 15,
  186. imageUrl: model?.listStyle?.searchCss?.image ?? '',
  187. ),
  188. SizedBox(width: 4),
  189. Text(model?.listStyle?.searchCss?.text ?? '输入搜索内容,领券省钱', style: TextStyle(fontSize: 13, color: HexColor.fromHex(model?.listStyle?.searchCss?.textColor??"#FFFFFF")))
  190. ],
  191. ),
  192. ),
  193. )),
  194. SizedBox(width: 10),
  195. GestureDetector(
  196. onTap: () => _onClickListener(context, model?.listStyle?.rightCss),
  197. child: CachedNetworkImage(
  198. width: 30,
  199. height: 30,
  200. imageUrl: model?.listStyle?.rightCss?.image ?? '',
  201. )),
  202. ],
  203. );
  204. }
  205. /// 无图标
  206. Widget _buildStyle2Widget(BuildContext context) {
  207. return GestureDetector(
  208. behavior: HitTestBehavior.opaque,
  209. onTap: () => _onClickListener(context, model?.listStyle?.searchCss),
  210. child: Container(
  211. width: double.infinity,
  212. child: Container(
  213. width: double.infinity,
  214. decoration: BoxDecoration(
  215. borderRadius: BorderRadius.circular(40),
  216. color: HexColor.fromHex(model?.listStyle?.searchCss?.bgColor ?? '#F9F9F9'),
  217. ),
  218. padding: const EdgeInsets.symmetric(vertical: 6),
  219. child: Row(
  220. mainAxisAlignment: MainAxisAlignment.center,
  221. children: <Widget>[
  222. /// 搜索按钮
  223. CachedNetworkImage(
  224. imageUrl: model?.listStyle?.searchCss?.image ?? '',
  225. height: 20,
  226. width: 20,
  227. ),
  228. const SizedBox(width: 7.5),
  229. /// 提示文字
  230. Text(
  231. model?.listStyle?.searchCss?.text ?? '搜索更多优惠商品',
  232. style: TextStyle(fontSize: 14, color: HexColor.fromHex(model?.listStyle?.searchCss?.textColor ?? '#999999')),
  233. )
  234. ],
  235. ),
  236. ),
  237. ),
  238. );
  239. }
  240. }