You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
9.5 KiB

import 'dart:convert';
3 years ago
import 'dart:io';
3 years ago
import 'dart:ui';
3 years ago
import 'package:android_intent_plus/android_intent.dart';
4 years ago
import 'package:dio/dio.dart';
3 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
3 years ago
import 'package:flutter_easyloading/flutter_easyloading.dart';
7 months ago
import 'package:geolocator/geolocator.dart';
3 years ago
import 'package:huixiang/generated/l10n.dart';
4 years ago
import 'package:huixiang/retrofit/data/address.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
7 months ago
import 'package:huixiang/utils/location.dart';
3 years ago
import 'package:huixiang/view_widget/request_permission.dart';
7 months ago
import 'package:permission_handler/permission_handler.dart' as PH;
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
class AddressMapPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _AddressMapPage();
}
}
class _AddressMapPage extends State<AddressMapPage> {
String city = "武汉市";
String keyWord = "";
4 years ago
ApiService apiService;
@override
void initState() {
super.initState();
4 years ago
SharedPreferences.getInstance().then((value) => {
apiService = ApiService(Dio(),
context: context, token: value.getString('token')),
7 months ago
},);
4 years ago
7 months ago
LocationInstance.getInstance().startLocation(context, (Position result) {
print("event: $result");
if (result != null &&
result.latitude != null &&
result.longitude != null) {
7 months ago
latLng = Position(latitude: result.latitude, longitude: result.longitude);
LocationInstance.getInstance().getAddress(result.latitude, result.longitude).then((value) {
if (value != null) {
city = value.city;
}
});
7 months ago
saveLatLng(result);
searchPoi(latLng);
}
});
startLocation();
}
7 months ago
Position center = Position();
3 years ago
List<Address> poiList;
4 years ago
7 months ago
searchPoi(Position latLng) async {
4 years ago
keyWord = textEditingController.text;
3 years ago
print("keyWord: ${keyWord}");
7 months ago
var addressPoi = await apiService.searchPoi("${latLng.latitude}", "${latLng.longitude}", keyWord, 20, 1);
//如果定位列表无法显示,可以考虑数据是否不在当前字段里面;例:原数据放在['pois']里面,后改成了数据放在['entrances']里面
3 years ago
List<dynamic> poi = addressPoi['pois']??addressPoi['entrances'];
3 years ago
poiList = poi
.map((e) => Address.fromJson({
3 years ago
"address": e["address"] is List ? "" : e["address"],
"area": e["adname"],
"city": e["cityname"],
"province": e["pname"],
"latitude": e["location"].toString().split(",")[0],
"longitude": e["location"].toString().split(",")[1],
"cityInfo": e["name"],
"id": "",
"mid": "",
"phone": "",
"tag": "",
"username": "",
"isDefault": true,
}))
3 years ago
.toList();
setState(() {});
}
3 years ago
int time = 0;
startLocation() async {
7 months ago
if (!(await PH.Permission.locationWhenInUse.serviceStatus.isEnabled)) {
3 years ago
enableLocation();
return;
}
7 months ago
if (await PH.Permission.location.isPermanentlyDenied) {
3 years ago
requestDialog();
7 months ago
} else if (await PH.Permission.location.isGranted) {
3 years ago
// EasyLoading.show(status: S.of(context).zhengzaijiazai);
7 months ago
center = await Geolocator.getCurrentPosition();
3 years ago
Future.delayed(Duration(seconds: 6), () {
3 years ago
EasyLoading.dismiss();
3 years ago
});
} else if (await PH.Permission.location.isDenied) {
7 months ago
await PH.Permission.location.request();
3 years ago
} else {
if (Platform.isIOS) {
//去设置中心
requestDialog();
} else {
7 months ago
await PH.Permission.location.request();
}
}
}
3 years ago
enableLocation() {
showCupertinoDialog(
context: context,
builder: (context) {
return RequestPermission(
"assets/image/icon_permission_location_bg.webp",
3 years ago
S.of(context).nindingweigongnengweikaiqi,
S.of(context).weilexiangnintuijianfujindemendianxinxi,
S.of(context).dakaidingwei,
3 years ago
(result) async {
3 years ago
if (result) {
7 months ago
// final AndroidIntent intent = AndroidIntent(
// action: 'action_location_source_settings',
// package: "com.zsw.huixiang");
// await intent.launch();
3 years ago
// startLocation();
7 months ago
Geolocator.openLocationSettings();
3 years ago
}
},
heightRatioWithWidth: 0.82,
);
},
);
}
3 years ago
requestDialog() {
showCupertinoDialog(
context: context,
builder: (context) {
return RequestPermission(
"assets/image/icon_permission_location_bg.webp",
3 years ago
S.of(context).nindingweiquanxianweiyunxu,
S.of(context).weilexiangnintuijianfujindemendianxinxi,
S.of(context).kaiqiquanxian,
3 years ago
(result) async {
3 years ago
if (result) {
7 months ago
await PH.openAppSettings();
if (await PH.Permission.location.isGranted) {
3 years ago
startLocation();
}
}
},
heightRatioWithWidth: 0.82,
);
});
}
3 years ago
7 months ago
Position latLng;
3 years ago
7 months ago
saveLatLng(Position latLng) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("latitude", "${latLng.latitude}");
await prefs.setString("longitude", "${latLng.longitude}");
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
searchWidget(),
7 months ago
// Expanded(
// child: InkWell(
// onTap: () {
// startLocation();
// },
// child: Container(
// width: 32,
// height: 32,
// padding: EdgeInsets.all(8),
// decoration: BoxDecoration(
// borderRadius: BorderRadius.circular(16),
// color: Colors.white,
// ),
// child: Image.asset(
// "assets/image/icon_address_location_action.webp",
// width: 16.w,
// height: 16.h,
// ),
// ),
// ),
// flex: 1,
// ),
Expanded(
4 years ago
child: Container(
child: ListView.separated(
padding: EdgeInsets.symmetric(vertical: 0),
itemCount: poiList != null ? poiList.length : 0,
itemBuilder: (context, position) {
return InkWell(
onTap: () {
3 years ago
Navigator.of(context).pop(poiList[position].toJson());
4 years ago
},
child: addressItem(poiList[position]),
);
},
separatorBuilder: (contetx, position) {
return Container(
height: 1,
4 years ago
margin: EdgeInsets.symmetric(horizontal: 16.w),
4 years ago
color: Color(0xFFF1F1F1),
);
},
),
),
flex: 1,
)
],
),
);
}
3 years ago
Widget addressItem(Address address) {
4 years ago
return Container(
4 years ago
margin: EdgeInsets.symmetric(vertical: 16.h, horizontal: 16.w),
4 years ago
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
3 years ago
address.cityInfo ?? "",
4 years ago
overflow: TextOverflow.ellipsis,
style: TextStyle(
4 years ago
fontSize: 16.sp,
4 years ago
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
SizedBox(
4 years ago
height: 4.h,
4 years ago
),
Text(
"${address.province}${address.city}${address.area}${address.address}",
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Color(0xFF4C4C4C),
4 years ago
fontSize: 14.sp,
4 years ago
),
3 years ago
),
4 years ago
],
),
);
}
4 years ago
TextEditingController textEditingController = TextEditingController();
Widget searchWidget() {
return Container(
4 years ago
height: 36.h,
margin: EdgeInsets.fromLTRB(16, 48, 16, 8),
padding: EdgeInsets.fromLTRB(10, 6, 16, 6),
decoration: BoxDecoration(
3 years ago
color: Colors.white,
7 months ago
borderRadius: BorderRadius.all(
Radius.circular(4),
),
3 years ago
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0,
7 months ago
),
3 years ago
],
),
child: Row(
4 years ago
mainAxisSize: MainAxisSize.max,
children: [
Icon(
Icons.search,
size: 24,
color: Colors.black,
),
Expanded(
child: TextField(
4 years ago
controller: textEditingController,
textInputAction: TextInputAction.search,
onEditingComplete: () {
FocusScope.of(context).requestFocus(FocusNode());
searchPoi(center);
},
decoration: InputDecoration(border: InputBorder.none),
),
),
4 years ago
InkWell(
onTap: () {
textEditingController.clear();
},
child: Icon(
Icons.close,
size: 19,
color: Colors.grey,
),
),
],
),
);
}
}