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.
diplomatic-quarter/lib/pages/ErService/rapid-response-team/rrt-pickup-address-page.dart

346 lines
12 KiB
Dart

import 'dart:async';
3 years ago
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/er/rrt-view-model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/rrt/service_price.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/HomeHealthCare/NewHomeHealthCare/location_page.dart';
3 years ago
import 'package:diplomaticquarterapp/pages/BookAppointment/widgets/reminder_dialog.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
3 years ago
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
3 years ago
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/app_map/google_huawei_map.dart';
3 years ago
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
3 years ago
import 'package:diplomaticquarterapp/widgets/dialogs/select_location_dialog.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
3 years ago
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
3 years ago
import 'rrt-place-order.dart';
3 years ago
class RRTRequestPickupAddressPage extends StatefulWidget {
final ServicePrice servicePrice;
3 years ago
RRTRequestPickupAddressPage({@required this.servicePrice});
@override
3 years ago
State<StatefulWidget> createState() => RRTRequestPickupAddressPageState();
}
3 years ago
class RRTRequestPickupAddressPageState extends State<RRTRequestPickupAddressPage> with SingleTickerProviderStateMixin {
bool acceptTerms = false;
bool mapIdle = true;
Completer<GoogleMapController> mapController = Completer();
static CameraPosition cameraPosition = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
3 years ago
zoom: 17,
);
LatLng currentPostion;
List<AddressInfo> myAddresses = [];
3 years ago
AddressInfo selectedAddress = new AddressInfo();
StreamController<int> addressStreamController = StreamController();
Stream<int> addressStream;
StreamController<int> addressLoadingStreamController = StreamController();
Stream<int> addressLoadingStream;
3 years ago
3 years ago
// AddressInfo _selectedAddress;
double latitude = 0;
double longitude = 0;
AppMap appMap;
bool showCurrentLocation = false;
3 years ago
AppSharedPreferences sharedPref = AppSharedPreferences();
@override
3 years ago
void initState() {
3 years ago
super.initState();
3 years ago
appMap = AppMap(
cameraPosition.toMap(),
onCameraMove: (camera) {
_updatePosition(camera);
},
onMapCreated: () {
3 years ago
_getUserLocation();
3 years ago
setState(() {});
},
);
// addressStream = addressStreamController.stream;
// addressLoadingStream = addressLoadingStreamController.stream;
}
setLatitudeAndLongitude({bool isSetState = false, String latLong}) async {
if (latLong == null) {
if (myAddresses.isEmpty) {
setState(() {
showCurrentLocation = true;
});
} else {
latLong = myAddresses[myAddresses.length - 1].latLong;
}
}
if (!showCurrentLocation) {
List latLongArr = latLong.split(',');
latitude = double.parse(latLongArr[0]);
longitude = double.parse(latLongArr[1]);
currentPostion = LatLng(latitude, longitude);
setMap();
}
}
setMap() {
setState(() {
cameraPosition = CameraPosition(
target: currentPostion,
3 years ago
zoom: 16,
);
appMap.moveTo(cameraPostion: cameraPosition);
3 years ago
selectedAddress.latLong = currentPostion.latitude.toString() + "," + currentPostion.longitude.toString();
});
}
3 years ago
void loadAddresses() async {
await viewModel.getCustomerInfo();
if (viewModel.addressesList.isNotEmpty) setState(() {});
}
TranslationBase localize;
RRTViewModel viewModel;
3 years ago
@override
Widget build(BuildContext context) {
localize = TranslationBase.of(context);
ProjectViewModel projectViewModel = Provider.of(context);
addressStreamController.sink.add(0);
return BaseView<RRTViewModel>(
3 years ago
onModelReady: (vm) {
viewModel = vm;
loadAddresses();
myAddresses = viewModel.addressesList;
},
builder: (ctx, vm, widget) => AppScaffold(
appBarTitle: localize.pickupLocation,
isShowAppBar: true,
3 years ago
showNewAppBar: true,
showNewAppBarTitle: true,
baseViewModel: viewModel,
body: Column(
children: [
3 years ago
Container(
margin: EdgeInsets.only(left: 12, right: 12, bottom: 12, top: 12),
3 years ago
decoration: cardRadius(12),
child: Container(
child: InkWell(
onTap: () => confirmSelectLocationDialog(myAddresses),
child: Container(
padding: EdgeInsets.all(8),
width: double.infinity,
// height: 65,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
getAddressName(),
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 12,
letterSpacing: -0.45,
),
),
),
Icon(Icons.arrow_drop_down)
],
),
),
),
height: 50,
width: double.infinity,
),
),
InkWell(
onTap: () async {
Navigator.push(
context,
FadePage(
page: LocationPage(
latitude: latitude,
longitude: longitude,
),
),
);
},
child: Padding(
padding: EdgeInsets.only(left: 12, right: 12, bottom: 16, top: 8),
child: Row(
children: [
Icon(Icons.add_circle_outline_sharp),
mWidth(12),
Text(
TranslationBase.of(context).addNewAddress,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
letterSpacing: -0.46,
),
),
],
),
),
),
Expanded(
child: Stack(
alignment: Alignment.center,
children: [
3 years ago
if (appMap != null) appMap,
Icon(
Icons.place,
color: CustomColors.accentColor,
size: 50,
),
],
),
),
Padding(
padding: const EdgeInsets.only(left: 20, right: 20, top: 14, bottom: 14),
child: continueButton(),
),
],
3 years ago
)));
}
void _updatePosition(CameraPosition _position) {
latitude = _position.target.latitude;
longitude = _position.target.longitude;
3 years ago
selectedAddress.latLong = latitude.toString() + "," + longitude.toString();
}
3 years ago
Widget centerTargetPoint() {
double size = mapIdle ? 20 : 30;
double margin = mapIdle ? 3 : 10;
return Center(
child: Container(
3 years ago
width: size,
height: size,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(20), border: Border.all(width: 2, color: Theme.of(context).appBarTheme.color)),
child: Container(margin: EdgeInsets.all(margin), decoration: BoxDecoration(color: Theme.of(context).appBarTheme.color, borderRadius: BorderRadius.circular(20))),
),
);
}
3 years ago
Widget continueButton() {
return Padding(
3 years ago
padding: const EdgeInsets.all(0.0),
3 years ago
child: DefaultButton(TranslationBase.of(context).continues, () {
Navigator.push(
context,
FadePage(
page: RRTPlaceOrderPage(
selectedAddress: selectedAddress,
servicePrice: widget.servicePrice,
)));
}),
);
}
void confirmSelectLocationDialog(List<AddressInfo> addresses) {
showDialog(
context: context,
child: SelectLocationDialog(
addresses: addresses,
3 years ago
selectedAddress: selectedAddress,
onValueSelected: (value) {
showCurrentLocation = false;
setLatitudeAndLongitude(latLong: value.latLong);
setState(() {
3 years ago
selectedAddress = value;
});
},
),
);
}
String getAddressName() {
3 years ago
if (selectedAddress != null && selectedAddress.address1 != null)
return selectedAddress.address1;
else
return TranslationBase.of(context).selectAddress;
}
3 years ago
moveToLocation(LatLng location, {bool animate = true}) async {
await Future.delayed(Duration(milliseconds: 200));
cameraPosition = CameraPosition(
3 years ago
target: location,
zoom: 16.4746,
);
if (animate)
(await mapController.future).animateCamera(
CameraUpdate.newCameraPosition(cameraPosition),
3 years ago
);
else
3 years ago
(await mapController.future).moveCamera(
CameraUpdate.newCameraPosition(cameraPosition),
3 years ago
);
}
3 years ago
void _getUserLocation() async {
if (await this.sharedPref.getDouble(USER_LAT) != null && await this.sharedPref.getDouble(USER_LONG) != null) {
var lat = await this.sharedPref.getDouble(USER_LAT);
var long = await this.sharedPref.getDouble(USER_LONG);
latitude = lat;
longitude = long;
currentPostion = LatLng(lat, long);
setMap();
} else {
if (await Permission.location.request().isGranted) {
var position = await GeolocatorPlatform.instance.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
currentPostion = LatLng(position.latitude, position.longitude);
latitude = position.latitude;
longitude = position.longitude;
setMap();
} else {
requestPermissions().then(
(value) async {
if (value[Permission.location].isGranted) {
var position = await GeolocatorPlatform.instance.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
currentPostion = LatLng(position.latitude, position.longitude);
latitude = position.latitude;
longitude = position.longitude;
setMap();
}
},
);
}
}
}
3 years ago
goToCurrentLocation() async {
3 years ago
Geolocator.getCurrentPosition().then((value) {
selectedAddress = AddressInfo(latLong: '${value.latitude},${value.longitude}');
moveToLocation(LatLng(value.latitude, value.longitude));
});
}
3 years ago
}