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:amap_map_fluttify/amap_map_fluttify.dart'; // import 'package:flustars/flustars.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:liftmanager/internal/sign/model/sign_config_item.dart'; import 'package:liftmanager/internal/sign/model/sign_list_entity.dart'; import 'package:liftmanager/internal/sign/sign_router.dart'; import 'package:liftmanager/net/api_service.dart'; import 'package:liftmanager/res/resources.dart'; import 'package:liftmanager/routers/fluro_navigator.dart'; import 'package:liftmanager/utils/toast.dart'; import 'package:liftmanager/widgets/app_bar.dart'; import 'package:permission_handler/permission_handler.dart'; class SignPage extends StatefulWidget { @override State createState() { return SignPageState(); } } class SignPageState extends State { Map _locationResult; StreamSubscription> _locationListener; AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin(); //计时器。 Timer _timer; String _nowTimeStr = ""; LatLng latLng = LatLng(0, 0); String currentAddress = ""; SignConfigItem configItem = SignConfigItem(); List list = []; BuildContext mContext; AmapController _controller; startTime() async { _timer = Timer.periodic(Duration(seconds: 1), (timer) { if (!mounted) return; setState(() { _nowTimeStr = DateTime.now().toString().split(" ")[1].split(".")[0]; }); }); } @override void initState() { super.initState(); _locationListener = _locationPlugin .onLocationChanged() .listen((Map result) { setState(() { _locationPlugin.stopLocation(); _locationResult = result; // address latitude longitude _locationResult.forEach((key, value) { if (key == 'address') { currentAddress = '$value'; setState(() {}); } else if (key == 'latitude') { latLng.latitude = double.parse('$value'); setState(() {}); } else if (key == 'longitude') { latLng.longitude = double.parse('$value'); setState(() {}); } print(111); print('key:$key :'); print('value:$value :'); }); }); }); mContext = context; startTime(); getSignConfig(); getTodayList(); getLocation(); } 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); } } @override void didChangeDependencies() { super.didChangeDependencies(); mContext.dependOnInheritedWidgetOfExactType(); } getLocation() async { if (await requestPermission()) { if (null != _locationPlugin) { ///开始定位之前设置定位参数 _setLocationOption(); _locationPlugin.startLocation(); } } } @override void dispose() { _timer.cancel(); if (null != _locationListener) { _locationListener.cancel(); } ///销毁定位 if (null != _locationPlugin) { _locationPlugin.destroy(); } super.dispose(); } getSignConfig() { ApiService(context: context).signConfig( onSuccess: (res) { configItem = res; setState(() {}); }, onError: (code, msg) {}); } getTodayList() { ApiService(context: context).signList( 1, 10, DateTime.now().toString().split(" ")[0], onSuccess: (data) { list = data.rows; }, onError: (code, msg) { toasts(msg); }); } getSignAdd() { if (configItem.topTime.length == 0) { toasts("未设置打卡"); return; } if (currentAddress.length == 0) { toasts("定位正在初始化,请重试。"); return; } NavigatorUtils.pushResult(context, "${SignRouter.signAddPage}?address=${Uri.encodeComponent(currentAddress)}&coordinate=${latLng.longitude},${latLng.latitude}&toptime=${configItem.topTime}&downtime=${configItem.downTime}&toptimepm=${configItem.topTimePm}&downtimepm=${configItem.downTimePm}&toptimeot=${configItem.topTimeOt}&downtimeot=${configItem.downTimeOt}", (res) { getTodayList(); }); } Future requestPermission() async { final permissions = await PermissionHandler() .requestPermissions([PermissionGroup.location]); if (permissions[PermissionGroup.location] == PermissionStatus.granted) { // toasts("已经定位."); return true; } else { toasts('需要定位权限!'); return false; } } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( centerTitle: "考勤", actions: [ FlatButton( child: Text("考勤记录", key: const Key('actionName')), textColor: Colours.text, highlightColor: Colors.transparent, onPressed: () { NavigatorUtils.push(context, SignRouter.signListPage); }, ) ], ), body: Container( color: Colors.white, child: Column( children: [ // Container( // color: ThemeUtils.getTabsBg(context), // padding: EdgeInsets.fromLTRB(15, 10, 15, 10), // child: Row( // children: [ // Expanded( // flex: 1, // child: Text( // "当前位置:${currentAddress ?? ''}", // maxLines: 1, // overflow: TextOverflow.ellipsis, // ), // ) // ], // )), Container( height: ScreenUtil().setHeight(250), child: Stack( children: [ AmapView( mapType: MapType.Standard, showZoomControl: false, centerCoordinate: latLng, zoomLevel: 17, maskDelay: Duration(milliseconds: 500), onMapCreated: (controller) async { _controller = controller; await _controller?.showMyLocation(MyLocationOption( myLocationType: MyLocationType.Locate, )); }, ), Positioned( top: ScreenUtil().setHeight(140), left: ScreenUtil().setWidth(130), child: ClipRRect( borderRadius: BorderRadius.circular(4), child: Container( width: ScreenUtil().setWidth(222), padding: EdgeInsets.all(5), color: Colours.blue_app_main, child:Text( " 当前位置:${currentAddress ?? ''} ", style: TextStyle(color: Colors.white), maxLines: 1, overflow: TextOverflow.ellipsis, ), ), ) ) // AmapView( // mapType: MapType.Standard, // // 是否显示缩放控件 // showZoomControl: false, // // 是否显示指南针控件 // showCompass: false, // // 是否显示比例尺控件 // showScaleControl: false, // // 是否使能缩放手势 // zoomGesturesEnabled: true, // // 是否使能滚动手势 // scrollGesturesEnabled: true, // // 是否使能旋转手势 // rotateGestureEnabled: true, // // 是否使能倾斜手势 // tiltGestureEnabled: true, // // 缩放级别 // zoomLevel: 18, //// markers: _markers, // // 中心点坐标 // centerCoordinate: latLng, // // 标记 // onMapCreated: (controller) async { // if (await requestPermission()) { // await controller.showMyLocation(MyLocationOption(show: true)); // } //// await getLocation(); //// _markers = [ //// MarkerOption( //// title: "我的位置", //// latLng: latLng, //// iconUri: _assetsIcon, //// imageConfig: //// createLocalImageConfiguration(mContext), //// width: 28, //// height: 28) //// ]; //// controller.setCenterCoordinate(latLng); //// controller.showMyLocation(MyLocationOption()); //// setState(() {}); // }, // ), // BoxShadow(color: Colors.grey[300],offset: Offset(1, 1), // ///模糊阴影半径 // blurRadius: 5, // ) // Positioned( // right: 14, // bottom: 19, // child: GestureDetector( // onTap: () { // getSignAdd(); // }, // child: Container( // height: 75, // width: 75, // decoration: BoxDecoration( // color: Colours.app_main, // boxShadow: [ // ///阴影颜色/位置/大小等 // BoxShadow( // color: Colors.grey[300], offset: Offset(1, 1), // ///模糊阴影半径 // blurRadius: 5, // ), // BoxShadow( // color: Colors.grey[300], // offset: Offset(-1, -1), // blurRadius: 5), // BoxShadow( // color: Colors.grey[300], // offset: Offset(1, -1), // blurRadius: 5), // BoxShadow( // color: Colors.grey[300], // offset: Offset(-1, 1), // blurRadius: 5) // ], // borderRadius: BorderRadius.circular(73)), // child: Column( // mainAxisAlignment: MainAxisAlignment.center, // children: [ // Text( // "打卡", // style: TextStyle( // fontSize: 13, // fontWeight: FontWeight.w500, // color: Colors.white), // ), // Text("${_nowTimeStr}", // style: TextStyle( // fontSize: 13, // fontWeight: FontWeight.w500, // color: Colors.white)) // ], // ), // ), // ), // ), ], )), Container( // padding: EdgeInsets.only(left: 10), alignment: Alignment.centerLeft, height: 45, decoration: BoxDecoration( border: Border( bottom: BorderSide(width: 0.5, color: Colours.line), ), ), child:Row( children: [ Container( // margin: EdgeInsets.only(left: 10), height:13 ,width: 2,color: Colours.blue_app_main,), SizedBox(width: 10,), Text( "今日打卡记录", style: TextStyle(fontSize: 14, color: Colours.text), ), ], ) ), Expanded( flex: 1, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ list.length>0? Container( color: Colors.white, padding: const EdgeInsets.symmetric( horizontal: 15.0, vertical: 12.0), alignment: Alignment.centerLeft, child: RichText( text: TextSpan( // style: DefaultTextStyle.of(context).style, children: [ TextSpan( text: '${list[0].createDate}', style: TextStyle(color: Colours.text)), TextSpan(text: ' ${list[0].typeDesc}', style: TextStyle(color: Colours.blue_app_main) ), ])), ):Container(), list.length > 1 ? Container( height: 22, width: 2, color: Colours.new_dark_line, margin: EdgeInsets.only(left: 90), ) : Container(), list.length > 1 ? Container( color: Colors.white, padding: const EdgeInsets.symmetric( horizontal: 15.0, vertical: 12.0), alignment: Alignment.centerLeft, child: Text( "${list[1].createDate} ${list[1].typeDesc}", style: TextStyle(color: Colours.text_gray), ), ) : Container() ], ) // ListView.separated( // shrinkWrap: true, // itemCount: list.length, // separatorBuilder: (BuildContext context, int index) { // return Container( // height: 0.5, // color: Color(0xFFF5F5F5), // ); // }, // itemBuilder: (_, index) { // return Container( // color: Colors.white, // padding: const EdgeInsets.symmetric( // horizontal: 15.0, vertical: 12.0), // alignment: Alignment.centerLeft, // child: Row( // children: [ // Text("${list[index].createDate} ${list[index].typeDesc}"), // ], // ) // // Column( // // crossAxisAlignment: CrossAxisAlignment.start, // // mainAxisAlignment: MainAxisAlignment.start, // // children: [ // // // Text("类型:${list[index].typeDesc}"), // // // Text("时间:${list[index].createDate}"), // // // Text("地址:${list[index].address}"), // // // Text("状态:${list[index].statusDesc}") // // ], // // ), // ); // }, // ) ), GestureDetector( onTap: () { getSignAdd(); }, child: Container( height: 110, width: 110, decoration: BoxDecoration( color: Colours.blue_app_main, boxShadow: [ ///阴影颜色/位置/大小等 BoxShadow( color: Colors.grey[300], offset: Offset(1, 1), ///模糊阴影半径 blurRadius: 5, ), BoxShadow( color: Colors.grey[300], offset: Offset(-1, -1), blurRadius: 5), BoxShadow( color: Colors.grey[300], offset: Offset(1, -1), blurRadius: 5), BoxShadow( color: Colors.grey[300], offset: Offset(-1, 1), blurRadius: 5) ], borderRadius: BorderRadius.circular(73)), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text("${_nowTimeStr}", style: TextStyle( fontSize: 22, fontWeight: FontWeight.w500, color: Colors.white)), Text( "打卡", style: TextStyle( fontSize: 14, fontWeight: FontWeight.w500, color: Colors.white), ), ], ), ), ), SizedBox( height: ScreenUtil().setHeight(30), ) ], ), ), ); } }