app-starlock/star_lock/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_page.dart
2024-01-23 17:29:18 +08:00

426 lines
15 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';
import '../../../../appRouters.dart';
import '../../../../app_settings/app_colors.dart';
import '../../../../blue/blue_manage.dart';
import '../../../../tools/appRouteObserver.dart';
import '../../../../tools/titleAppBar.dart';
import '../../../../translations/trans_lib.dart';
import 'lockAddressGaoDe_logic.dart';
class LockAddressGaoDePage extends StatefulWidget {
const LockAddressGaoDePage({Key? key}) : super(key: key);
@override
State<LockAddressGaoDePage> createState() => _LockAddressGaoDePageState();
}
class _LockAddressGaoDePageState extends State<LockAddressGaoDePage> with RouteAware{
final logic = Get.put(LockAddressGaoDeLogic());
final state = Get.find<LockAddressGaoDeLogic>().state;
// 高德地图
static const AMapApiKey amapApiKeys = AMapApiKey(iosKey: '883a3355d2d77c2fdc2667030dc97ffe', androidKey: '11d49b3f4fc09c04a02bbb7500925ba2');
AMapController? mapController;
AMapFlutterLocation location = AMapFlutterLocation();
PermissionStatus? permissionStatus;
Map<String, Object>? addressInfo;
@override
void initState() {
super.initState();
AMapFlutterLocation.updatePrivacyAgree(true);
AMapFlutterLocation.updatePrivacyShow(true, true);
requestPermission();
AMapFlutterLocation.setApiKey("11d49b3f4fc09c04a02bbb7500925ba2", "883a3355d2d77c2fdc2667030dc97ffe");
// EasyLoading.show();
// Future.delayed(const Duration(seconds: 10), () { //asynchronous delay
// print("10s之后 菊花没有隐藏的话,强制隐藏菊花");
// if(EasyLoading.isShow){
// EasyLoading.dismiss();
// }
// });
// requestLocation();
}
Future<void> requestPermission() async {
final status = await Permission.location.request();
print("Permission.location.request()=status:$status");
permissionStatus = status;
if(Platform.isIOS){
_setLocationOption();
requestIOSLocation();
}
switch (status) {
case PermissionStatus.denied:
print("拒绝");
break;
case PermissionStatus.granted:
if(Platform.isIOS){
// _setLocationOption();
// requestIOSLocation();
}else{
requestAndroidLocation();
location.startLocation();
}
break;
case PermissionStatus.limited:
print("限制");
break;
case PermissionStatus.permanentlyDenied:
print("永久的否认");
break;
case PermissionStatus.provisional:
print("临时");
break;
default:
print("其他状态");
// requestLocation();
break;
}
}
Future<void> requestAndroidLocation() async {
location.onLocationChanged().listen((event) {
print("listenLocationChanged$event");
// EasyLoading.dismiss();
if (event.isNotEmpty) {
setState(() {
addressInfo = event;
});
location.stopLocation();
}
});
}
Future<void> requestIOSLocation() async {
location = AMapFlutterLocation()
..setLocationOption(AMapLocationOption())
..onLocationChanged().listen((event) {
print("listenLocationChanged$event");
// EasyLoading.dismiss();
if (event.isNotEmpty) {
setState(() {
addressInfo = event;
});
// location.stopLocation();
}
})
..startLocation();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: TitleAppBar(
barTitle: TranslationLoader.lanKeys!.lockAddress!.tr,
haveBack: true,
backgroundColor: AppColors.mainColor,
// actionsList: [
// TextButton(
// child: Text(
// TranslationLoader.lanKeys!.next!.tr,
// style: TextStyle(color: Colors.white, fontSize: 24.sp),
// ),
// onPressed: () {
// if(addressInfo!.isEmpty){
// Toast.show(msg:"还未获取到位置信息哦,请耐心等待一下!");
// return;
// }
// Get.toNamed(Routers.saveLockPage, arguments: {
// "addressInfo": addressInfo,
// "pwdTimestamp": state.pwdTimestamp.value,
// });
// // Navigator.pushNamed(context, Routers.saveLockPage);
// },
// ),
// ],
),
body: ListView(
children: [
Container(
margin: EdgeInsets.only(left: 25.w, top: 40.h, bottom: 40.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text("地理位置", style: TextStyle(fontSize: 50.sp)),
],
),
),
SizedBox(
child:
addressInfo != null ?
Column(
children: [
SizedBox(
height: 1.sw/5*4,
width: 1.sw,
child: AMapWidget(
apiKey: amapApiKeys,
// 初始化地图中心
initialCameraPosition: (
CameraPosition(
target: LatLng(double.parse(addressInfo!['latitude'].toString()), double.parse(addressInfo!['longitude'].toString())),
zoom: 10.0,
)
),
//定位小蓝点
myLocationStyleOptions: MyLocationStyleOptions(
true,
),
// 普通地图normal,卫星地图satellite,夜间视图night,导航视图 navi,公交视图bus,
mapType: MapType.normal,
// 缩放级别范围
minMaxZoomPreference: const MinMaxZoomPreference(3, 20),
// 隐私政策包含高德 必须填写
privacyStatement: const AMapPrivacyStatement(hasAgree: true, hasContains: true, hasShow: true),
// 地图创建成功时返回AMapController
onMapCreated: (AMapController controller) {
mapController = controller;
},
),
),
Container(
// color: Colors.red,
height: 45.h,
margin: EdgeInsets.only(left: 25.w, top: 20.h, right: 25.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text("检查以确保以下地址是正确的", style: TextStyle(fontSize: 24.sp)),
],
),
),
// SizedBox(height: 20.h),
Container(
// color: Colors.red,
// height: 45.h,
margin: EdgeInsets.only(left: 25.w, top: 20.h, right: 25.w),
child:Column(
children: [
Row(
children: [
Expanded(
child: Text(addressInfo!["address"].toString() ?? "", style: const TextStyle(color: Colors.grey, fontSize: 16, fontWeight: FontWeight.w500, overflow: TextOverflow.clip))
),
],
),
SizedBox(height: 5.h),
Container(height: 1.h, color: AppColors.mainColor,),
],
)
),
],
) : SizedBox(
height: 1.sw/5*4 + 65.h*2,
child: const Center(child: Text('地图加载中,请稍候。。。')))
,
),
SizedBox(height:200.h),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TextButton(
child: Text(
"跳过",
style: TextStyle(color: Colors.black, fontSize: 24.sp),
),
onPressed: () {
Get.toNamed(Routers.addLockSelectCountryPage, arguments: {
"addressInfo": {},
"pwdTimestamp": state.pwdTimestamp.value,
"lockInfo": state.lockInfo,
"featureValue": state.featureValue,
"featureSettingValue": state.featureSettingValue,
"featureSettingParams": state.featureSettingParams,
});
},
),
TextButton(
child: Text(
TranslationLoader.lanKeys!.next!.tr,
style: TextStyle(color: Colors.black, fontSize: 24.sp),
),
onPressed: () {
if(addressInfo!.isEmpty){
logic.showToast("还未获取到位置信息哦,请耐心等待一下!");
return;
}
Get.toNamed(Routers.saveLockPage, arguments: {
"addressInfo": addressInfo,
"pwdTimestamp": state.pwdTimestamp.value,
"lockInfo": state.lockInfo,
"featureValue": state.featureValue,
"featureSettingValue": state.featureSettingValue,
"featureSettingParams": state.featureSettingParams,
"isFromMap": 1,
});
// Navigator.pushNamed(context, Routers.saveLockPage);
},
),
],
)
// Positioned(
// left: 20.w, right: 20.w, bottom: 100.h,
// child: Container(
// // height: h(106),
// decoration: BoxDecoration(
// color: AppColors.mainColor,
// borderRadius: BorderRadius.circular(15.w),
// ),
// child:Column(
// children: [
// Container(
// // height: h(53),
// padding: EdgeInsets.only(top: 15.h, bottom: 15, left:15.w, right: 15.w),
// child: Row(
// children: [
// Image.asset('images/main/icon_addUserShowAddress.png', width: 30.w, height: 30.w),
// SizedBox(width: 10.w),
// Expanded(
// child: Text(addressInfo!["address"].toString() ?? "", style: const TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500, overflow: TextOverflow.clip))
// ),
// ],
// ),
// ),
// // Container(height: 1.h, color: const Color(0xFF021732),),
// // Container(
// // // height: h(52),
// // padding: EdgeInsets.only(top: 15.h, bottom: 15, left:15.w, right: 15.w),
// // child: Row(
// // children: [
// // Image.asset('images/main/icon_addUserAddressShowTime.png', width: 30.w, height: 30.w),
// // SizedBox(width: 10.w,),
// // Expanded(
// // child: Text(DateTool().getNowDateYMDHM(), style: const TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),)
// // ),
// // ],
// // ),
// // ),
// ],
// )
// ),),
],
),
);
}
///设置定位参数
void _setLocationOption() {
AMapLocationOption locationOption = AMapLocationOption();
///是否单次定位
locationOption.onceLocation = true;
///是否需要返回逆地理信息
locationOption.needAddress = true;
///逆地理信息的语言类型
locationOption.geoLanguage = GeoLanguage.DEFAULT;
locationOption.desiredLocationAccuracyAuthorizationMode = AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;
locationOption.fullAccuracyPurposeKey = "AMapLocationScene";
///设置Android端连续定位的定位间隔
locationOption.locationInterval = 2000;
///设置Android端的定位模式<br>
///可选值:<br>
///<li>[AMapLocationMode.Battery_Saving]</li>
///<li>[AMapLocationMode.Device_Sensors]</li>
///<li>[AMapLocationMode.Hight_Accuracy]</li>
locationOption.locationMode = AMapLocationMode.Battery_Saving;
///设置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.BestForNavigation;
///设置iOS端是否允许系统暂停定位
locationOption.pausesLocationUpdatesAutomatically = false;
///将定位参数设置给定位插件
location.setLocationOption(locationOption);
}
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
/// 路由订阅
AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!);
}
@override
void dispose() {
AppRouteObserver().routeObserver.unsubscribe(this);
super.dispose();
BlueManage().disconnect(BlueManage().connectDeviceMacAddress);
location.stopLocation();
location.destroy();
Get.log("地图界面销毁了");
}
/// 从上级界面进入 当前界面即将出现
@override
void didPush() {
super.didPush();
}
/// 返回上一个界面 当前界面即将消失
@override
void didPop() {
super.didPop();
}
/// 从下级返回 当前界面即将出现
@override
void didPopNext() {
super.didPopNext();
}
/// 进入下级界面 当前界面即将消失
@override
void didPushNext() {
super.didPushNext();
location.stopLocation();
location.destroy();
}
}