app-starlock/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_page.dart

377 lines
13 KiB
Dart
Raw Normal View History

2023-09-28 18:05:23 +08:00
import 'dart:async';
import 'dart:io';
2023-09-28 18:05:23 +08:00
2024-07-31 20:02:38 +08:00
import 'package:amap_flutter_base/amap_flutter_base.dart';
2023-09-28 18:05:23 +08:00
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
2024-07-31 20:02:38 +08:00
import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:flutter/material.dart';
2023-09-28 18:05:23 +08:00
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
2024-07-31 20:02:38 +08:00
import 'package:star_lock/mine/addLock/lockAddress/gaode/lockAddressGaoDe_state.dart';
import 'package:star_lock/widget/permission/permission_dialog.dart';
2023-09-28 18:05:23 +08:00
import '../../../../appRouters.dart';
import '../../../../app_settings/app_colors.dart';
2023-12-27 10:47:58 +08:00
import '../../../../blue/blue_manage.dart';
2024-02-01 15:10:24 +08:00
import '../../../../flavors.dart';
import '../../../../tools/appRouteObserver.dart';
2023-09-28 18:05:23 +08:00
import '../../../../tools/titleAppBar.dart';
import 'lockAddressGaoDe_logic.dart';
2023-09-28 18:05:23 +08:00
class LockAddressGaoDePage extends StatefulWidget {
const LockAddressGaoDePage({Key? key}) : super(key: key);
@override
State<LockAddressGaoDePage> createState() => _LockAddressGaoDePageState();
}
class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>
with RouteAware {
2024-07-31 20:02:38 +08:00
final LockAddressGaoDeLogic logic = Get.put(LockAddressGaoDeLogic());
final LockAddressGaoDeState state = Get.find<LockAddressGaoDeLogic>().state;
2023-09-28 18:05:23 +08:00
// 高德地图
static AMapApiKey amapApiKeys =
AMapApiKey(iosKey: F.aMapKey.iosKey, androidKey: F.aMapKey.androidKey);
2023-09-28 18:05:23 +08:00
AMapController? mapController;
AMapFlutterLocation location = AMapFlutterLocation();
2023-09-28 18:05:23 +08:00
PermissionStatus? permissionStatus;
Map<String, Object>? addressInfo;
@override
void initState() {
super.initState();
AMapFlutterLocation.updatePrivacyAgree(true);
AMapFlutterLocation.updatePrivacyShow(true, true);
2024-02-01 15:10:24 +08:00
AMapFlutterLocation.setApiKey(F.aMapKey.androidKey, F.aMapKey.iosKey);
2024-09-08 02:00:00 +08:00
requestPermission();
2023-09-28 18:05:23 +08:00
}
// Future<void> requestPermission() async {
// final status = await Permission.location.request();
2024-04-26 15:38:59 +08:00
// AppLog.log("Permission.location.request()=status:$status");
// permissionStatus = status;
// if(Platform.isIOS){
// _setLocationOption();
// requestIOSLocation();
// }
// switch (status) {
// case PermissionStatus.denied:
2024-04-26 15:38:59 +08:00
// // AppLog.log("拒绝");
// break;
// case PermissionStatus.granted:
// if(Platform.isIOS){
// // _setLocationOption();
// // requestIOSLocation();
// }else{
// requestAndroidLocation();
// location.startLocation();
// }
// break;
// case PermissionStatus.limited:
2024-04-26 15:38:59 +08:00
// // AppLog.log("限制");
// break;
// case PermissionStatus.permanentlyDenied:
2024-04-26 15:38:59 +08:00
// // AppLog.log("永久的否认");
// break;
// case PermissionStatus.provisional:
2024-04-26 15:38:59 +08:00
// // AppLog.log("临时");
// break;
// default:
2024-04-26 15:38:59 +08:00
// // AppLog.log("其他状态");
// // requestLocation();
// break;
// }
// }
2023-09-28 18:05:23 +08:00
Future<void> requestPermission() async {
2024-07-31 20:02:38 +08:00
final bool status = await PermissionDialog.request(Permission.location);
if(Platform.isIOS){
_setLocationOption();
requestIOSLocation();
}
if (!Platform.isIOS || status) {
requestAndroidLocation();
location.startLocation();
}
2023-09-28 18:05:23 +08:00
}
Future<void> requestAndroidLocation() async {
2024-07-31 20:02:38 +08:00
location.onLocationChanged().listen((Map<String, Object> event) {
if (event.isNotEmpty) {
setState(() {
addressInfo = event;
});
location.stopLocation();
}
});
2023-09-28 18:05:23 +08:00
}
Future<void> requestIOSLocation() async {
location = AMapFlutterLocation()
..setLocationOption(AMapLocationOption())
2024-07-31 20:02:38 +08:00
..onLocationChanged().listen((Map<String, Object> event) {
2024-04-26 15:38:59 +08:00
// AppLog.log("listenLocationChanged$event");
// EasyLoading.dismiss();
if (event.isNotEmpty) {
setState(() {
addressInfo = event;
});
// location.stopLocation();
}
})
..startLocation();
}
2023-09-28 18:05:23 +08:00
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
2023-09-28 18:05:23 +08:00
appBar: TitleAppBar(
2024-07-31 20:02:38 +08:00
barTitle: '锁地址'.tr,
2023-09-28 18:05:23 +08:00
haveBack: true,
backgroundColor: AppColors.mainColor,
),
body: ListView(
2024-07-31 20:02:38 +08:00
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 25.w, top: 40.h, bottom: 40.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
2024-07-31 20:02:38 +08:00
children: <Widget>[
Text('地理位置'.tr, style: TextStyle(fontSize: 50.sp)),
],
),
),
SizedBox(
2024-07-31 20:02:38 +08:00
child: (addressInfo != null && addressInfo!.containsKey('latitude'))
? Column(
2024-07-31 20:02:38 +08:00
children: <Widget>[
SizedBox(
height: 1.sw / 5 * 4,
width: 1.sw,
child: AMapWidget(
apiKey: amapApiKeys,
// 初始化地图中心
2024-07-31 20:02:38 +08:00
initialCameraPosition: CameraPosition(
target: LatLng(
double.parse(
addressInfo!['latitude'].toString()),
double.parse(
addressInfo!['longitude'].toString())),
zoom: 10.0,
2024-07-31 20:02:38 +08:00
),
//定位小蓝点
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,
margin:
EdgeInsets.only(left: 25.w, top: 20.h, right: 25.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
2024-07-31 20:02:38 +08:00
children: <Widget>[
Flexible(
2024-07-31 20:02:38 +08:00
child: Text('检查以确保以下地址是正确的'.tr,
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(
2024-07-31 20:02:38 +08:00
children: <Widget>[
Row(
2024-07-31 20:02:38 +08:00
children: <Widget>[
Expanded(
child: Text(
2024-07-31 20:02:38 +08:00
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: Center(child: Text('地图加载中,请稍候。。。'.tr))),
),
SizedBox(height: 200.h),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
2024-07-31 20:02:38 +08:00
children: <Widget>[
TextButton(
child: Text(
2024-07-31 20:02:38 +08:00
'跳过'.tr,
style: TextStyle(color: Colors.black, fontSize: 24.sp),
2023-09-28 18:05:23 +08:00
),
onPressed: () {
2024-07-31 20:02:38 +08:00
Get.toNamed(Routers.addLockSelectCountryPage, arguments: <String, Object>{
'addressInfo': {},
'pwdTimestamp': state.pwdTimestamp.value,
'lockInfo': state.lockInfo,
'featureValue': state.featureValue,
'featureSettingValue': state.featureSettingValue,
'featureSettingParams': state.featureSettingParams,
});
},
),
TextButton(
child: Text(
2024-08-01 18:54:32 +08:00
'下一步'.tr,
style: TextStyle(color: Colors.black, fontSize: 24.sp),
2023-09-28 18:05:23 +08:00
),
onPressed: () {
if (addressInfo!.isEmpty) {
2024-07-31 20:02:38 +08:00
logic.showToast('还未获取到位置信息哦,请耐心等待一下!'.tr);
return;
}
2024-07-31 20:02:38 +08:00
Get.toNamed(Routers.saveLockPage, arguments: <String, Object?>{
'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);
2023-09-28 18:05:23 +08:00
},
),
],
)
],
),
2023-09-28 18:05:23 +08:00
);
}
///设置定位参数
void _setLocationOption() {
AMapLocationOption locationOption = AMapLocationOption();
///是否单次定位
locationOption.onceLocation = true;
///是否需要返回逆地理信息
locationOption.needAddress = true;
///逆地理信息的语言类型
locationOption.geoLanguage = GeoLanguage.DEFAULT;
locationOption.desiredLocationAccuracyAuthorizationMode =
AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;
2024-07-31 20:02:38 +08:00
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() {
super.didChangeDependencies();
/// 路由订阅
AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!);
}
2023-09-28 18:05:23 +08:00
@override
void dispose() {
AppRouteObserver().routeObserver.unsubscribe(this);
2023-09-28 18:05:23 +08:00
super.dispose();
2023-12-27 10:47:58 +08:00
BlueManage().disconnect();
2023-12-27 10:47:58 +08:00
location.stopLocation();
location.destroy();
}
/// 从上级界面进入 当前界面即将出现
@override
void didPush() {
super.didPush();
}
/// 返回上一个界面 当前界面即将消失
@override
void didPop() {
super.didPop();
2023-09-28 18:05:23 +08:00
}
/// 从下级返回 当前界面即将出现
@override
void didPopNext() {
super.didPopNext();
}
/// 进入下级界面 当前界面即将消失
@override
void didPushNext() {
super.didPushNext();
location.stopLocation();
location.destroy();
}
2023-09-28 18:05:23 +08:00
}