near_list.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import 'dart:async';
  2. import 'package:amap_location_flutter_plugin/amap_location_flutter_plugin.dart';
  3. import 'package:amap_location_flutter_plugin/amap_location_option.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:liftmanager/internal/friends/friends_router.dart';
  6. import 'package:liftmanager/res/gaps.dart';
  7. import 'package:liftmanager/net/api_service.dart';
  8. import 'package:liftmanager/utils/toast.dart';
  9. import 'package:liftmanager/widgets/app_bar.dart';
  10. import 'package:liftmanager/internal/search/search_router.dart';
  11. import 'package:liftmanager/widgets/app_search_bar.dart';
  12. import 'package:liftmanager/res/resources.dart';
  13. import 'package:liftmanager/routers/fluro_navigator.dart';
  14. import 'package:liftmanager/widgets/load_image.dart';
  15. import 'package:flutter_screenutil/flutter_screenutil.dart';
  16. import 'package:flustars/flustars.dart' as FlutterStars;
  17. import 'package:permission_handler/permission_handler.dart';
  18. import 'package:liftmanager/common/common.dart';
  19. class NearList extends StatefulWidget {
  20. // QuestionList(this.index);
  21. // final String index;
  22. @override
  23. State<StatefulWidget> createState() {
  24. return NearListState();
  25. }
  26. }
  27. class NearListState extends State<NearList> {
  28. // NewsDetailItem item = NewsDetailItem();
  29. Map<String, Object> _locationResult;
  30. StreamSubscription<Map<String, Object>> _locationListener;
  31. AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin();
  32. ScrollController _scrollController = new ScrollController();
  33. bool _hasData = false;
  34. List<dynamic>nearList = [];
  35. void initState() {
  36. super.initState();
  37. _locationListener = _locationPlugin
  38. .onLocationChanged()
  39. .listen((Map<String, Object> result) {
  40. setState(() {
  41. _locationPlugin.stopLocation();
  42. _locationResult = result;
  43. // address latitude longitude
  44. _locationResult.forEach((key, value) {
  45. print(111);
  46. print('key:$key :');
  47. print('value:$value :');
  48. });
  49. dynamic obj = {
  50. "lat": _locationResult["latitude"],
  51. "lon": _locationResult["longitude"],
  52. "userId":int.parse(FlutterStars.SpUtil.getString(Constant.userId))
  53. };
  54. getNearList(obj);
  55. });
  56. });
  57. getLocation();
  58. }
  59. ///获取定位权限
  60. Future<bool> requestPermission() async {
  61. final permissions = await PermissionHandler()
  62. .requestPermissions([PermissionGroup.location]);
  63. if (permissions[PermissionGroup.location] == PermissionStatus.granted) {
  64. return true;
  65. } else {
  66. NavigatorUtils.goBack(context);
  67. toasts('需要定位权限!');
  68. return false;
  69. }
  70. }
  71. void _setLocationOption() {
  72. if (null != _locationPlugin) {
  73. AMapLocationOption locationOption = new AMapLocationOption();
  74. ///是否单次定位
  75. locationOption.onceLocation = true;
  76. ///是否需要返回逆地理信息
  77. locationOption.needAddress = true;
  78. ///逆地理信息的语言类型
  79. locationOption.geoLanguage = GeoLanguage.DEFAULT;
  80. ///设置Android端连续定位的定位间隔
  81. locationOption.locationInterval = 20000;
  82. ///设置Android端的定位模式<br>
  83. ///可选值:<br>
  84. ///<li>[AMapLocationMode.Battery_Saving]</li>
  85. ///<li>[AMapLocationMode.Device_Sensors]</li>
  86. ///<li>[AMapLocationMode.Hight_Accuracy]</li>
  87. locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
  88. ///设置iOS端的定位最小更新距离<br>
  89. locationOption.distanceFilter = -1;
  90. ///设置iOS端期望的定位精度
  91. /// 可选值:<br>
  92. /// <li>[DesiredAccuracy.Best] 最高精度</li>
  93. /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
  94. /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
  95. /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
  96. /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
  97. locationOption.desiredAccuracy = DesiredAccuracy.NearestTenMeters;
  98. ///设置iOS端是否允许系统暂停定位
  99. locationOption.pausesLocationUpdatesAutomatically = false;
  100. ///将定位参数设置给定位插件
  101. _locationPlugin.setLocationOption(locationOption);
  102. }
  103. }
  104. ///获取位置信息
  105. getLocation() async {
  106. if (await requestPermission()) {
  107. if (null != _locationPlugin) {
  108. ///开始定位之前设置定位参数
  109. _setLocationOption();
  110. _locationPlugin.startLocation();
  111. }
  112. }
  113. }
  114. Future getNearList(obj) async {
  115. await NewApiService().nearListNoPage(obj, onSuccess: (res) {
  116. // LogUtil.d(JsonEncoder().convert(res.records));
  117. // LogUtil.d("5555664");
  118. nearList=res;
  119. _hasData = true;
  120. setState(() {});
  121. }, onError: (code, msg) {
  122. toasts(msg);
  123. });
  124. }
  125. @override
  126. void dispose() {
  127. _scrollController.dispose();
  128. ///移除定位监听
  129. if (null != _locationListener) {
  130. _locationListener.cancel();
  131. }
  132. ///销毁定位
  133. if (null != _locationPlugin) {
  134. _locationPlugin.destroy();
  135. }
  136. super.dispose();
  137. }
  138. @override
  139. Widget build(BuildContext context) {
  140. double width = MediaQuery.of(context).size.width;
  141. return Scaffold(
  142. appBar: MyAppBar(
  143. centerTitle: "附近的人",
  144. ),
  145. body:_hasData?Container(
  146. child:ListView(
  147. children: <Widget>[
  148. OrderListWidget(list:nearList)
  149. ]
  150. ),
  151. ):Center(
  152. child: Image.asset(
  153. "assets/images/search_friends.gif",
  154. width: width*0.7,
  155. // height: ScreenUtil().setWidth(43),
  156. // alignment: Alignment.centerLeft,
  157. ),
  158. ),
  159. );
  160. }
  161. }
  162. class OrderListWidget extends StatelessWidget {
  163. OrderListWidget({Key key,this.list}) : super(key: key);
  164. List<dynamic>list;
  165. List<Widget> getListWdiget(context) => list.asMap().keys.map((i){
  166. double width = MediaQuery.of(context).size.width;
  167. return Container(
  168. // height: 20,
  169. padding:EdgeInsets.only(bottom:ScreenUtil().setWidth(10),top:ScreenUtil().setWidth(10)),
  170. child: Row(
  171. crossAxisAlignment: CrossAxisAlignment.start,
  172. children: <Widget>[
  173. Expanded(
  174. child: GestureDetector(
  175. behavior: HitTestBehavior.opaque,
  176. onTap: (){
  177. FlutterStars.SpUtil.putObject("nearUserDetail", list[i]);
  178. NavigatorUtils.push(context, FriendsRouter.nearDetail);
  179. },
  180. child:Row(
  181. crossAxisAlignment: CrossAxisAlignment.center,
  182. children:<Widget>[
  183. Container(
  184. margin: EdgeInsets.only(left:ScreenUtil().setWidth(10),right:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(15)),
  185. decoration: BoxDecoration(
  186. borderRadius: BorderRadius.circular(ScreenUtil().setWidth(22)),
  187. ),
  188. child: ClipRRect(
  189. borderRadius: BorderRadius.all(Radius.circular(22)),
  190. child: LoadNetworkImage(
  191. // image: AssetImage(i['img']),
  192. list[i].avatarUrl,
  193. width: ScreenUtil().setWidth(43),
  194. height:ScreenUtil().setWidth(43),
  195. // alignment: Alignment.centerLeft,
  196. ),
  197. )
  198. ),
  199. Expanded(
  200. child: Container(
  201. padding: EdgeInsets.only(right:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(15)),
  202. decoration: BoxDecoration(
  203. border: Border(
  204. bottom: BorderSide(width: 0.5, color: Colours.line),
  205. ),
  206. ),
  207. child:Column(
  208. crossAxisAlignment: CrossAxisAlignment.start,
  209. children: <Widget>[
  210. Container(
  211. child:Text(
  212. list[i].name??"",
  213. style: TextStyle(
  214. fontSize:ScreenUtil().setSp(17)
  215. ),
  216. textAlign:TextAlign.start,
  217. ),
  218. ),
  219. Container(
  220. child:Text(
  221. (list[i].distance.toString()??"")+"米以内",
  222. style: TextStyle(
  223. color:Color(0xff999999),
  224. fontSize:ScreenUtil().setSp(13)
  225. ),
  226. textAlign:TextAlign.start,
  227. ),
  228. ),
  229. ],
  230. )
  231. )
  232. )
  233. ]
  234. ),
  235. )
  236. ),
  237. ],
  238. ),
  239. );
  240. }).toList();
  241. @override
  242. Widget build(BuildContext context) {
  243. return Container(
  244. child: Column(
  245. crossAxisAlignment: CrossAxisAlignment.start,
  246. children: getListWdiget(context),
  247. ),
  248. );
  249. }
  250. }