Chad

修改页面逻辑

......@@ -75,6 +75,9 @@ android {
release {
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.release
}
}
}
......
......@@ -41,6 +41,7 @@ class Results {
PlusCode? plusCode;
dynamic rating;
int? userRatingsTotal;
bool isSelect = false;
Results(
{this.geometry,
......@@ -58,11 +59,11 @@ class Results {
this.openingHours,
this.plusCode,
this.rating,
this.userRatingsTotal});
this.userRatingsTotal,
this.isSelect = false});
Results.fromJson(Map<String, dynamic> json) {
geometry =
json['geometry'] != null ? Geometry.fromJson(json['geometry']) : null;
geometry = json['geometry'] != null ? Geometry.fromJson(json['geometry']) : null;
icon = json['icon'];
iconBackgroundColor = json['icon_background_color'];
iconMaskBaseUri = json['icon_mask_base_uri'];
......@@ -79,11 +80,8 @@ class Results {
types = json['types'].cast<String>();
vicinity = json['vicinity'];
businessStatus = json['business_status'];
openingHours = json['opening_hours'] != null
? OpeningHours.fromJson(json['opening_hours'])
: null;
plusCode =
json['plus_code'] != null ? PlusCode.fromJson(json['plus_code']) : null;
openingHours = json['opening_hours'] != null ? OpeningHours.fromJson(json['opening_hours']) : null;
plusCode = json['plus_code'] != null ? PlusCode.fromJson(json['plus_code']) : null;
rating = json['rating'];
userRatingsTotal = json['user_ratings_total'];
}
......@@ -125,10 +123,8 @@ class Geometry {
Geometry({this.location, this.viewport});
Geometry.fromJson(Map<String, dynamic> json) {
location =
json['location'] != null ? Location.fromJson(json['location']) : null;
viewport =
json['viewport'] != null ? Viewport.fromJson(json['viewport']) : null;
location = json['location'] != null ? Location.fromJson(json['location']) : null;
viewport = json['viewport'] != null ? Viewport.fromJson(json['viewport']) : null;
}
Map<String, dynamic> toJson() {
......@@ -169,10 +165,8 @@ class Viewport {
Viewport({this.northeast, this.southwest});
Viewport.fromJson(Map<String, dynamic> json) {
northeast =
json['northeast'] != null ? Location.fromJson(json['northeast']) : null;
southwest =
json['southwest'] != null ? Location.fromJson(json['southwest']) : null;
northeast = json['northeast'] != null ? Location.fromJson(json['northeast']) : null;
southwest = json['southwest'] != null ? Location.fromJson(json['southwest']) : null;
}
Map<String, dynamic> toJson() {
......
......@@ -22,12 +22,10 @@ class AddressSelectPage extends StatefulWidget {
class AddressSelectPageState extends State<AddressSelectPage> {
List<nearby.Results> _list = [];
int _index = 0;
final ScrollController _controller = ScrollController();
LatLng _center = const LatLng(45.521563, -122.677433);
LatLng? _center;
late GoogleMapController mapController;
bool isLoading = false;
int _markerIdCounter = 1;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
late StreamSubscription _locationSubscription;
String radius = "1000";
......@@ -65,44 +63,38 @@ class AddressSelectPageState extends State<AddressSelectPage> {
return;
}
}
_locationSubscription = location.onLocationChanged.listen((LocationData currentLocation) {
_center = LatLng(currentLocation.latitude!, currentLocation.longitude!);
Log.e("currentLocation.latitude ${currentLocation.latitude}");
getNearbyPlaces("");
});
var currentLocation = await location.getLocation();
_center = LatLng(currentLocation.latitude!, currentLocation.longitude!);
getNearbyPlaces("");
_goToCurrentCenter();
}
void getNearbyPlaces(String keyword) async {
String uri =
'${'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${_center.latitude},${_center.longitude}&radius=$radius'}&key=$apiKey&keyword=$keyword';
'${'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${_center?.latitude},${_center?.longitude}&radius=$radius'}&key=$apiKey&keyword=$keyword';
print(uri);
var url = Uri.parse(uri);
var response = await http.post(url);
nearbyPlacesResponse = nearby.NearbyPlacesResponse.fromJson(jsonDecode(response.body));
_list = nearbyPlacesResponse.results!;
if (_list.isNotEmpty) {
selectItemLocation(_list[0]);
}
setState(() {
isLoading = false;
});
buildMarkers();
}
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
final String markerIdVal = 'marker_id_$_markerIdCounter';
_markerIdCounter++;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position: _center,
);
mapController.moveCamera(CameraUpdate.newLatLng(_center));
setState(() {
markers[markerId] = marker;
});
getNearbyPlaces("");
}
void _goToCurrentCenter() {
mapController.animateCamera(CameraUpdate.newLatLng(_center));
if (_center != null) {
mapController.moveCamera(CameraUpdate.newLatLng(_center!));
}
}
@override
......@@ -113,12 +105,12 @@ class AddressSelectPageState extends State<AddressSelectPage> {
itemCount: _list.length,
separatorBuilder: (_, index) => const Divider(),
itemBuilder: (_, index) {
var item = _list[index];
return _AddressItem(
isSelected: _index == index,
date: _list[index],
isSelected: item.isSelect,
date: item,
onTap: () {
_index = index;
setState(() {});
selectItemLocation(item);
},
);
},
......@@ -130,25 +122,21 @@ class AddressSelectPageState extends State<AddressSelectPage> {
onPressed: (text) async {
isLoading = true;
_controller.animateTo(0.0, duration: const Duration(milliseconds: 10), curve: Curves.ease);
_index = 0;
// 构造检索参数
getNearbyPlaces(text);
},
);
var map = GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(target: _center, zoom: 16.0),
initialCameraPosition: CameraPosition(target: _center ?? const LatLng(45.521563, -122.677433), zoom: 16.0),
markers: Set<Marker>.of(markers.values),
).expanded(flex: 9);
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: searchBar,
body: Column(
children: <Widget>[
map,
listHolder,
initButton(),
],
children: <Widget>[map, listHolder, initButton()],
).safe(),
);
}
......@@ -156,15 +144,58 @@ class AddressSelectPageState extends State<AddressSelectPage> {
initButton() {
return MyButton(
onPressed: () {
if (_list.isEmpty) {
var selected = _list.where((element) => element.isSelect);
if (selected.isEmpty) {
Toast.show('未选择地址!');
return;
}
NavigatorUtils.goBackWithParams(context, _list[_index]);
NavigatorUtils.goBackWithParams(context, selected.first);
},
text: '确认选择地址',
);
}
void buildMarkers() {
markers.clear();
for (var value in _list) {
final MarkerId markerId = MarkerId(buildMarkerId(value));
final Marker marker = Marker(
icon: value.isSelect ? BitmapDescriptor.defaultMarker : BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
markerId: markerId,
position: buildMarkerLocation(value),
);
markers[markerId] = marker;
}
setState(() {});
}
String buildMarkerId(nearby.Results value) {
var lat = value.geometry?.location?.lat;
var lng = value.geometry?.location?.lng;
return "$lat-$lng";
}
LatLng buildMarkerLocation(nearby.Results value) {
var lat = value.geometry?.location?.lat;
var lng = value.geometry?.location?.lng;
return LatLng(lat ?? 0, lng ?? 0);
}
void selectItemLocation(nearby.Results item) {
for (var element in _list) {
element.isSelect = false;
}
item.isSelect = true;
var lat = item.geometry?.location?.lat;
var lng = item.geometry?.location?.lng;
if (lat != null && lng != null) {
_center = LatLng(lat, lng);
}
_goToCurrentCenter();
setState(() {});
buildMarkers();
}
}
class _AddressItem extends StatelessWidget {
......
......@@ -125,7 +125,7 @@ dependencies:
google_fonts: ^3.0.1
wakelock: ^0.6.1+2
location: ^4.4.0
google_maps_flutter: ^2.1.10
google_maps_flutter: ^2.2.1
http: ^0.13.5
dependency_overrides:
......