You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
healthcare-frontend/lib/prescriptions.dart

223 lines
6.4 KiB

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:geolocator/geolocator.dart';
import 'package:healthcare_user/common/settings.dart';
import 'package:healthcare_user/models/pharmacies.dart';
import 'package:image_picker/image_picker.dart';
class Prescriptions extends StatefulWidget {
const Prescriptions({Key? key}) : super(key: key);
@override
State<Prescriptions> createState() => _PrescriptionsState();
}
class _PrescriptionsState extends State<Prescriptions> {
final ImagePicker _picker = ImagePicker();
String Url='';
List<PharmaciesModel> pharmaciesList = [];
List<PharmaciesModel> FilteredList = [];
bool isSupplierDataLoading=false;
bool isSereverIssue = false;
double lat=0;
double lng=0;
String userAddress='';
String dropdownArea = '2';
//String dropdownType = 'Tank';
var AreaItems = [
'2',
'5',
'10',
'25',
'50',
'100'
];
Future<void> getAllPharmaciesData(var distance) async {
try {
var pharmacyResponse = await AppSettings.getAllpharmacies();
setState(() {
pharmaciesList =
((jsonDecode(pharmacyResponse)['data']) as List).map((dynamic model) {
return PharmaciesModel.fromJson(model);
}).toList();
FilteredList=[];
pharmaciesList.forEach((element) async{
var distanceInM;
if(distance=='2'){
distanceInM=2000;
}
else if(distance=='5'){
distanceInM=5000;
}
else if(distance=='10'){
distanceInM=10000;
}
else if(distance=='25'){
distanceInM=25000;
}
else if(distance=='50'){
distanceInM=50000;
}
else if(distance=='100'){
distanceInM=100000;
}
double distanceInMeters = await Geolocator.distanceBetween(element.lat,element.lng,lat,lng);
if(distanceInMeters<=distanceInM){
FilteredList.add(element);
}
});
//FilteredList=suppliersList.where((product) => product.address.toString().toUpperCase()=='SUMP').toList();
isSupplierDataLoading = false;
});
} catch (e) {
setState(() {
isSupplierDataLoading = false;
isSereverIssue = true;
});
}
}
@override
void initState() {
lat=AppSettings.userLatitude;
lng=AppSettings.userLongitude;
userAddress=AppSettings.userAddress;
getAllPharmaciesData(dropdownArea);
super.initState();
}
Future pickImageFromGallery() async {
try {
final image = await _picker.pickImage(source: ImageSource.gallery);
if (image == null) return;
final imageTemp = File(image.path);
var res=await AppSettings.uploadImageHTTPForPrescriptions(image);
print(jsonDecode(res));
setState(() {
Url = jsonDecode(res)['picture'];
});
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
Future takeImageFromCamera() async {
try {
final image = await _picker.pickImage(source: ImageSource.camera);
if (image == null) return;
final imageTemp = File(image.path);
var res=await AppSettings.uploadImageHTTPForPrescriptions(image);
print(jsonDecode(res));
setState(() {
Url = jsonDecode(res)['picture'];
});
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
/**/
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppSettings.appBar('Prescriptions'),
body: Container(
child: Column(
children: [
Padding(
padding: EdgeInsets.all(10),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor, // background
onPrimary: Colors.white, // foreground
),
onPressed: () async{
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return SizedBox(
height: 200,
child: Center(
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: Icon(
Icons.camera_alt_outlined,
size: 100,
color: greyColor,
),
onTap: () async {
await takeImageFromCamera();
Navigator.pop(context);
},
),
SizedBox(
width: MediaQuery.of(context)
.size
.width *
.20,
),
GestureDetector(
child: Icon(
Icons.photo,
size: 100,
color: greyColor,
),
onTap: () async {
await pickImageFromGallery();
Navigator.pop(context);
},
),
],
),
),
);
});
},
child: const Text('Select image'),
),
),
Container(
width: MediaQuery.of(context).size.width * .18,
height:
MediaQuery.of(context).size.height * .10,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
image: DecorationImage(
image: NetworkImage(Url) as ImageProvider, // picked file
fit: BoxFit.contain)),
),
],
)
),
);
}
}