YSSearch.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'package:ysairplane2/code/YSSearchResult.dart';
  5. import 'package:ysairplane2/tools/YSNetWorking.dart';
  6. import 'package:ysairplane2/tools/YSTools.dart';
  7. class YSSearch extends StatefulWidget {
  8. @override
  9. _YSSearchState createState() => _YSSearchState();
  10. }
  11. class _YSSearchState extends State<YSSearch> {
  12. TextEditingController _searchStr = TextEditingController();
  13. List<String> _historyList = [];
  14. @override
  15. void initState() {
  16. SharedPreferences.getInstance().then((value){
  17. setState(() {
  18. _historyList = value.getStringList('history')??[];
  19. });
  20. });
  21. super.initState();
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. backgroundColor: Colors.white,
  27. body: SingleChildScrollView(
  28. child: Container(
  29. width: MediaQuery.of(context).size.width,
  30. child: Column(
  31. crossAxisAlignment: CrossAxisAlignment.start,
  32. children: [
  33. Container(
  34. margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top+hsp(42),left: wsp(32),right: wsp(22)),
  35. child: Row(
  36. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  37. children: [
  38. Container(
  39. width: MediaQuery.of(context).size.width-wsp(130),
  40. padding: EdgeInsets.only(left: wsp(26),right: wsp(26)),
  41. height: hsp(66),
  42. decoration: BoxDecoration(
  43. border: Border.all(color: Color(0xFF007AFF),width: 1),
  44. borderRadius: BorderRadius.all(Radius.circular(5))
  45. ),
  46. child: CupertinoTextField(
  47. placeholder: '请输入搜索内容',
  48. placeholderStyle: TextStyle(fontSize: zsp(25),color: Color(0xFF545454)),
  49. style: TextStyle(fontSize: zsp(25),color: Color(0xFF545454)),
  50. prefix: Icon(Icons.search,size: hsp(40),color: Color(0xFF8E8E93),),
  51. suffix: GestureDetector(child: Icon(Icons.close,size: hsp(40),color: Color(0xFF8E8E93),),onTap: (){
  52. _searchStr.text = '';
  53. searchKey.currentState.getSearchWordData('');
  54. },),
  55. suffixMode: OverlayVisibilityMode.editing,
  56. controller: _searchStr,
  57. padding: EdgeInsets.all(0),
  58. decoration: BoxDecoration(),
  59. onChanged: (value) async{
  60. searchKey.currentState.getSearchWordData(value);
  61. },
  62. onSubmitted: (value){
  63. if(value.isNotEmpty){
  64. if(_historyList.contains(value)){
  65. _historyList.remove(value);
  66. }
  67. _historyList.insert(0, value);
  68. SharedPreferences.getInstance().then((prefer){
  69. prefer.setStringList('history', _historyList);
  70. setState(() {});
  71. Navigator.of(context).push(
  72. CupertinoPageRoute(
  73. builder: (context){
  74. return YSSearchResult(searchStr: value,);
  75. }
  76. )
  77. );
  78. });
  79. }
  80. },
  81. textInputAction: TextInputAction.search,
  82. ),
  83. ),
  84. GestureDetector(child: Text('取消',style: TextStyle(fontSize: zsp(30),color: Color(0xFF444444)),),onTap: (){Navigator.pop(context);},)
  85. ],
  86. ),
  87. ),
  88. Stack(
  89. children: [
  90. Column(
  91. crossAxisAlignment: CrossAxisAlignment.start,
  92. children: [
  93. Container(
  94. child: Row(
  95. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  96. children: [
  97. Text('定位/历史',style: TextStyle(fontSize: zsp(24),color: Color(0xFF9A9A9A)),),
  98. GestureDetector(
  99. child: Icon(Icons.delete,color: Color(0xFFE0E0E0),size: hsp(40),),
  100. onTap: (){
  101. SharedPreferences.getInstance().then((prefer){
  102. _historyList = [];
  103. prefer.setStringList('history', _historyList);
  104. setState(() {
  105. });
  106. });
  107. },
  108. )
  109. ],
  110. ),
  111. padding: EdgeInsets.only(left: wsp(32),right: wsp(62),top: hsp(34),bottom: hsp(10)),
  112. ),
  113. Container(
  114. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-hsp(200),
  115. child: SingleChildScrollView(
  116. padding: EdgeInsets.only(left: wsp(16)),
  117. child: Wrap(
  118. alignment: WrapAlignment.start,
  119. children: <Widget>[
  120. for (int i =0;i<_historyList.length;i++ ) GestureDetector(
  121. behavior: HitTestBehavior.opaque,
  122. onTap: (){
  123. String value = _historyList[i];
  124. _historyList.remove(_historyList[i]);
  125. _historyList.insert(0, value);
  126. SharedPreferences.getInstance().then((prefer){
  127. prefer.setStringList('history', _historyList);
  128. setState(() {});
  129. Navigator.of(context).push(
  130. CupertinoPageRoute(
  131. builder: (context){
  132. return YSSearchResult(searchStr: value,);
  133. }
  134. )
  135. );
  136. });
  137. },
  138. child: Container(
  139. margin: EdgeInsets.symmetric(vertical: hsp(16), horizontal: hsp(16)),
  140. decoration: BoxDecoration(
  141. color: Color(0xFFE0E0E0),
  142. borderRadius: BorderRadius.all(Radius.circular(3))
  143. ),
  144. padding: EdgeInsets.only(left: wsp(40), right: wsp(40),top: hsp(20),bottom: hsp(20)),
  145. child: Text('${_historyList[i]}', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  146. ),
  147. )
  148. ]
  149. ),
  150. ),
  151. )
  152. ],
  153. ),
  154. YSSearchWordView(key: searchKey, historyList: _historyList,)
  155. ],
  156. )
  157. ],
  158. ),
  159. ),
  160. physics: NeverScrollableScrollPhysics(),
  161. ),
  162. );
  163. }
  164. }
  165. class YSSearchWordView extends StatefulWidget {
  166. final List historyList;
  167. final ValueSetter wordSetter;
  168. const YSSearchWordView({Key key, @required this.historyList, this.wordSetter}) : super(key: key);
  169. @override
  170. _YSSearchWordViewState createState() => _YSSearchWordViewState();
  171. }
  172. GlobalKey<_YSSearchWordViewState> searchKey = GlobalKey();
  173. GlobalKey<_YSSearchWordViewState> searchKey2 = GlobalKey();
  174. class _YSSearchWordViewState extends State<YSSearchWordView> {
  175. List _dataArray = [];
  176. @override
  177. Widget build(BuildContext context) {
  178. return Container(
  179. child: ListView.separated(itemBuilder: (context,index){
  180. return GestureDetector(
  181. onTap: (){
  182. String value = _dataArray[index];
  183. if(widget.wordSetter!=null){
  184. widget.wordSetter(value);
  185. }else{
  186. if(widget.historyList.contains(value)){
  187. widget.historyList.remove(value);
  188. }
  189. widget.historyList.insert(0, value);
  190. SharedPreferences.getInstance().then((prefer){
  191. prefer.setStringList('history', widget.historyList);
  192. setState(() {});
  193. Navigator.of(context).push(
  194. CupertinoPageRoute(
  195. builder: (context){
  196. return YSSearchResult(searchStr: value,);
  197. }
  198. )
  199. );
  200. });
  201. }
  202. },
  203. child: Container(
  204. width: MediaQuery.of(context).size.width,
  205. padding: EdgeInsets.all(hsp(30)),
  206. color: Colors.white,
  207. child: Text(_dataArray[index]),
  208. ),
  209. );
  210. },separatorBuilder: (context,index){
  211. return Divider(height: hsp(1),thickness: hsp(1),color: Colors.grey.withOpacity(0.1),);
  212. },itemCount: _dataArray.length,shrinkWrap: true,padding: EdgeInsets.all(0),),
  213. );
  214. }
  215. getSearchWordData(String word) async{
  216. if(word.isEmpty){
  217. _dataArray.clear();
  218. }else{
  219. Map dict = await ysRequestHttp(context,type: requestType.get,
  220. api: '/app/applets/AirTour/search_ideas',
  221. parameter: {'keyWords':word},isLoading: false,isToken: true,unfocus: false);
  222. if(dict!=null){
  223. _dataArray = dict['data'];
  224. }
  225. }
  226. setState(() {});
  227. }
  228. }