YSCity.dart 15 KB

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