near_list.dart 9.6 KB

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