基础组件库
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.
 
 
 
 
 

267 lines
8.9 KiB

  1. import 'dart:convert';
  2. import 'dart:ui';
  3. import 'package:cached_network_image/cached_network_image.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import 'package:fluttertoast/fluttertoast.dart';
  8. import 'package:provider/provider.dart';
  9. import 'package:zhiying_base_widget/pages/search_page/notifier/search_tag_notifier.dart';
  10. import 'package:zhiying_base_widget/pages/search_result_page/search_result_page.dart';
  11. import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_bloc.dart';
  12. import 'package:zhiying_base_widget/pages/search_think_page/model/search_think_model.dart';
  13. import 'package:zhiying_base_widget/pages/search_think_page/search_think_page.dart';
  14. import 'package:zhiying_base_widget/widgets/search/input/model/search_input_model.dart';
  15. import 'package:zhiying_base_widget/widgets/search/input/search_input_sk.dart';
  16. import 'package:zhiying_comm/zhiying_comm.dart';
  17. ///
  18. /// 搜索页的搜索框
  19. ///
  20. class SearchInputWidget extends StatefulWidget {
  21. final Map<String, dynamic> data;
  22. SearchInputModel model;
  23. SearchInputWidget(this.data, {Key key}) : super(key: key) {
  24. try {
  25. model = SearchInputModel.fromJson(jsonDecode(data['data']));
  26. } catch (e) {
  27. Logger.error(e);
  28. }
  29. }
  30. @override
  31. _SearchInputWidgetState createState() => _SearchInputWidgetState();
  32. }
  33. class _SearchInputWidgetState extends State<SearchInputWidget> {
  34. FocusNode _focusNode;
  35. TextEditingController _editingController;
  36. /// 点击搜索按钮
  37. void _onSearchButtomClick() async {
  38. String content = _editingController?.text?.toString()?.trim() ?? '';
  39. /// TODO 保存历史标签
  40. if (!EmptyUtil.isEmpty(content)) {
  41. Provider.of<SearchTagNotifier>(context, listen: false).addTag(content);
  42. RouterUtil.hideKeyboard(context);
  43. SearchThinkModel searchThinkModel = SearchThinkModel();
  44. searchThinkModel.type = (Provider.of<SearchTagNotifier>(context, listen: false)?.getType()) ?? GlobalConfig.PROVIDER_TB;
  45. searchThinkModel.keywords = content ?? '';
  46. Navigator.push(
  47. context, CupertinoPageRoute(builder: (_) => SearchResultPage(searchThinkModel.toJson()..['tag'] = 'search_page')));
  48. // Navigator.push(context, CupertinoPageRoute(builder: (_) => SearchResultPage({'keywords': content, 'tag': 'search_page'})));
  49. } else {
  50. Fluttertoast.showToast(msg: '输入内容不能为空!');
  51. }
  52. }
  53. /// 【弃用】打开搜索联想页面
  54. void _openSearchThinkPage() {
  55. Navigator.push(context, CupertinoPageRoute(builder: (_) => SearchThinkPage({})));
  56. }
  57. /// 搜索框值改变
  58. void _searchInputChange(String text) {
  59. if (!EmptyUtil.isEmpty(text)) {
  60. // 进行网络更新
  61. print('输入框的内容是 $text');
  62. BlocProvider.of<SearchThinkBloc>(context).add(SearchThinkKeyWrodsChangeEvent(text));
  63. } else {
  64. /// 输入框为空的时候,隐藏联想视图,显示原本的视图
  65. BlocProvider.of<SearchThinkBloc>(context).add(SearchThinkShowBaseViewEvent());
  66. }
  67. }
  68. void _onEditingComplete() {
  69. Logger.log('=======我输入完毕拉!!!!');
  70. }
  71. @override
  72. void didChangeDependencies() {
  73. super.didChangeDependencies();
  74. }
  75. @override
  76. void initState() {
  77. _focusNode = FocusNode();
  78. _editingController = TextEditingController();
  79. super.initState();
  80. }
  81. @override
  82. void dispose() {
  83. _focusNode?.unfocus();
  84. _focusNode?.dispose();
  85. _editingController?.dispose();
  86. super.dispose();
  87. }
  88. @override
  89. Widget build(BuildContext context) {
  90. return Visibility(
  91. visible: !EmptyUtil.isEmpty(widget?.model),
  92. replacement: SearchInputSkeleton(),
  93. child: _getMainWidget(widget?.model),
  94. );
  95. }
  96. /// 获取主视图
  97. Widget _getMainWidget(SearchInputModel model) {
  98. return Container(
  99. color: Colors.white,
  100. width: double.infinity,
  101. margin: EdgeInsets.only(top: MediaQueryData.fromWindow(window).padding.top + 7.5, left: 12.5, right: 12.5),
  102. child: Row(
  103. children: <Widget>[
  104. /// 返回键
  105. _getReturnWidget(),
  106. const SizedBox(width: 8.5),
  107. /// 输入框
  108. Expanded(
  109. child: Container(
  110. width: double.infinity,
  111. height: 32,
  112. // margin: const EdgeInsets.only(
  113. // left: 12.5,
  114. // right: 12.5,
  115. // ),
  116. decoration: BoxDecoration(
  117. borderRadius: BorderRadius.circular(25),
  118. color: HexColor.fromHex('#F9F9F9'),
  119. ),
  120. child: Row(
  121. children: <Widget>[
  122. /// 搜索icon
  123. _getSearchIconWidget(model),
  124. const SizedBox(width: 7.5),
  125. /// 搜索输入框
  126. Expanded(child: _getSearchInputWidget(model)),
  127. /// 搜索按钮
  128. _getSearchButtomWidget(model),
  129. ],
  130. ),
  131. ),
  132. ),
  133. ],
  134. ),
  135. );
  136. }
  137. /// 返回键
  138. Widget _getReturnWidget() {
  139. return GestureDetector(
  140. onTap: () => Navigator.maybePop(context),
  141. child: Container(
  142. child: Icon(
  143. Icons.arrow_back_ios,
  144. size: 22,
  145. color: HexColor.fromHex('#333333'),
  146. ),
  147. // onPressed: () => Navigator.maybePop(context),
  148. ),
  149. );
  150. }
  151. /// 搜索icon
  152. Widget _getSearchIconWidget(SearchInputModel model) {
  153. return Container(
  154. height: double.infinity,
  155. width: 20,
  156. margin: const EdgeInsets.only(left: 12.5),
  157. padding: const EdgeInsets.symmetric(vertical: 6),
  158. child: CachedNetworkImage(
  159. imageUrl: model?.search_icon ?? '',
  160. ),
  161. );
  162. }
  163. /// 搜索输入框
  164. Widget _getSearchInputWidget(SearchInputModel model) {
  165. return Container(
  166. height: double.infinity,
  167. alignment: Alignment.centerLeft,
  168. decoration: BoxDecoration(borderRadius: BorderRadius.circular(30), color: HexColor.fromHex('#F9F9F9')),
  169. // padding: const EdgeInsets.symmetric(vertical: 6),
  170. child: TextField(
  171. showCursor: true,
  172. // readOnly: true,
  173. // onTap: ()=> _openSearchThinkPage(),
  174. onChanged: (val) => _searchInputChange(val),
  175. cursorWidth: 1,
  176. onSubmitted: (text) => _onSearchButtomClick(),
  177. onEditingComplete: () => _onEditingComplete(),
  178. controller: _editingController,
  179. focusNode: _focusNode,
  180. cursorColor: Colors.transparent,
  181. style: TextStyle(fontSize: 14, color: HexColor.fromHex('#333333'), textBaseline: TextBaseline.alphabetic),
  182. decoration: InputDecoration(
  183. filled: true,
  184. isDense: true,
  185. // contentPadding: EdgeInsets.zero,
  186. contentPadding: const EdgeInsets.only(left: 0, right: 5, top: 0, bottom: 0),
  187. // contentPadding: const EdgeInsets.only(left: 0, right: 0, bottom: 12, top: 0),
  188. // focusColor: Colors.transparent,
  189. fillColor: Colors.transparent,
  190. border: InputBorder.none,
  191. focusedBorder: InputBorder.none,
  192. focusedErrorBorder: InputBorder.none,
  193. errorBorder: InputBorder.none,
  194. disabledBorder: InputBorder.none,
  195. enabledBorder: InputBorder.none,
  196. hintText: model?.search_inpu_hint_text ?? '搜索更多优惠商品',
  197. hintStyle: TextStyle(color: HexColor.fromHex('#999999'), fontSize: 14, textBaseline: TextBaseline.alphabetic),
  198. ),
  199. ),
  200. );
  201. }
  202. /// 搜索按钮
  203. Widget _getSearchButtomWidget(SearchInputModel model) {
  204. return GestureDetector(
  205. behavior: HitTestBehavior.opaque,
  206. onTap: () => _onSearchButtomClick(),
  207. child: Container(
  208. padding: const EdgeInsets.symmetric(horizontal: 17.5, vertical: 6),
  209. decoration: BoxDecoration(
  210. gradient: LinearGradient(
  211. colors: [HexColor.fromHex(model?.search_button_color ?? '#FD5E5E'), HexColor.fromHex(model?.search_button_t ?? '#FF0100')],
  212. begin: Alignment.centerLeft,
  213. end: Alignment.centerRight),
  214. borderRadius: BorderRadius.circular(30),
  215. ),
  216. child: Text(
  217. model?.search_button ?? '搜索',
  218. style: TextStyle(color: HexColor.fromHex('#FFFFFF'), fontSize: 14),
  219. ),
  220. ),
  221. );
  222. }
  223. /// 【弃用】搜索按钮
  224. // Widget _getSearchButtomWidget() {
  225. // return Material(
  226. // child: Container(
  227. // decoration: BoxDecoration(
  228. // borderRadius: BorderRadius.only(topRight: Radius.circular(25), bottomRight: Radius.circular(25))
  229. // ),
  230. // height: double.infinity,
  231. // width: 63,
  232. // child: RaisedButton(
  233. // padding: const EdgeInsets.only(bottom: 6, top: 6, left: 17.5, right: 17.5),
  234. // shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular( 25)),
  235. // child: Text('搜索', style: TextStyle( fontSize: 14, color: HexColor.fromHex('#FFFFFF')),),
  236. // onPressed: ()=> _onSearchButtomClick(),
  237. // color: HexColor.fromHex('#FF0100'),
  238. // ),
  239. // ),
  240. // );
  241. // }
  242. }