near_list.dart 9.2 KB

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