import 'dart:async'; import 'package:amap_location_flutter_plugin/amap_location_flutter_plugin.dart'; import 'package:amap_location_flutter_plugin/amap_location_option.dart'; import 'package:flutter/material.dart'; import 'package:liftmanager/internal/friends/friends_router.dart'; import 'package:liftmanager/res/gaps.dart'; import 'package:liftmanager/net/api_service.dart'; import 'package:liftmanager/utils/toast.dart'; import 'package:liftmanager/widgets/app_bar.dart'; import 'package:liftmanager/internal/search/search_router.dart'; import 'package:liftmanager/widgets/app_search_bar.dart'; import 'package:liftmanager/res/resources.dart'; import 'package:liftmanager/routers/fluro_navigator.dart'; import 'package:liftmanager/widgets/load_image.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flustars/flustars.dart' as FlutterStars; import 'package:permission_handler/permission_handler.dart'; import 'package:liftmanager/common/common.dart'; class NearList extends StatefulWidget { // QuestionList(this.index); // final String index; @override State createState() { return NearListState(); } } class NearListState extends State { // NewsDetailItem item = NewsDetailItem(); Map _locationResult; StreamSubscription> _locationListener; AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin(); ScrollController _scrollController = new ScrollController(); bool _hasData = false; ListnearList = []; void initState() { super.initState(); _locationListener = _locationPlugin .onLocationChanged() .listen((Map result) { setState(() { _locationPlugin.stopLocation(); _locationResult = result; // address latitude longitude _locationResult.forEach((key, value) { print(111); print('key:$key :'); print('value:$value :'); }); dynamic obj = { "lat": _locationResult["latitude"], "lon": _locationResult["longitude"], "userId":int.parse(FlutterStars.SpUtil.getString(Constant.userId)) }; getNearList(obj); }); }); getLocation(); } ///获取定位权限 Future requestPermission() async { final permissions = await PermissionHandler() .requestPermissions([PermissionGroup.location]); if (permissions[PermissionGroup.location] == PermissionStatus.granted) { return true; } else { NavigatorUtils.goBack(context); toasts('需要定位权限!'); return false; } } void _setLocationOption() { if (null != _locationPlugin) { AMapLocationOption locationOption = new AMapLocationOption(); ///是否单次定位 locationOption.onceLocation = true; ///是否需要返回逆地理信息 locationOption.needAddress = true; ///逆地理信息的语言类型 locationOption.geoLanguage = GeoLanguage.DEFAULT; ///设置Android端连续定位的定位间隔 locationOption.locationInterval = 20000; ///设置Android端的定位模式
///可选值:
///
  • [AMapLocationMode.Battery_Saving]
  • ///
  • [AMapLocationMode.Device_Sensors]
  • ///
  • [AMapLocationMode.Hight_Accuracy]
  • locationOption.locationMode = AMapLocationMode.Hight_Accuracy; ///设置iOS端的定位最小更新距离
    locationOption.distanceFilter = -1; ///设置iOS端期望的定位精度 /// 可选值:
    ///
  • [DesiredAccuracy.Best] 最高精度
  • ///
  • [DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度
  • ///
  • [DesiredAccuracy.NearestTenMeters] 10米
  • ///
  • [DesiredAccuracy.Kilometer] 1000米
  • ///
  • [DesiredAccuracy.ThreeKilometers] 3000米
  • locationOption.desiredAccuracy = DesiredAccuracy.NearestTenMeters; ///设置iOS端是否允许系统暂停定位 locationOption.pausesLocationUpdatesAutomatically = false; ///将定位参数设置给定位插件 _locationPlugin.setLocationOption(locationOption); } } ///获取位置信息 getLocation() async { if (await requestPermission()) { if (null != _locationPlugin) { ///开始定位之前设置定位参数 _setLocationOption(); _locationPlugin.startLocation(); } } } Future getNearList(obj) async { await NewApiService().nearListNoPage(obj, onSuccess: (res) { // LogUtil.d(JsonEncoder().convert(res.records)); // LogUtil.d("5555664"); nearList=res; _hasData = true; setState(() {}); }, onError: (code, msg) { toasts(msg); }); } @override void dispose() { _scrollController.dispose(); ///移除定位监听 if (null != _locationListener) { _locationListener.cancel(); } ///销毁定位 if (null != _locationPlugin) { _locationPlugin.destroy(); } super.dispose(); } @override Widget build(BuildContext context) { double width = MediaQuery.of(context).size.width; return Scaffold( appBar: MyAppBar( centerTitle: "附近的人", ), body:_hasData?Container( child:ListView( children: [ OrderListWidget(list:nearList) ] ), ):Center( child: Image.asset( "assets/images/search_friends.gif", width: width*0.7, // height: ScreenUtil().setWidth(43), // alignment: Alignment.centerLeft, ), ), ); } } class OrderListWidget extends StatelessWidget { OrderListWidget({Key key,this.list}) : super(key: key); Listlist; List getListWdiget(context) => list.asMap().keys.map((i){ double width = MediaQuery.of(context).size.width; return Container( // height: 20, padding:EdgeInsets.only(bottom:ScreenUtil().setWidth(10),top:ScreenUtil().setWidth(10)), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: (){ FlutterStars.SpUtil.putObject("nearUserDetail", list[i]); NavigatorUtils.push(context, FriendsRouter.nearDetail); }, child:Row( crossAxisAlignment: CrossAxisAlignment.center, children:[ Container( margin: EdgeInsets.only(left:ScreenUtil().setWidth(10),right:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(15)), decoration: BoxDecoration( borderRadius: BorderRadius.circular(ScreenUtil().setWidth(22)), ), child: ClipRRect( borderRadius: BorderRadius.all(Radius.circular(22)), child: LoadNetworkImage( // image: AssetImage(i['img']), list[i].avatarUrl, width: ScreenUtil().setWidth(43), height:ScreenUtil().setWidth(43), // alignment: Alignment.centerLeft, ), ) ), Expanded( child: Container( padding: EdgeInsets.only(right:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(15)), decoration: BoxDecoration( border: Border( bottom: BorderSide(width: 0.5, color: Colours.line), ), ), child:Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( child:Text( list[i].name??"", style: TextStyle( fontSize:ScreenUtil().setSp(17) ), textAlign:TextAlign.start, ), ), Container( child:Text( (list[i].distance.toString()??"")+"米以内", style: TextStyle( color:Color(0xff999999), fontSize:ScreenUtil().setSp(13) ), textAlign:TextAlign.start, ), ), ], ) ) ) ] ), ) ), ], ), ); }).toList(); @override Widget build(BuildContext context) { return Container( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: getListWdiget(context), ), ); } }