YSCity.dart 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import 'dart:convert';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_easyrefresh/easy_refresh.dart';
  5. import 'package:ysairplane2/tools/YSNetWorking.dart';
  6. import 'package:ysairplane2/tools/YSTools.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. class YSCity extends StatefulWidget {
  9. final bool isHome;
  10. const YSCity({Key key, this.isHome}) : super(key: key);
  11. @override
  12. _YSCityState createState() => _YSCityState();
  13. }
  14. class _YSCityState extends State<YSCity> {
  15. List _historyList = [];
  16. List _hotList = [];
  17. List _dataArray = [];
  18. List _searchList = [];
  19. List _hotList2 = [];
  20. List _dataArray2 = [];
  21. int _selected = 0;
  22. int _page = 1;
  23. String _location = '';
  24. TextEditingController _searchStr = TextEditingController();
  25. @override
  26. void initState() {
  27. Future.delayed(Duration(seconds: 0)).then((value){
  28. _refresh();
  29. });
  30. super.initState();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. backgroundColor: Color(0xFFF0F0F1),
  36. appBar: CupertinoNavigationBar(
  37. backgroundColor: Colors.white,
  38. leading: GestureDetector(child: Icon(Icons.arrow_back_ios,size: 15,color: Color(0xFF000000),),onTap: (){Navigator.pop(context);},),
  39. border: Border(),
  40. middle: Text('选择城市',style: TextStyle(fontSize: zsp(36),color: Color(0xFF000000)),),
  41. ),
  42. body: EasyRefresh(
  43. onRefresh: _refresh,
  44. onLoad: _loadMore,
  45. header: TaurusHeader(
  46. ),
  47. footer: TaurusFooter(
  48. ),
  49. child: SingleChildScrollView(
  50. child: Container(
  51. width: MediaQuery.of(context).size.width,
  52. child: Column(
  53. children: [
  54. Container(
  55. height: hsp(88),
  56. color: Color(0xFF007AFF),
  57. alignment: Alignment.center,
  58. child: Container(
  59. height: hsp(66),
  60. width: MediaQuery.of(context).size.width-wsp(30),
  61. padding: EdgeInsets.only(left: wsp(30),right: wsp(30)),
  62. decoration: BoxDecoration(
  63. color: Colors.white,
  64. borderRadius: BorderRadius.all(Radius.circular(5))
  65. ),
  66. child: CupertinoTextField(
  67. placeholder: '搜索城市或者国家',
  68. placeholderStyle: TextStyle(fontSize: zsp(28),color: Color(0xFF545454)),
  69. style: TextStyle(fontSize: zsp(28),color: Color(0xFF545454)),
  70. prefix: Icon(Icons.search,size: hsp(40),color: Color(0xFF8E8E93),),
  71. suffix: GestureDetector(child: Icon(Icons.close,size: hsp(40),color: Color(0xFF8E8E93),),
  72. onTap: (){_searchStr.text = '';FocusScope.of(context).unfocus();},),
  73. suffixMode: OverlayVisibilityMode.editing,
  74. controller: _searchStr,
  75. padding: EdgeInsets.all(0),
  76. decoration: BoxDecoration(),
  77. onEditingComplete: (){
  78. _getSearchData();
  79. },
  80. textInputAction: TextInputAction.search,
  81. ),
  82. ),
  83. ),
  84. _searchStr.text.isNotEmpty? Container(
  85. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44-hsp(88),
  86. child: ListView.separated(
  87. itemBuilder: (context,index){
  88. return GestureDetector(
  89. onTap: (){
  90. Map city = {};
  91. if(widget.isHome==true){
  92. city['id'] = '';
  93. city['name'] = _searchList[index]['name'];
  94. city['initials'] = '';
  95. city['country'] = '';
  96. city['area'] = '';
  97. city['home'] = 1;
  98. city['airplaneName'] = '';
  99. }else{
  100. city['id'] = _searchList[index]['id'];
  101. city['name'] = _searchList[index]['display'];
  102. city['initials'] = _searchList[index]['id'];
  103. city['country'] = _searchList[index]['country'];
  104. city['area'] = _searchList[index]['country'];
  105. city['home'] = 1;
  106. city['airplaneName'] = _searchList[index]['airplaneName'];
  107. }
  108. List indexList = [];
  109. for(int i = 0;i<_historyList.length;i++){
  110. if(_historyList[i]['name']==city['name']){
  111. indexList.add(i);
  112. }
  113. }
  114. indexList.forEach((element) {
  115. _historyList.removeAt(element);
  116. });
  117. _historyList.insert(0, city);
  118. SharedPreferences.getInstance().then((prefer){
  119. String historyStr = jsonEncode(_historyList);
  120. prefer.setString('cityHistory', historyStr);
  121. Navigator.of(context).pop(city);
  122. });
  123. },
  124. child: Container(
  125. color: Colors.white,
  126. height: hsp(100),
  127. padding: EdgeInsets.only(left: wsp(40),right: wsp(40)),
  128. alignment: Alignment.centerLeft,
  129. child: Row(
  130. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  131. children: [
  132. indicateString(_searchList[index]['display']!=null?'${_searchList[index]['display']}':'${_searchList[index]['name']}',
  133. _searchStr.text, _searchList[index]['airplaneName']!=null?_searchList[index]['airplaneName']:''),
  134. Icon(Icons.keyboard_arrow_right,size: hsp(40),color: Color(0xFFA4A5A5),)
  135. ],
  136. )
  137. ),
  138. );
  139. },
  140. separatorBuilder: (context,index){
  141. return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),);
  142. },
  143. itemCount: _searchList.length,
  144. ),
  145. ):Container(
  146. child: Row(
  147. children: [
  148. GestureDetector(
  149. child: Column(
  150. children: [
  151. Container(
  152. height: hsp(85),
  153. color: Colors.white,
  154. width: MediaQuery.of(context).size.width/2,
  155. alignment: Alignment.center,
  156. child: Text('国内城市',style: TextStyle(fontSize: zsp(30),color: Color(0xFF444444)),),
  157. ),
  158. Container(
  159. height: hsp(3),
  160. color: _selected==0?Color(0xFF007AFF):Colors.transparent,
  161. width: wsp(133),
  162. )
  163. ],
  164. ),
  165. onTap: (){
  166. setState(() {
  167. _selected = 0;
  168. });
  169. },
  170. ),
  171. GestureDetector(
  172. child: Column(
  173. children: [
  174. Container(
  175. height: hsp(88),
  176. color: Colors.white,
  177. width: MediaQuery.of(context).size.width/2,
  178. alignment: Alignment.center,
  179. child: Text('国际/中国港澳台',style: TextStyle(fontSize: zsp(30),color: Color(0xFF444444)),),
  180. ),
  181. Container(
  182. height: hsp(3),
  183. color: _selected==1?Color(0xFF007AFF):Colors.transparent,
  184. width: wsp(133),
  185. )
  186. ],
  187. ),
  188. onTap: (){
  189. setState(() {
  190. _selected = 1;
  191. });
  192. },
  193. )
  194. ],
  195. ),
  196. ),
  197. Column(
  198. crossAxisAlignment: CrossAxisAlignment.start,
  199. children: [
  200. Container(
  201. child: Row(
  202. children: [
  203. Text('定位/历史',style: TextStyle(fontSize: zsp(24),color: Color(0xFF9A9A9A)),),
  204. GestureDetector(
  205. child: Icon(Icons.delete,color: Color(0xFF9A9A9A),size: hsp(40),),
  206. onTap: (){
  207. SharedPreferences.getInstance().then((prefer){
  208. _historyList.clear();
  209. String historyStr = jsonEncode(_historyList);
  210. prefer.setString('cityHistory', historyStr);
  211. setState(() {});
  212. });
  213. },
  214. )
  215. ],
  216. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  217. ),
  218. padding: EdgeInsets.only(left: wsp(24),right: wsp(24),top: hsp(34),bottom: hsp(10)),
  219. width: MediaQuery.of(context).size.width,
  220. ),
  221. Container(
  222. margin: EdgeInsets.only(left: zsp(8)),
  223. child: Wrap(
  224. alignment: WrapAlignment.start,
  225. children: <Widget>[
  226. for (int i =0;i<_historyList.length+1;i++ ) GestureDetector(
  227. behavior: HitTestBehavior.opaque,
  228. onTap: (){
  229. if(widget.isHome==true){
  230. Map city = {};
  231. city['id'] = '';
  232. city['name'] = i>0?'${_historyList[i-1]['name']}':_location;
  233. city['initials'] = '';
  234. city['country'] = '';
  235. city['area'] = '';
  236. city['home'] = 1;
  237. city['airplaneName'] = '';
  238. List indexList = [];
  239. for(int i = 0;i<_historyList.length;i++){
  240. if(_historyList[i]['name']==city['name']){
  241. indexList.add(i);
  242. }
  243. }
  244. indexList.forEach((element) {
  245. _historyList.removeAt(element);
  246. });
  247. _historyList.insert(0, city);
  248. SharedPreferences.getInstance().then((prefer){
  249. String historyStr = jsonEncode(_historyList);
  250. prefer.setString('cityHistory', historyStr);
  251. Navigator.of(context).pop(city);
  252. });
  253. }else{
  254. if(i>0){
  255. _searchStr.text = '${_historyList[i-1]['name']}';
  256. }else{
  257. _searchStr.text = _location;
  258. }
  259. }
  260. _getSearchData();
  261. },
  262. child: Container(
  263. margin: EdgeInsets.symmetric(vertical: hsp(16), horizontal: hsp(16)),
  264. decoration: BoxDecoration(
  265. color: Colors.white,
  266. borderRadius: BorderRadius.all(Radius.circular(5))
  267. ),
  268. padding: EdgeInsets.only(left: wsp(40), right: wsp(40),top: hsp(20),bottom: hsp(20)),
  269. child: i==0?Row(
  270. mainAxisSize: MainAxisSize.min,
  271. children: [
  272. Icon(Icons.location_on,color: Color(0xFF007AFF),size: hsp(40),),
  273. Text(' $_location', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  274. ],
  275. ):Text('${_historyList[i-1]['name']}', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  276. ),
  277. )
  278. ]
  279. ),
  280. ),
  281. Container(
  282. child: Text('热门城市',style: TextStyle(fontSize: zsp(24),color: Color(0xFF9A9A9A)),),
  283. padding: EdgeInsets.only(left: wsp(24),right: wsp(24),top: hsp(34),bottom: hsp(10)),
  284. width: MediaQuery.of(context).size.width,
  285. ),
  286. Container(
  287. margin: EdgeInsets.only(bottom: hsp(10),left: zsp(8)),
  288. child: Wrap(
  289. alignment: WrapAlignment.start,
  290. children: <Widget>[
  291. for (Map item in (_selected==0?_hotList:_hotList2)) GestureDetector(
  292. behavior: HitTestBehavior.opaque,
  293. onTap: () {
  294. if (widget.isHome == true) {
  295. Map city = {};
  296. city['id'] = '';
  297. city['name'] = '${item['name']}';
  298. city['initials'] = '';
  299. city['country'] = '';
  300. city['area'] = '';
  301. city['home'] = 1;
  302. city['airplaneName'] = '';
  303. List indexList = [];
  304. for(int i = 0;i<_historyList.length;i++){
  305. if(_historyList[i]['name']==city['name']){
  306. indexList.add(i);
  307. }
  308. }
  309. indexList.forEach((element) {
  310. _historyList.removeAt(element);
  311. });
  312. _historyList.insert(0, city);
  313. SharedPreferences.getInstance().then((prefer){
  314. String historyStr = jsonEncode(_historyList);
  315. prefer.setString('cityHistory', historyStr);
  316. Navigator.of(context).pop(city);
  317. });
  318. } else {
  319. _searchStr.text = '${item['name']}';
  320. _getSearchData();
  321. }
  322. },
  323. child: Container(
  324. margin: EdgeInsets.symmetric(vertical: hsp(16), horizontal: hsp(16)),
  325. decoration: BoxDecoration(
  326. color: Colors.white,
  327. borderRadius: BorderRadius.all(Radius.circular(5))
  328. ),
  329. padding: EdgeInsets.only(left: wsp(40), right: wsp(40),top: hsp(20),bottom: hsp(20)),
  330. child: Text('${item['name']}', style: TextStyle(fontSize: zsp(26),color: Color(0xFF16181A)), textAlign: TextAlign.center,),
  331. ),
  332. )
  333. ]
  334. ),
  335. ),
  336. ],
  337. ),
  338. Container(
  339. color: Colors.white,
  340. child: ListView.separated(
  341. shrinkWrap: true,
  342. itemBuilder: (context,index){
  343. return Column(
  344. children: [
  345. Container(
  346. color: Colors.white,
  347. height: hsp(60),
  348. padding: EdgeInsets.only(left: wsp(30)),
  349. alignment: Alignment.centerLeft,
  350. child: Text('${_dataArray[index]['word']}',style: TextStyle(fontSize: zsp(24),color: Color(0xFFA4A5A5),fontWeight: FontWeight.bold),),
  351. ),
  352. Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),),
  353. ListView.separated(
  354. itemBuilder: (context,indexSub){
  355. return GestureDetector(
  356. behavior: HitTestBehavior.opaque,
  357. onTap: (){
  358. Map city = (_selected==0?_dataArray:_dataArray2)[index]['mapList'][indexSub];
  359. if (widget.isHome == true) {
  360. Map city2 = {};
  361. city2['id'] = '';
  362. city2['name'] = '${city['name']}';
  363. city2['initials'] = '';
  364. city2['country'] = '';
  365. city2['area'] = '';
  366. city2['home'] = 1;
  367. city2['airplaneName'] = '';
  368. List indexList = [];
  369. for(int i = 0;i<_historyList.length;i++){
  370. if(_historyList[i]['name']==city['name']){
  371. indexList.add(i);
  372. }
  373. }
  374. indexList.forEach((element) {
  375. _historyList.removeAt(element);
  376. });
  377. _historyList.insert(0, city);
  378. SharedPreferences.getInstance().then((prefer){
  379. String historyStr = jsonEncode(_historyList);
  380. prefer.setString('cityHistory', historyStr);
  381. Navigator.of(context).pop(city);
  382. });
  383. } else {
  384. _searchStr.text = '${city['name']}';
  385. _getSearchData();
  386. }
  387. },
  388. child: Container(
  389. color: Colors.white,
  390. height: hsp(100),
  391. padding: EdgeInsets.only(left: wsp(40)),
  392. alignment: Alignment.centerLeft,
  393. child: Text('${(_selected==0?_dataArray:_dataArray2)[index]['mapList'][indexSub]['name']}',style: TextStyle(fontSize: zsp(32),color: Color(0xFF16181A)),),
  394. ),
  395. );
  396. },
  397. separatorBuilder: (context,indexSub){
  398. return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),);
  399. },
  400. itemCount: ((_selected==0?_dataArray:_dataArray2)[index]['mapList']).length,
  401. shrinkWrap: true,
  402. physics: NeverScrollableScrollPhysics(),
  403. )
  404. ],
  405. );
  406. },
  407. separatorBuilder: (context,index){
  408. return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFF1F2F3),);
  409. },
  410. itemCount: (_selected==0?_dataArray:_dataArray2).length,
  411. physics: NeverScrollableScrollPhysics(),
  412. ),
  413. ),
  414. ],
  415. ),
  416. ),
  417. ),
  418. ),
  419. );
  420. }
  421. Future<void> _refresh() async{
  422. SharedPreferences prefer = await SharedPreferences.getInstance();
  423. _location = prefer.getString('location')??'';
  424. _page = 1;
  425. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/region/region',parameter: {'pageNum':_page},isLoading: true,isToken: false);
  426. if(dict!=null){
  427. SharedPreferences prefer = await SharedPreferences.getInstance();
  428. String historyStr = prefer.getString('cityHistory')??'[]';
  429. _historyList = jsonDecode(historyStr);
  430. _hotList.clear();
  431. _hotList2.clear();
  432. _dataArray = dict['region']['1']['resultList'];
  433. _dataArray.forEach((element) {
  434. List array = element['mapList'];
  435. array.forEach((elementSub) {
  436. if(elementSub['isHot']==1){
  437. _hotList.add(elementSub);
  438. }
  439. });
  440. });
  441. _dataArray2 = dict['region']['2']['resultList'];
  442. _dataArray2.forEach((element) {
  443. List array = element['mapList'];
  444. array.forEach((elementSub) {
  445. if(elementSub['isHot']==1){
  446. _hotList2.add(elementSub);
  447. }
  448. });
  449. });
  450. setState(() {
  451. });
  452. }
  453. }
  454. Future<void> _loadMore() async{
  455. _page++;
  456. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/region/region',parameter: {'pageNum':_page},isLoading: true,isToken: false);
  457. if(dict!=null){
  458. List array1 = dict['region']['1']['resultList'];
  459. array1.forEach((element) {
  460. List array = element['mapList'];
  461. array.forEach((elementSub) {
  462. if(elementSub['isHot']==1){
  463. _hotList.add(elementSub);
  464. }
  465. });
  466. });
  467. _dataArray.addAll(array1);
  468. List array2 = dict['region']['2']['resultList'];
  469. array2.forEach((element) {
  470. List array = element['mapList'];
  471. array.forEach((elementSub) {
  472. if(elementSub['isHot']==1){
  473. _hotList2.add(elementSub);
  474. }
  475. });
  476. });
  477. _dataArray2.addAll(array2);
  478. setState(() {});
  479. }
  480. }
  481. _getSearchData() async{
  482. Map request = {};
  483. request['cid'] = _searchStr.text;
  484. if(widget.isHome==true){
  485. request['type'] = 1;
  486. }
  487. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/airplane/Poi/airportById',parameter: request,isLoading: false,isToken: false);
  488. if(dict!=null){
  489. setState(() {
  490. _searchList = dict['data'];
  491. });
  492. }
  493. }
  494. // List _siftCity() {
  495. // List array = [];
  496. // _searchList.forEach((element) {
  497. // if(((element['display']).split(_searchStr.text)).length>1){
  498. // array.add(element);
  499. // }
  500. // });
  501. // return array;
  502. // }
  503. }