Refactoring
parent
dd308e49de
commit
646ebbd3a5
@ -1,69 +0,0 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:flutter/cupertino.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
||||||
|
|
||||||
class AppMap extends StatefulWidget {
|
|
||||||
final CameraPositionCallback onCameraMove;
|
|
||||||
Map initialCamera;
|
|
||||||
|
|
||||||
late AppMapState _state;
|
|
||||||
final Function onMapCreated;
|
|
||||||
|
|
||||||
AppMap(this.initialCamera,
|
|
||||||
{required this.onCameraMove, required this.onMapCreated});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<StatefulWidget> createState() => _state = AppMapState();
|
|
||||||
|
|
||||||
moveTo({required CameraPosition cameraPostion}) {
|
|
||||||
_state.googleMapController
|
|
||||||
.animateCamera(CameraUpdate.newCameraPosition(cameraPostion));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AppMapState extends State<AppMap> {
|
|
||||||
bool isHuawei = false;
|
|
||||||
|
|
||||||
Completer<GoogleMapController> _googleMapControllerComp = Completer();
|
|
||||||
late GoogleMapController googleMapController;
|
|
||||||
|
|
||||||
checkIsHuawei() async {
|
|
||||||
setState(() {});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
super.dispose();
|
|
||||||
googleMapController.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
if (isHuawei == null) return CircularProgressIndicator();
|
|
||||||
return googleMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget googleMap() {
|
|
||||||
return GoogleMap(
|
|
||||||
mapType: MapType.normal,
|
|
||||||
zoomControlsEnabled: true,
|
|
||||||
myLocationButtonEnabled: true,
|
|
||||||
myLocationEnabled: true,
|
|
||||||
initialCameraPosition: CameraPosition.fromMap(widget.initialCamera) ??
|
|
||||||
const CameraPosition(
|
|
||||||
target: LatLng(-26.1711459, 27.9002758), zoom: 2.0),
|
|
||||||
onCameraMove: widget.onCameraMove,
|
|
||||||
onMapCreated: (GoogleMapController controller) {
|
|
||||||
googleMapController = controller;
|
|
||||||
_googleMapControllerComp.complete(controller);
|
|
||||||
widget.onMapCreated();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,180 +0,0 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:easy_localization/src/public_ext.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
import 'package:geolocator/geolocator.dart';
|
|
||||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
||||||
import 'package:mc_common_app/generated/locale_keys.g.dart';
|
|
||||||
import 'package:mc_common_app/utils/navigator.dart';
|
|
||||||
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
|
|
||||||
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
|
|
||||||
|
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
|
||||||
|
|
||||||
import 'map_selection_widget.dart';
|
|
||||||
import 'package:geocoding/geocoding.dart';
|
|
||||||
|
|
||||||
class PickLocationPage extends StatefulWidget {
|
|
||||||
Function(double, double, String) onPickAddress;
|
|
||||||
|
|
||||||
PickLocationPage({required this.onPickAddress});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<PickLocationPage> createState() => _PickLocationPageState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _PickLocationPageState extends State<PickLocationPage> {
|
|
||||||
double latitude = 0;
|
|
||||||
double longitude = 0;
|
|
||||||
|
|
||||||
final Set<Marker> markers = new Set();
|
|
||||||
late AppMap appMap;
|
|
||||||
|
|
||||||
static CameraPosition _kGooglePlex = CameraPosition(
|
|
||||||
target: LatLng(37.42796133580664, -122.085749655962),
|
|
||||||
zoom: 14.4746,
|
|
||||||
);
|
|
||||||
late LatLng currentPostion;
|
|
||||||
Completer<GoogleMapController> mapController = Completer();
|
|
||||||
final ValueNotifier<String> _counter = ValueNotifier<String>("");
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
appMap = AppMap(
|
|
||||||
_kGooglePlex.toMap() as Map,
|
|
||||||
onCameraMove: (camera) {
|
|
||||||
_updatePosition(camera);
|
|
||||||
},
|
|
||||||
onMapCreated: () {
|
|
||||||
// goToCurrentLocation();
|
|
||||||
_getUserLocation();
|
|
||||||
setState(() {});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
super.initState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Scaffold(
|
|
||||||
appBar: CustomAppBar(
|
|
||||||
title: LocaleKeys.pickLocation.tr(),
|
|
||||||
isRemoveBackButton: false,
|
|
||||||
),
|
|
||||||
body: Column(
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: Stack(
|
|
||||||
children: [
|
|
||||||
if (appMap != null) appMap,
|
|
||||||
ValueListenableBuilder<String>(
|
|
||||||
builder: (BuildContext context, String value, Widget? child) {
|
|
||||||
// This builder will only get called when the _counter
|
|
||||||
// is updated.
|
|
||||||
return value.isNotEmpty
|
|
||||||
? Container(
|
|
||||||
width: double.infinity,
|
|
||||||
margin: EdgeInsets.all(12),
|
|
||||||
child: Card(
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.all(12.0),
|
|
||||||
child: Text(value ?? ""),
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
: Container();
|
|
||||||
},
|
|
||||||
valueListenable: _counter,
|
|
||||||
),
|
|
||||||
const Align(
|
|
||||||
alignment: Alignment.center,
|
|
||||||
child: Icon(
|
|
||||||
Icons.place,
|
|
||||||
color: Colors.red,
|
|
||||||
size: 50,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Container(
|
|
||||||
width: double.infinity,
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.all(12.0),
|
|
||||||
child: ShowFillButton(
|
|
||||||
title: LocaleKeys.pickAddress.tr(),
|
|
||||||
onPressed: () {
|
|
||||||
widget.onPickAddress(latitude, longitude, _counter.value);
|
|
||||||
pop(context);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _updatePosition(CameraPosition _position) async {
|
|
||||||
latitude = _position.target.latitude;
|
|
||||||
longitude = _position.target.longitude;
|
|
||||||
print(latitude);
|
|
||||||
print(latitude);
|
|
||||||
updateAddress(latitude, longitude);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _getUserLocation() async {
|
|
||||||
if (await Permission.location.request().isGranted) {
|
|
||||||
var position = await GeolocatorPlatform.instance.getCurrentPosition();
|
|
||||||
currentPostion = LatLng(position.latitude, position.longitude);
|
|
||||||
latitude = position.latitude;
|
|
||||||
longitude = position.longitude;
|
|
||||||
setMap();
|
|
||||||
updateAddress(latitude, longitude);
|
|
||||||
} else {
|
|
||||||
requestPermissions().then(
|
|
||||||
(value) async {
|
|
||||||
if (value[Permission.location]!.isGranted) {
|
|
||||||
var position = await GeolocatorPlatform.instance.getCurrentPosition();
|
|
||||||
currentPostion = LatLng(position.latitude, position.longitude);
|
|
||||||
latitude = position.latitude;
|
|
||||||
longitude = position.longitude;
|
|
||||||
setMap();
|
|
||||||
updateAddress(latitude, longitude);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
updateAddress(double latitude, double longitude) async {
|
|
||||||
List<Placemark> placemarks = await placemarkFromCoordinates(latitude, longitude);
|
|
||||||
_counter.value = '${placemarks.first.street}, ${placemarks.first.subLocality}, ${placemarks.first.locality}, ${placemarks.first.postalCode}, ${placemarks.first.country}';
|
|
||||||
}
|
|
||||||
|
|
||||||
setMap() {
|
|
||||||
setState(() {
|
|
||||||
_kGooglePlex = CameraPosition(
|
|
||||||
target: currentPostion,
|
|
||||||
zoom: 14.4746,
|
|
||||||
);
|
|
||||||
appMap.moveTo(cameraPostion: _kGooglePlex);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<String> getCurrentAddress() async {
|
|
||||||
// List<Placemark> placemarks = await placemarkFromCoordinates(latitude, longitude);
|
|
||||||
// return placemarks.first.name ?? "";
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<Map<Permission, PermissionStatus>> requestPermissions() async {
|
|
||||||
var permissionResults = [Permission.location].request();
|
|
||||||
return permissionResults;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue