From ada87ac24315f2904f9f137658b9a36f02ceda3b Mon Sep 17 00:00:00 2001 From: Gidzzy Date: Fri, 21 Apr 2023 01:30:35 +0300 Subject: [PATCH 1/4] Recent update --- .../components/geolocation/geolocation.dart | 17 ----------------- .../components/header_with_search_bar.dart | 2 +- pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/screens/dashboard/components/geolocation/geolocation.dart b/lib/screens/dashboard/components/geolocation/geolocation.dart index f58c39b..66294d2 100644 --- a/lib/screens/dashboard/components/geolocation/geolocation.dart +++ b/lib/screens/dashboard/components/geolocation/geolocation.dart @@ -3,7 +3,6 @@ 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 { @@ -57,16 +56,6 @@ class _geolocationpageState extends State { }); } - // //open the current location in the GoogleMap - // Future _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 _openMap(String lat, String long) async { Position currentPosition = await _getCurrentLocation(); @@ -77,12 +66,6 @@ class _geolocationpageState extends State { LatLng(currentPosition.latitude, currentPosition.longitude); LatLng destinationLatLng = LatLng(double.parse(lat), double.parse(long)); - 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'; - setState(() { _markers.clear(); _markers.add(Marker( diff --git a/lib/screens/dashboard/components/header_with_search_bar.dart b/lib/screens/dashboard/components/header_with_search_bar.dart index 8199bab..2a1aefb 100644 --- a/lib/screens/dashboard/components/header_with_search_bar.dart +++ b/lib/screens/dashboard/components/header_with_search_bar.dart @@ -108,7 +108,7 @@ class _HeaderWithSearchBoxState extends State { child: Container( alignment: Alignment.center, margin: const EdgeInsets.symmetric(horizontal: kDefaultPadding), - height: 80, + height: 100, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(20), diff --git a/pubspec.lock b/pubspec.lock index aab922a..ea25538 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -360,6 +360,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.6" + google_maps_webservice: + dependency: "direct main" + description: + name: google_maps_webservice + sha256: "188bbb363adf1bc69c708029e315f383020c1caba8c5146176de1a6f6feb3524" + url: "https://pub.dev" + source: hosted + version: "0.0.19" http: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 043643a..823d022 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -48,6 +48,7 @@ dependencies: location: ^4.4.0 geocoding: ^2.1.0 flutter_polyline_points: ^1.0.0 + google_maps_webservice: ^0.0.19 dev_dependencies: flutter_test: From 8ff10488bddff5a620602f0c163ca23300c8f3fe Mon Sep 17 00:00:00 2001 From: Giddy-K Date: Sat, 29 Apr 2023 23:58:55 +0300 Subject: [PATCH 2/4] recent price changes --- .../admin/adminPages/admin_details.dart | 21 ++++++-- lib/screens/admin/models/drug.dart | 23 ++++++++ lib/screens/admin/models/user.dart | 52 +++++++++++-------- .../components/header_with_search_bar.dart | 41 ++++++++++----- 4 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 lib/screens/admin/models/drug.dart diff --git a/lib/screens/admin/adminPages/admin_details.dart b/lib/screens/admin/adminPages/admin_details.dart index e0babf6..3adfe92 100644 --- a/lib/screens/admin/adminPages/admin_details.dart +++ b/lib/screens/admin/adminPages/admin_details.dart @@ -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 createState() => _AdminDetailsState(); @@ -17,10 +18,12 @@ class _AdminDetailsState extends State { List 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 drugs = []; void _chooseLocation() async { final pickedLocation = await Navigator.push( @@ -65,7 +68,7 @@ class _AdminDetailsState extends State { ), TextField( controller: controllerProfessional, - decoration: decoration('Proffession'), + decoration: decoration('Profession'), ), const SizedBox( height: 24, @@ -102,7 +105,15 @@ class _AdminDetailsState extends State { ), ElevatedButton( onPressed: () { - List drugs = controllerDrugs.text.split(','); + List 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 location = _locationController.text.split(','); final user = User( diff --git a/lib/screens/admin/models/drug.dart b/lib/screens/admin/models/drug.dart new file mode 100644 index 0000000..f5c7848 --- /dev/null +++ b/lib/screens/admin/models/drug.dart @@ -0,0 +1,23 @@ +class Drug { + String name; + double price; + + Drug({ + required this.name, + required this.price, + }); + + factory Drug.fromJson(Map json) { + return Drug( + name: json['name'], + price: json['price'], + ); + } + + Map toJson() { + final Map data = {}; + data['name'] = name; + data['price'] = price; + return data; + } +} diff --git a/lib/screens/admin/models/user.dart b/lib/screens/admin/models/user.dart index c25af00..67bbf56 100644 --- a/lib/screens/admin/models/user.dart +++ b/lib/screens/admin/models/user.dart @@ -1,13 +1,14 @@ +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 drugs; + List location; + String profession; + String mobileNumber; User({ - this.id = '', + this.id, required this.name, required this.drugs, required this.location, @@ -15,20 +16,27 @@ class User { required this.mobileNumber, }); - Map toJson() => { - 'id': id, - 'name': name, - 'drugs': drugs, - 'location': location, - 'profession': profession, - 'mobileNumber': mobileNumber - }; + factory User.fromJson(Map json) { + final List drugJson = json['drugs'] ?? []; + final List drugs = drugJson.map((dynamic item) => Drug.fromJson(item)).toList(); + return User( + id: json['id'], + name: json['name'], + drugs: drugs, + location: List.from(json['location'] ?? []), + profession: json['profession'], + mobileNumber: json['mobileNumber'], + ); + } - static User fromJson(Map json) => User( - name: json['name'], - drugs: json['drugs'], - location: json['location'], - profession: json['profession'], - mobileNumber: json['mobileNumber'], - ); + Map toJson() { + final Map data = {}; + 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; + } } diff --git a/lib/screens/dashboard/components/header_with_search_bar.dart b/lib/screens/dashboard/components/header_with_search_bar.dart index 2a1aefb..ede40be 100644 --- a/lib/screens/dashboard/components/header_with_search_bar.dart +++ b/lib/screens/dashboard/components/header_with_search_bar.dart @@ -1,5 +1,6 @@ import 'package:brianpharmacy/constraints.dart'; import 'package:brianpharmacy/screens/admin/adminPages/geo_location.dart'; +import 'package:brianpharmacy/screens/admin/models/drug.dart'; import 'package:brianpharmacy/screens/dashboard/components/geolocation/geolocation.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; @@ -132,17 +133,26 @@ class _HeaderWithSearchBoxState extends State { child: CircularProgressIndicator(), ); } - - final query = drug.toLowerCase(); - + String query = ''; final filteredDocs = snapshots.data!.docs.where((doc) { final data = doc.data() as Map; - final drugs = List.from(data['drugs']); - final matches = drugs - .any((drug) => drug.toLowerCase().contains(query)); + final drugs = List.from( + data['drugs'].map((d) => Drug.fromJson(d))); + final matches = drugs.any( + (drug) => drug.name.toLowerCase().contains(query)); return matches; }).toList(); + for (final doc in filteredDocs) { + final data = doc.data() as Map; + final drugs = List.from( + data['drugs'].map((d) => Drug.fromJson(d))); + final matchedDrugs = drugs.where( + (drug) => drug.name.toLowerCase().contains(query)); + for (final drug in matchedDrugs) { + print(drug.name); + } + } return ListView.builder( itemCount: filteredDocs.length, itemBuilder: (context, index) { @@ -195,17 +205,24 @@ class _HeaderWithSearchBoxState extends State { TextSpan( children: [ const TextSpan( - text: 'Latitude: ', + text: 'Drug: ', style: TextStyle( - fontWeight: FontWeight.bold), + fontWeight: FontWeight.bold, + ), + ), + TextSpan( + text: data['drugs'][index]['name'], ), - TextSpan(text: data['location'].first), const TextSpan( - text: 'Longitude: ', + text: ' Price: ', style: TextStyle( - fontWeight: FontWeight.bold), + fontWeight: FontWeight.bold, + ), + ), + TextSpan( + text: data['drugs'][index]['price'] + .toString(), ), - TextSpan(text: data['location'].last), ], ), ), From 4ab0004eb76cbbf34d3174725c9709978ddbac82 Mon Sep 17 00:00:00 2001 From: Giddy-K Date: Mon, 1 May 2023 00:32:12 +0300 Subject: [PATCH 3/4] FInal changes --- .../components/geolocation/geolocation.dart | 100 ++++++++++-------- .../components/geolocation/googleMaps.dart | 3 + .../components/header_with_search_bar.dart | 4 +- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/lib/screens/dashboard/components/geolocation/geolocation.dart b/lib/screens/dashboard/components/geolocation/geolocation.dart index 66294d2..227d30c 100644 --- a/lib/screens/dashboard/components/geolocation/geolocation.dart +++ b/lib/screens/dashboard/components/geolocation/geolocation.dart @@ -4,20 +4,19 @@ import 'package:flutter/material.dart'; import 'package:geolocator/geolocator.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; -// ignore: camel_case_types -class geolocationpage extends StatefulWidget { +class GeolocationPage extends StatefulWidget { final Map data; - const geolocationpage({super.key, required this.data}); + const GeolocationPage({super.key, required this.data}); @override - State createState() => _geolocationpageState(); + State createState() => _GeolocationPageState(); } -// ignore: camel_case_types -class _geolocationpageState extends State { +class _GeolocationPageState extends State { String locationMessage = 'current location'; late String lat; late String long; + double distance = 0.0; //Getting current location Future _getCurrentLocation() async { @@ -56,28 +55,56 @@ class _geolocationpageState extends State { }); } -//open the current location and destination location in the GoogleMap with a route - Future _openMap(String lat, String long) async { + GoogleMapController? _controller; + final Set _markers = {}; + final Set _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); + + 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(); @@ -85,29 +112,23 @@ class _geolocationpageState extends State { }); } - GoogleMapController? _controller; - Set _markers = {}; - Set _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 _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, @@ -119,21 +140,14 @@ class _geolocationpageState extends State { ), 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), - ), ); } } diff --git a/lib/screens/dashboard/components/geolocation/googleMaps.dart b/lib/screens/dashboard/components/geolocation/googleMaps.dart index 81fd438..df573ea 100644 --- a/lib/screens/dashboard/components/geolocation/googleMaps.dart +++ b/lib/screens/dashboard/components/geolocation/googleMaps.dart @@ -32,6 +32,9 @@ class _GoogleMapsState extends State { 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)); } diff --git a/lib/screens/dashboard/components/header_with_search_bar.dart b/lib/screens/dashboard/components/header_with_search_bar.dart index ede40be..8a96f0a 100644 --- a/lib/screens/dashboard/components/header_with_search_bar.dart +++ b/lib/screens/dashboard/components/header_with_search_bar.dart @@ -1,9 +1,9 @@ import 'package:brianpharmacy/constraints.dart'; -import 'package:brianpharmacy/screens/admin/adminPages/geo_location.dart'; import 'package:brianpharmacy/screens/admin/models/drug.dart'; import 'package:brianpharmacy/screens/dashboard/components/geolocation/geolocation.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; +import 'dart:math'; class HeaderWithSearchBox extends StatefulWidget { const HeaderWithSearchBox({ @@ -175,7 +175,7 @@ class _HeaderWithSearchBoxState extends State { MaterialPageRoute( builder: (BuildContext context) => - geolocationpage( + GeolocationPage( data: data, ), )); From 4e968a6f185518d98c2a8813ef511bf9a927727d Mon Sep 17 00:00:00 2001 From: Giddy-K Date: Mon, 1 May 2023 00:41:35 +0300 Subject: [PATCH 4/4] 1st May changes --- lib/screens/admin/adminPages/admin_details.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/screens/admin/adminPages/admin_details.dart b/lib/screens/admin/adminPages/admin_details.dart index 3adfe92..40081da 100644 --- a/lib/screens/admin/adminPages/admin_details.dart +++ b/lib/screens/admin/adminPages/admin_details.dart @@ -33,10 +33,12 @@ class _AdminDetailsState extends State { ), ); if (pickedLocation != null) { - setState(() { - _locationController.text = - ' ${pickedLocation.latitude},${pickedLocation.longitude}'; - }); + setState( + () { + _locationController.text = + ' ${pickedLocation.latitude},${pickedLocation.longitude}'; + }, + ); } }