You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HMG_Patient_App/lib/widgets/pickupLocation/PickupLocationFromMap.dart

187 lines
7.2 KiB
Dart

import 'dart:io';
7 months ago
import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/ambulanceRequest/locationDetails.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/app_map/google_huawei_map.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
5 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
4 years ago
import 'package:geocoding/geocoding.dart';
2 years ago
import 'package:google_maps_flutter/google_maps_flutter.dart' as googleMap;
2 years ago
import 'package:google_maps_flutter/google_maps_flutter.dart';
7 months ago
import 'package:google_maps_place_picker_mb_v2/google_maps_place_picker.dart';
import 'package:huawei_hmsavailability/huawei_hmsavailability.dart';
5 years ago
import 'package:provider/provider.dart';
4 years ago
class PickupLocationFromMap extends StatefulWidget {
2 years ago
final Function(LocationDetails)? onPick;
final double? latitude;
final double? longitude;
final bool? isWithAppBar;
final String? buttonLabel;
final Color? buttonColor;
5 years ago
2 years ago
const PickupLocationFromMap({Key? key, this.onPick, this.latitude, this.longitude, this.isWithAppBar = true, this.buttonLabel, this.buttonColor}) : super(key: key);
5 years ago
4 years ago
@override
State<PickupLocationFromMap> createState() => _PickupLocationFromMapState();
}
class _PickupLocationFromMapState extends State<PickupLocationFromMap> {
bool isHuawei = false;
2 years ago
late Placemark selectedPlace;
late AppMap appMap;
late LatLng currentPostion;
4 years ago
AppSharedPreferences sharedPref = AppSharedPreferences();
double latitude = 0;
double longitude = 0;
2 years ago
late HmsApiAvailability hmsApiAvailability;
4 years ago
static CameraPosition kGooglePlex = CameraPosition(
target: LatLng(24.126613,45.081137),
4 years ago
zoom: 14.4746,
);
@override
void initState() {
3 years ago
if(Platform.isAndroid) {
hmsApiAvailability = HmsApiAvailability();
checkIsHuawei();
}
4 years ago
appMap = AppMap(
2 years ago
//changed by Aamir
kGooglePlex.toMap() as Map<dynamic, dynamic>,
2 years ago
onCameraMove: () {
_updatePosition(kGooglePlex);
4 years ago
},
onMapCreated: () {
2 years ago
currentPostion = LatLng(widget.latitude!, widget.longitude!);
latitude = widget.latitude!;
longitude = widget.longitude!;
4 years ago
setState(() {});
},
onCameraIdle: () async {
List<Placemark> placemarks = await placemarkFromCoordinates(latitude, longitude);
selectedPlace = placemarks[0];
print(selectedPlace);
},
);
super.initState();
}
checkIsHuawei() async {
// isHuawei = await FlutterHmsGmsAvailability.isHmsAvailable;
// print(isHuawei);
// setState(() {});
await hmsApiAvailability.isHMSAvailable().then((value) {
isHuawei = value == 0 ? true : false;
});
4 years ago
print(isHuawei);
setState(() {});
}
5 years ago
@override
Widget build(BuildContext context) {
ProjectViewModel projectViewModel = Provider.of(context);
4 years ago
return AppScaffold(
isShowAppBar: true,
showNewAppBarTitle: true,
showNewAppBar: true,
appBarTitle: TranslationBase.of(context).selectLocation,
4 years ago
body: isHuawei
? Column(
children: [
Expanded(
child: Stack(
alignment: Alignment.center,
children: [
if (appMap != null) appMap,
Container(
margin: EdgeInsets.only(bottom: 50.0),
child: Icon(
Icons.place,
color: CustomColors.accentColor,
size: 50,
),
4 years ago
),
],
),
),
Container(
padding: const EdgeInsets.only(left: 20, right: 20, top: 14, bottom: 14),
child: DefaultButton(TranslationBase.of(context).next, () async {
LocationDetails locationDetails = new LocationDetails();
locationDetails.lat = latitude;
locationDetails.long = longitude;
2 years ago
locationDetails.formattedAddress = selectedPlace.street!;
widget.onPick!(locationDetails);
4 years ago
Navigator.of(context).pop();
}),
),
],
)
: PlacePicker(
apiKey: GOOGLE_API_KEY,
enableMyLocationButton: true,
automaticallyImplyAppBarLeading: false,
autocompleteLanguage: projectViewModel.currentLanguage,
enableMapTypeButton: true,
selectInitialPosition: true,
region: "SA",
onPlacePicked: (PickResult result) {
LocationDetails locationDetails = new LocationDetails();
locationDetails.lat = latitude;
locationDetails.long = longitude;
2 years ago
locationDetails.formattedAddress = result.formattedAddress!;
4 years ago
print(result.adrAddress);
2 years ago
widget.onPick!(locationDetails);
4 years ago
Navigator.of(context).pop();
},
selectedPlaceWidgetBuilder: (_, selectedPlace, state, isSearchBarFocused) {
print("state: $state, isSearchBarFocused: $isSearchBarFocused");
return isSearchBarFocused
? Container()
: FloatingCard(
bottomPosition: 0.0,
leftPosition: 0.0,
rightPosition: 0.0,
width: 500,
borderRadius: BorderRadius.circular(12.0),
child: state == SearchingState.Searching
? Center(child: CircularProgressIndicator())
: Container(
margin: EdgeInsets.all(12),
child: DefaultButton(
TranslationBase.of(context).next,
() {
LocationDetails locationDetails = new LocationDetails();
2 years ago
locationDetails.lat = selectedPlace!.geometry!.location.lat;
locationDetails.long = selectedPlace.geometry!.location.lng;
locationDetails.formattedAddress = selectedPlace.formattedAddress!;
widget.onPick!(locationDetails);
4 years ago
Navigator.of(context).pop();
},
),
),
);
},
2 years ago
initialPosition: googleMap.LatLng(projectViewModel.latitude, projectViewModel.longitude),
4 years ago
useCurrentLocation: true,
),
5 years ago
);
}
4 years ago
void _updatePosition(CameraPosition _position) {
print(_position);
2 years ago
latitude = _position.target.latitude;
longitude = _position.target.longitude;
4 years ago
}
5 years ago
}