YSCity.dart 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import 'dart:convert';
  2. import 'dart:math';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:ysairplane/tools/YSNetWorking.dart';
  6. import 'package:ysairplane/tools/YSTools.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. class YSCity extends StatefulWidget {
  9. @override
  10. _YSCityState createState() => _YSCityState();
  11. }
  12. class _YSCityState extends State<YSCity> {
  13. List _historyList = [];
  14. List _hotList = [];
  15. List _searchList = [];
  16. List _dataArray = [];
  17. TextEditingController _searchStr = TextEditingController();
  18. @override
  19. void initState() {
  20. Future.delayed(Duration(seconds: 0)).then((value){
  21. _getHotCityData();
  22. });
  23. super.initState();
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. return Scaffold(
  28. backgroundColor: Color(0xFFF0F0F1),
  29. appBar: CupertinoNavigationBar(
  30. backgroundColor: Colors.white,
  31. leading: GestureDetector(child: Icon(Icons.arrow_back_ios,size: 15,color: Color(0xFF000000),),onTap: (){Navigator.pop(context);},),
  32. border: Border(),
  33. middle: Text('选择城市',style: TextStyle(fontSize: zsp(36),color: Color(0xFF000000)),),
  34. ),
  35. body: SingleChildScrollView(
  36. child: Container(
  37. width: MediaQuery.of(context).size.width,
  38. child: Column(
  39. children: [
  40. Container(
  41. height: hsp(88),
  42. color: Color(0xFF007AFF),
  43. alignment: Alignment.center,
  44. child: Container(
  45. height: hsp(66),
  46. width: MediaQuery.of(context).size.width-wsp(30),
  47. padding: EdgeInsets.only(left: wsp(30),right: wsp(30)),
  48. decoration: BoxDecoration(
  49. color: Colors.white,
  50. borderRadius: BorderRadius.all(Radius.circular(5))
  51. ),
  52. child: CupertinoTextField(
  53. placeholder: '搜索城市或者国家',
  54. placeholderStyle: TextStyle(fontSize: zsp(28),color: Color(0xFF545454)),
  55. style: TextStyle(fontSize: zsp(28),color: Color(0xFF545454)),
  56. prefix: Icon(Icons.search,size: hsp(40),color: Color(0xFF8E8E93),),
  57. suffix: GestureDetector(child: Icon(Icons.close,size: hsp(40),color: Color(0xFF8E8E93),),onTap: (){_searchStr.text = '';},),
  58. suffixMode: OverlayVisibilityMode.editing,
  59. controller: _searchStr,
  60. padding: EdgeInsets.all(0),
  61. decoration: BoxDecoration(),
  62. onChanged: (value){
  63. setState(() {});
  64. },
  65. textInputAction: TextInputAction.search,
  66. ),
  67. ),
  68. ),
  69. _searchStr.text.isNotEmpty? Container(
  70. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44-hsp(88),
  71. child: ListView.separated(
  72. itemBuilder: (context,index){
  73. return GestureDetector(
  74. onTap: (){
  75. Map city = _siftCity()[index];
  76. List indexList = [];
  77. for(int i = 0;i<_historyList.length;i++){
  78. if(_historyList[i]['display']==city['display']){
  79. indexList.add(i);
  80. }
  81. }
  82. indexList.forEach((element) {
  83. _historyList.removeAt(element);
  84. });
  85. _historyList.insert(0, city);
  86. SharedPreferences.getInstance().then((prefer){
  87. String historyStr = jsonEncode(_historyList);
  88. prefer.setString('cityHistory', historyStr);
  89. Navigator.of(context).pop(city);
  90. });
  91. },
  92. child: Container(
  93. color: Colors.white,
  94. height: hsp(100),
  95. padding: EdgeInsets.only(left: wsp(40),right: wsp(40)),
  96. alignment: Alignment.centerLeft,
  97. child: Row(
  98. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  99. children: [
  100. indicateString('${_siftCity()[index]['display']}', _searchStr.text, _siftCity()[index]['airplanename']!=null?_siftCity()[index]['airplanename']:''),
  101. Icon(Icons.keyboard_arrow_right,size: hsp(40),color: Color(0xFFA4A5A5),)
  102. ],
  103. )
  104. ),
  105. );
  106. },
  107. separatorBuilder: (context,index){
  108. return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),);
  109. },
  110. itemCount: _siftCity().length,
  111. ),
  112. ):Container(
  113. child: Row(
  114. children: [
  115. Column(
  116. children: [
  117. Container(
  118. height: hsp(85),
  119. color: Colors.white,
  120. width: MediaQuery.of(context).size.width/2,
  121. alignment: Alignment.center,
  122. child: Text('国内城市',style: TextStyle(fontSize: zsp(30),color: Color(0xFF444444)),),
  123. ),
  124. Container(
  125. height: hsp(3),
  126. color: Color(0xFF007AFF),
  127. width: wsp(133),
  128. )
  129. ],
  130. ),
  131. Column(
  132. children: [
  133. Container(
  134. height: hsp(88),
  135. color: Colors.white,
  136. width: MediaQuery.of(context).size.width/2,
  137. alignment: Alignment.center,
  138. child: Text('国际/中国港澳台',style: TextStyle(fontSize: zsp(30),color: Color(0xFF444444)),),
  139. ),
  140. Container(
  141. height: hsp(3),
  142. color: Colors.transparent,
  143. width: wsp(133),
  144. )
  145. ],
  146. )
  147. ],
  148. ),
  149. ),
  150. Column(
  151. crossAxisAlignment: CrossAxisAlignment.start,
  152. children: [
  153. Container(
  154. child: Row(
  155. children: [
  156. Text('定位/历史',style: TextStyle(fontSize: zsp(24),color: Color(0xFF9A9A9A)),),
  157. GestureDetector(
  158. child: Icon(Icons.delete,color: Color(0xFF9A9A9A),size: hsp(40),),
  159. onTap: (){
  160. SharedPreferences.getInstance().then((prefer){
  161. _historyList.clear();
  162. String historyStr = jsonEncode(_historyList);
  163. prefer.setString('cityHistory', historyStr);
  164. setState(() {});
  165. });
  166. },
  167. )
  168. ],
  169. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  170. ),
  171. padding: EdgeInsets.only(left: wsp(24),right: wsp(24),top: hsp(34),bottom: hsp(10)),
  172. width: MediaQuery.of(context).size.width,
  173. ),
  174. Container(
  175. margin: EdgeInsets.only(left: zsp(8)),
  176. child: Wrap(
  177. alignment: WrapAlignment.start,
  178. children: <Widget>[
  179. for (int i =0;i<_historyList.length+1;i++ ) GestureDetector(
  180. behavior: HitTestBehavior.opaque,
  181. onTap: (){
  182. if(i>0){
  183. Map city = _historyList[i-1];
  184. _historyList.remove(city);
  185. _historyList.insert(0, city);
  186. SharedPreferences.getInstance().then((prefer){
  187. String historyStr = jsonEncode(_historyList);
  188. prefer.setString('cityHistory', historyStr);
  189. Navigator.of(context).pop(city);
  190. });
  191. }
  192. },
  193. child: Container(
  194. margin: EdgeInsets.symmetric(vertical: hsp(16), horizontal: hsp(16)),
  195. decoration: BoxDecoration(
  196. color: Colors.white,
  197. borderRadius: BorderRadius.all(Radius.circular(5))
  198. ),
  199. padding: EdgeInsets.only(left: wsp(40), right: wsp(40),top: hsp(20),bottom: hsp(20)),
  200. child: i==0?Row(
  201. mainAxisSize: MainAxisSize.min,
  202. children: [
  203. Icon(Icons.location_on,color: Color(0xFF007AFF),size: hsp(40),),
  204. Text(' 西安', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  205. ],
  206. ):Text('${_historyList[i-1]['display']}', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  207. ),
  208. )
  209. ]
  210. ),
  211. ),
  212. Container(
  213. child: Text('热门城市',style: TextStyle(fontSize: zsp(24),color: Color(0xFF9A9A9A)),),
  214. padding: EdgeInsets.only(left: wsp(24),right: wsp(24),top: hsp(34),bottom: hsp(10)),
  215. width: MediaQuery.of(context).size.width,
  216. ),
  217. Container(
  218. margin: EdgeInsets.only(bottom: hsp(10),left: zsp(8)),
  219. child: Wrap(
  220. alignment: WrapAlignment.start,
  221. children: <Widget>[
  222. for (Map item in _hotList) GestureDetector(
  223. behavior: HitTestBehavior.opaque,
  224. onTap: (){
  225. Map city = item;
  226. List indexList = [];
  227. for(int i = 0;i<_historyList.length;i++){
  228. if(_historyList[i]['display']==city['display']){
  229. indexList.add(i);
  230. }
  231. }
  232. indexList.forEach((element) {
  233. _historyList.removeAt(element);
  234. });
  235. _historyList.insert(0, city);
  236. SharedPreferences.getInstance().then((prefer){
  237. String historyStr = jsonEncode(_historyList);
  238. prefer.setString('cityHistory', historyStr);
  239. Navigator.of(context).pop(city);
  240. });
  241. },
  242. child: Container(
  243. margin: EdgeInsets.symmetric(vertical: hsp(16), horizontal: hsp(16)),
  244. decoration: BoxDecoration(
  245. color: Colors.white,
  246. borderRadius: BorderRadius.all(Radius.circular(5))
  247. ),
  248. padding: EdgeInsets.only(left: wsp(40), right: wsp(40),top: hsp(20),bottom: hsp(20)),
  249. child: Text('${item['display']}', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  250. ),
  251. )
  252. ]
  253. ),
  254. )
  255. ],
  256. ),
  257. Container(
  258. color: Colors.white,
  259. child: ListView.separated(
  260. shrinkWrap: true,
  261. itemBuilder: (context,index){
  262. return Column(
  263. children: [
  264. Container(
  265. color: Colors.white,
  266. height: hsp(60),
  267. padding: EdgeInsets.only(left: wsp(30)),
  268. alignment: Alignment.centerLeft,
  269. child: Text('${_dataArray[index]['word']}',style: TextStyle(fontSize: zsp(24),color: Color(0xFFA4A5A5),fontWeight: FontWeight.bold),),
  270. ),
  271. Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),),
  272. ListView.separated(
  273. itemBuilder: (context,indexSub){
  274. return GestureDetector(
  275. behavior: HitTestBehavior.opaque,
  276. onTap: (){
  277. Map city = _dataArray[index]['mapList'][indexSub];
  278. List indexList = [];
  279. for(int i = 0;i<_historyList.length;i++){
  280. if(_historyList[i]['display']==city['display']){
  281. indexList.add(i);
  282. }
  283. }
  284. indexList.forEach((element) {
  285. _historyList.removeAt(element);
  286. });
  287. _historyList.insert(0, city);
  288. SharedPreferences.getInstance().then((prefer){
  289. String historyStr = jsonEncode(_historyList);
  290. prefer.setString('cityHistory', historyStr);
  291. Navigator.of(context).pop(city);
  292. });
  293. },
  294. child: Container(
  295. color: Colors.white,
  296. height: hsp(100),
  297. padding: EdgeInsets.only(left: wsp(40)),
  298. alignment: Alignment.centerLeft,
  299. child: Text('${_dataArray[index]['mapList'][indexSub]['display']}',style: TextStyle(fontSize: zsp(32),color: Color(0xFF16181A)),),
  300. ),
  301. );
  302. },
  303. separatorBuilder: (context,indexSub){
  304. return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),);
  305. },
  306. itemCount: (_dataArray[index]['mapList']).length,
  307. shrinkWrap: true,
  308. physics: NeverScrollableScrollPhysics(),
  309. )
  310. ],
  311. );
  312. },
  313. separatorBuilder: (context,index){
  314. return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),);
  315. },
  316. itemCount: _dataArray.length,
  317. physics: NeverScrollableScrollPhysics(),
  318. ),
  319. )
  320. ],
  321. ),
  322. ),
  323. ),
  324. );
  325. }
  326. _getHotCityData() async{
  327. SharedPreferences prefer = await SharedPreferences.getInstance();
  328. String historyStr = prefer.getString('cityHistory')??'[]';
  329. _historyList = jsonDecode(historyStr);
  330. setState(() {});
  331. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/poi/list',parameter: {},isLoading: false,isToken: false);
  332. if(dict!=null){
  333. _dataArray = dict['data'];
  334. _dataArray.forEach((element) {
  335. List array = element['mapList'];
  336. array.forEach((elementSub) {
  337. if(elementSub['isHot']=='1'){
  338. _hotList.add(elementSub);
  339. }
  340. _searchList.add(elementSub);
  341. });
  342. });
  343. print('_hotList = $_hotList');
  344. setState(() {});
  345. }
  346. }
  347. List _siftCity() {
  348. List array = [];
  349. _searchList.forEach((element) {
  350. if(((element['display']).split(_searchStr.text)).length>1){
  351. array.add(element);
  352. }
  353. });
  354. return array;
  355. }
  356. }