基础组件库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

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