YSChooseLocation.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'package:dio/dio.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:ysairplane2/base/YSBase.dart';
  6. import 'package:ysairplane2/tools/YSNetWorking.dart';
  7. import 'package:ysairplane2/tools/YSTools.dart';
  8. class YSChooseLocation extends StatefulWidget {
  9. final location;
  10. const YSChooseLocation({Key key, this.location}) : super(key: key);
  11. @override
  12. _YSChooseLocationState createState() => _YSChooseLocationState();
  13. }
  14. class _YSChooseLocationState extends State<YSChooseLocation> {
  15. MethodChannel _platform;
  16. String _coordinate = '40,116';
  17. @override
  18. Widget build(BuildContext context) {
  19. return YSBase(
  20. ystitle: '选择登机地址',
  21. yschild: Container(
  22. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44,
  23. width: MediaQuery.of(context).size.width,
  24. color: Colors.white,
  25. child: Stack(
  26. children: [
  27. platformView(
  28. platforms: (value){
  29. value.setMethodCallHandler((call) {
  30. if(call.method == "search"){
  31. List array = call.arguments;
  32. _showAlert(array);
  33. }else if(call.method == "coordinate"){
  34. _coordinate = call.arguments;
  35. if(widget.location!=null){
  36. print(widget.location);
  37. Map item = {'lat':widget.location['lat'],'lng':widget.location['lng']
  38. ,'title':widget.location['title'],'content':widget.location['content'],'type':0};
  39. _platform.invokeMethod('setLocation', item);
  40. _coordinate = '${item['lat']},${item['lng']}';
  41. }
  42. }
  43. return;
  44. });
  45. _platform = value;
  46. }
  47. ),
  48. Container(
  49. height: hsp(100),
  50. width: MediaQuery.of(context).size.width-hsp(80),
  51. margin: EdgeInsets.only(top: hsp(50),left: hsp(40)),
  52. decoration: BoxDecoration(
  53. color: Colors.white,
  54. borderRadius: BorderRadius.all(Radius.circular(5))
  55. ),
  56. padding: EdgeInsets.only(left: hsp(10),right: hsp(10)),
  57. child: CupertinoTextField(
  58. placeholder: '请输入你要登机的地点',
  59. style: TextStyle(fontSize: zsp(30),color: Color(0xFF666666)),
  60. decoration: BoxDecoration(),
  61. textInputAction: TextInputAction.search,
  62. onSubmitted: (value){
  63. if(value.length>0){
  64. _searchLocation(value);
  65. }
  66. },
  67. ),
  68. )
  69. ],
  70. ),
  71. )
  72. );
  73. }
  74. _showAlert(List array){
  75. showModalBottomSheet(
  76. context: context,
  77. backgroundColor: Colors.transparent,
  78. isScrollControlled: true,
  79. builder: (context){
  80. return Container(
  81. height: MediaQuery.of(context).size.height*0.5,
  82. width: MediaQuery.of(context).size.width,
  83. decoration: BoxDecoration(
  84. color: Colors.white,
  85. borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10))
  86. ),
  87. child: Column(
  88. children: [
  89. Container(
  90. height: 39.7,
  91. child: Text('选择地址',style: TextStyle(fontSize: zsp(32),color: Colors.black,fontWeight: FontWeight.bold),),
  92. alignment: Alignment.center,
  93. ),
  94. Divider(height: 0.3,thickness: 0.3,color: Colors.grey,),
  95. Container(
  96. height: MediaQuery.of(context).size.height*0.5-40,
  97. child: ListView.separated(
  98. itemBuilder: (context,index){
  99. return GestureDetector(
  100. onTap: (){
  101. Navigator.pop(context);
  102. Navigator.of(context).pop({'lat': array[index]['lat'],'lng': array[index]['lng'],'title':
  103. array[index]['title'],'content': array[index]['content'],'airplaneName':array[index]['title']});
  104. },
  105. behavior: HitTestBehavior.opaque,
  106. child: Container(
  107. padding: EdgeInsets.only(left: 15,right: 15,bottom: 5,top: 5),
  108. child: Column(
  109. crossAxisAlignment: CrossAxisAlignment.start,
  110. children: [
  111. Container(
  112. child: Text('${array[index]['title']}',style: TextStyle(fontSize: zsp(30),color: Colors.black,fontWeight: FontWeight.bold),),
  113. margin: EdgeInsets.only(bottom: 5),
  114. ),
  115. Text('${array[index]['content']}',style: TextStyle(fontSize: zsp(24),color: Color(0xFF999999)),),
  116. ],
  117. ),
  118. ),
  119. );
  120. },
  121. separatorBuilder: (context,index){
  122. return Divider(height: 0.3,thickness: 0.3,color: Colors.grey,);
  123. },
  124. itemCount: array.length,
  125. ),
  126. )
  127. ],
  128. ),
  129. );
  130. }
  131. );
  132. }
  133. _searchLocation(String searchStr) async{
  134. FocusScope.of(context).unfocus();
  135. Map<String, dynamic> httpHeaders = {
  136. 'Accept': 'application/json,*/*',
  137. 'Content-Type': 'application/json',
  138. };
  139. Response response = await Dio().get('https://apis.map.qq.com/ws/place/v1/search?keyword=$searchStr&boundary=nearby($_coordinate,10000)&'
  140. 'key=OOZBZ-NHIWS-OCAOO-6VB3W-ROSBH-ZYB7Z',queryParameters: Map<String, dynamic>.from({}),options: Options(receiveTimeout: outTime,headers: httpHeaders));
  141. Map dict = response.data;
  142. print('==========object');
  143. if(dict['status']==0){
  144. print(dict['data']);
  145. List array = dict['data'];
  146. List dataArray = [];
  147. int count = array.length>20?20:array.length;
  148. for(int i = 0;i<count;i++){
  149. Map item = array[i];
  150. dataArray.add({'title':'${item['title']}','airplaneName':'${item['address']}','content':'${item['address']}'
  151. ,'lat':'${item['location']['lat']}','lng':'${item['location']['lng']}'});
  152. }
  153. _showAlert(dataArray);
  154. }
  155. }
  156. }