app-starlock/star_lock/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_page.dart
2023-09-28 18:05:23 +08:00

213 lines
7.5 KiB
Dart

import 'dart:async';
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 '../../../../tools/titleAppBar.dart';
import '../../../../tools/toast.dart';
import '../../../../translations/trans_lib.dart';
// typedef BlockAddressMapCallback = void Function(dynamic addressMap);
class LockAddressGaoDePage extends StatefulWidget {
// BlockAddressMapCallback callback;
const LockAddressGaoDePage({Key? key}) : super(key: key);
@override
State<LockAddressGaoDePage> createState() => _LockAddressGaoDePageState();
}
class _LockAddressGaoDePageState extends State<LockAddressGaoDePage>{
// 高德地图
static const AMapApiKey amapApiKeys = AMapApiKey(iosKey: '883a3355d2d77c2fdc2667030dc97ffe', androidKey: '11d49b3f4fc09c04a02bbb7500925ba2');
AMapController? mapController;
AMapFlutterLocation? location;
PermissionStatus? permissionStatus;
Map<String, Object>? addressInfo;
@override
void initState() {
super.initState();
AMapFlutterLocation.updatePrivacyAgree(true);
AMapFlutterLocation.updatePrivacyShow(true, true);
AMapFlutterLocation.setApiKey("11d49b3f4fc09c04a02bbb7500925ba2", "883a3355d2d77c2fdc2667030dc97ffe");
requestPermission();
}
Future<void> requestPermission() async {
final status = await Permission.location.request();
print("Permission.location.request()=status:$status");
permissionStatus = status;
switch (status) {
case PermissionStatus.denied:
print("拒绝");
break;
case PermissionStatus.granted:
requestLocation();
break;
case PermissionStatus.limited:
print("限制");
break;
default:
print("其他状态");
requestLocation();
break;
}
}
Future<void> requestLocation() async {
location = AMapFlutterLocation()
..setLocationOption(AMapLocationOption())
..onLocationChanged().listen((event) {
print("listenLocationChanged$event");
// double? latitude = double.parse(event['latitude']);
// double? longitude = double.parse(event['longitude'] as String);
if (event.isNotEmpty) {
// widget.callback(event);
setState(() {
addressInfo = event;
// currentLocation = CameraPosition(
// target: LatLng(latitude, longitude),
// zoom: 10,
// );
});
}
})
..startLocation();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.mainBackgroundColor,
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);
// Navigator.pushNamed(context, Routers.saveLockPage);
},
),
],
),
body:
addressInfo != null ?
Container(
width: 1.sw,
// height: 1.sh,
child: Stack(
children: [
SizedBox(
// height: 1.sh - 40.h,
// width: 1.sw,
child:
(addressInfo!["address"].toString().isNotEmpty) ?
AMapWidget(
apiKey: amapApiKeys,
// 初始化地图中心
initialCameraPosition: (
CameraPosition(
target: LatLng(double.parse(addressInfo!['latitude'] as String), double.parse(addressInfo!['longitude'] as String)),
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(),
),
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),)
// ),
// ],
// ),
// ),
],
)
),),
],
),
)
: const Center(child: Text('地图加载中,请稍候。。。')),
);
}
@override
void dispose() {
super.dispose();
location?.destroy();
print("地图界面销毁了");
}
}