near_list.dart 9.5 KB

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