near_list.dart 9.8 KB

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