123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- 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<StatefulWidget> createState() {
- return NearListState();
- }
- }
- class NearListState extends State<NearList> {
- // NewsDetailItem item = NewsDetailItem();
- Map<String, Object> _locationResult;
- StreamSubscription<Map<String, Object>> _locationListener;
- AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin();
-
- ScrollController _scrollController = new ScrollController();
- bool _hasData = false;
- List<dynamic>nearList = [];
- void initState() {
- super.initState();
- ///移除定位监听
- if (null != _locationListener) {
- _locationListener.cancel();
- }
- ///销毁定位
- if (null != _locationPlugin) {
- _locationPlugin.destroy();
- }
-
- _locationListener = _locationPlugin
- .onLocationChanged()
- .listen((Map<String, Object> 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<bool> 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端的定位模式<br>
- ///可选值:<br>
- ///<li>[AMapLocationMode.Battery_Saving]</li>
- ///<li>[AMapLocationMode.Device_Sensors]</li>
- ///<li>[AMapLocationMode.Hight_Accuracy]</li>
- locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
- ///设置iOS端的定位最小更新距离<br>
- locationOption.distanceFilter = -1;
- ///设置iOS端期望的定位精度
- /// 可选值:<br>
- /// <li>[DesiredAccuracy.Best] 最高精度</li>
- /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
- /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
- /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
- /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
- 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: <Widget>[
- 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);
- List<dynamic>list;
-
- List<Widget> 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: <Widget>[
- Expanded(
- child: GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: (){
- FlutterStars.SpUtil.putObject("nearUserDetail", list[i]);
- NavigatorUtils.push(context, FriendsRouter.nearDetail);
-
- },
- child:Row(
- crossAxisAlignment: CrossAxisAlignment.center,
- children:<Widget>[
- 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: <Widget>[
- 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),
- ),
- );
- }
- }
|