Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gideon #18

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions lib/screens/admin/adminPages/admin_details.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:brianpharmacy/screens/admin/adminPages/admin_location.dart';
import 'package:brianpharmacy/screens/admin/models/drug.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

import '../models/user.dart';

class AdminDetails extends StatefulWidget {
const AdminDetails({super.key});
const AdminDetails({Key? key}) : super(key: key);

@override
State<AdminDetails> createState() => _AdminDetailsState();
Expand All @@ -17,10 +18,12 @@ class _AdminDetailsState extends State<AdminDetails> {
List<String> text = [];
final controllerName = TextEditingController();
final controllerPhone = TextEditingController();
TextEditingController controllerLocation = TextEditingController();
TextEditingController controllerDrugs = TextEditingController();
final controllerLocation = TextEditingController();
final controllerProfessional = TextEditingController();
final _locationController = TextEditingController();
final controllerDrugs = TextEditingController();

List<Drug> drugs = [];

void _chooseLocation() async {
final pickedLocation = await Navigator.push(
Expand All @@ -30,10 +33,12 @@ class _AdminDetailsState extends State<AdminDetails> {
),
);
if (pickedLocation != null) {
setState(() {
_locationController.text =
' ${pickedLocation.latitude},${pickedLocation.longitude}';
});
setState(
() {
_locationController.text =
' ${pickedLocation.latitude},${pickedLocation.longitude}';
},
);
}
}

Expand Down Expand Up @@ -65,7 +70,7 @@ class _AdminDetailsState extends State<AdminDetails> {
),
TextField(
controller: controllerProfessional,
decoration: decoration('Proffession'),
decoration: decoration('Profession'),
),
const SizedBox(
height: 24,
Expand Down Expand Up @@ -102,7 +107,15 @@ class _AdminDetailsState extends State<AdminDetails> {
),
ElevatedButton(
onPressed: () {
List<String> drugs = controllerDrugs.text.split(',');
List<String> drugList = controllerDrugs.text.split(',');
drugs.clear();
for (var drug in drugList) {
var parts = drug.split(':');
var name = parts[0].trim();
var price = double.parse(parts[1].trim());
drugs.add(Drug(name: name, price: price));
}

List<String> location = _locationController.text.split(',');

final user = User(
Expand Down
23 changes: 23 additions & 0 deletions lib/screens/admin/models/drug.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Drug {
String name;
double price;

Drug({
required this.name,
required this.price,
});

factory Drug.fromJson(Map<String, dynamic> json) {
return Drug(
name: json['name'],
price: json['price'],
);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['name'] = name;
data['price'] = price;
return data;
}
}
52 changes: 30 additions & 22 deletions lib/screens/admin/models/user.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
import 'package:brianpharmacy/screens/admin/models/drug.dart';
class User {
String id;
final String name;
List location = [];
List drugs = [];
final String profession;
final String mobileNumber;
String? id;
String name;
List<Drug> drugs;
List<String> location;
String profession;
String mobileNumber;

User({
this.id = '',
this.id,
required this.name,
required this.drugs,
required this.location,
required this.profession,
required this.mobileNumber,
});

Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'drugs': drugs,
'location': location,
'profession': profession,
'mobileNumber': mobileNumber
};
factory User.fromJson(Map<String, dynamic> json) {
final List<dynamic> drugJson = json['drugs'] ?? [];
final List<Drug> drugs = drugJson.map((dynamic item) => Drug.fromJson(item)).toList();
return User(
id: json['id'],
name: json['name'],
drugs: drugs,
location: List<String>.from(json['location'] ?? []),
profession: json['profession'],
mobileNumber: json['mobileNumber'],
);
}

static User fromJson(Map<String, dynamic> json) => User(
name: json['name'],
drugs: json['drugs'],
location: json['location'],
profession: json['profession'],
mobileNumber: json['mobileNumber'],
);
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['name'] = name;
data['drugs'] = drugs.map((drug) => drug.toJson()).toList();
data['location'] = location;
data['profession'] = profession;
data['mobileNumber'] = mobileNumber;
return data;
}
}
115 changes: 56 additions & 59 deletions lib/screens/dashboard/components/geolocation/geolocation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:url_launcher/url_launcher_string.dart';

// ignore: camel_case_types
class geolocationpage extends StatefulWidget {
class GeolocationPage extends StatefulWidget {
final Map<String, dynamic> data;
const geolocationpage({super.key, required this.data});
const GeolocationPage({super.key, required this.data});

@override
State<geolocationpage> createState() => _geolocationpageState();
State<GeolocationPage> createState() => _GeolocationPageState();
}

// ignore: camel_case_types
class _geolocationpageState extends State<geolocationpage> {
class _GeolocationPageState extends State<GeolocationPage> {
String locationMessage = 'current location';
late String lat;
late String long;
double distance = 0.0;

//Getting current location
Future<Position> _getCurrentLocation() async {
Expand Down Expand Up @@ -57,74 +55,80 @@ class _geolocationpageState extends State<geolocationpage> {
});
}

// //open the current location in the GoogleMap
// Future<void> _openMap(String lat, String long) async {
// String googleURL =
// 'https://www.google.com/maps/search/?api=1@query=$lat,$long';
// await canLaunchUrlString(googleURL)
// ? await launchUrlString(googleURL)
// : throw 'could not launch $googleURL';
// }


//open the current location and destination location in the GoogleMap with a route
Future<void> _openMap(String lat, String long) async {
GoogleMapController? _controller;
final Set<Marker> _markers = {};
final Set<Polyline> _polylines = {};

void _addPolyline(LatLng source, LatLng destination) {
_polylines.add(Polyline(
polylineId: const PolylineId('route'),
visible: true,
points: [source, destination],
width: 4,
color: Colors.blue,
startCap: Cap.roundCap,
endCap: Cap.roundCap,
));
}

void _onMapCreated(GoogleMapController controller) async {
_controller = controller;

Position currentPosition = await _getCurrentLocation();
String sourceLat = currentPosition.latitude.toString();
String sourceLong = currentPosition.longitude.toString();

LatLng sourceLatLng =
LatLng(currentPosition.latitude, currentPosition.longitude);
LatLng destinationLatLng = LatLng(double.parse(lat), double.parse(long));
LatLng destinationLatLng = LatLng(
double.parse(widget.data['location'].first),
double.parse(widget.data['location'].last));

// calculate distance between source and destination
distance = await _calculateDistance(sourceLatLng, destinationLatLng);

String googleURL =
'https://www.google.com/maps/dir/?api=1&origin=$sourceLat,$sourceLong&destination=$lat,$long';
await canLaunchUrlString(googleURL)
? await launchUrlString(googleURL)
: throw 'could not launch $googleURL';
CameraPosition cameraPosition = CameraPosition(
target: sourceLatLng,
zoom: 14.4746,
);
_controller!.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));

setState(() {
_markers.clear();
_markers.add(Marker(
markerId: const MarkerId('source'),
position: sourceLatLng,
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
infoWindow: const InfoWindow(title: 'Your Location'),
));
_markers.add(Marker(
markerId: const MarkerId('destination'),
position: destinationLatLng,
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
infoWindow: InfoWindow(title: widget.data['name']),
));
_polylines.clear();
_addPolyline(sourceLatLng, destinationLatLng);
});
}

GoogleMapController? _controller;
Set<Marker> _markers = {};
Set<Polyline> _polylines = {};

void _addPolyline(LatLng source, LatLng destination) {
_polylines.add(Polyline(
polylineId: const PolylineId('route'),
visible: true,
points: [source, destination],
width: 4,
color: Colors.blue,
startCap: Cap.roundCap,
endCap: Cap.roundCap,
));
}

void _onMapCreated(GoogleMapController controller) {
_controller = controller;
Future<double> _calculateDistance(
LatLng sourceLatLng, LatLng destinationLatLng) async {
double distanceInMeters = Geolocator.distanceBetween(
sourceLatLng.latitude,
sourceLatLng.longitude,
destinationLatLng.latitude,
destinationLatLng.longitude);
double distanceInKm = distanceInMeters / 1000;
return distanceInKm;
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Map"),
),
body: Stack(children: [
GoogleMap(
onMapCreated: _onMapCreated,
Expand All @@ -136,21 +140,14 @@ class _geolocationpageState extends State<geolocationpage> {
),
myLocationEnabled: true,
),
Visibility(
visible: distance != 0.0,
child: Text(
'Distance: ${distance.toStringAsFixed(2)} km',
style: const TextStyle(fontSize: 24),
), // show only when distance is calculated
),
]),
floatingActionButton: FloatingActionButton(
onPressed: () {
_getCurrentLocation().then((value) {
lat = widget.data['location'].first;
long = widget.data['location'].last;
setState(() {
locationMessage = 'Latitude: $lat, Longitude: $long';
});
_liveLocation();
_openMap(lat, long);
});
},
child: const Icon(Icons.map),
),
);
}
}
3 changes: 3 additions & 0 deletions lib/screens/dashboard/components/geolocation/googleMaps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class _GoogleMapsState extends State<GoogleMaps> {

CameraPosition cameraPosition =
CameraPosition(target: latLngPosition, zoom: 14);

// wait for the GoogleMapController to be initialized before animating the camera
await _controllerGoogleMap.future;
newGoogleMapController
.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
}
Expand Down
Loading