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/common/updateprofile.dart

836 lines
40 KiB

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthcare_user/common/dashboard.dart';
import 'package:healthcare_user/common/settings.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:google_maps_flutter_android/google_maps_flutter_android.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'package:location/location.dart' as locationmap;
import '../google_maps_place_picker_mb/src/models/pick_result.dart';
import '../google_maps_place_picker_mb/src/place_picker.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:healthcare_user/google_maps_place_picker_mb/google_maps_place_picker.dart';
import 'package:healthcare_user/keys.dart';
class UpdateProfile extends StatefulWidget {
const UpdateProfile({Key? key}) : super(key: key);
@override
State<UpdateProfile> createState() => _UpdateprofileState();
}
class _UpdateprofileState extends State<UpdateProfile> {
bool isPwdObscureText = true;
bool isConfirmPwdObscureText = true;
TextEditingController nameController = TextEditingController();
TextEditingController mobileNumberController = TextEditingController();
TextEditingController emailController = TextEditingController();
TextEditingController ageController = TextEditingController();
PickResult? selectedPlace;
bool _mapsInitialized = false;
final String _mapsRenderer = "latest";
var kInitialPosition = const LatLng(15.462477, 78.717401);
locationmap.Location location = locationmap.Location();
final GoogleMapsFlutterPlatform mapsImplementation =
GoogleMapsFlutterPlatform.instance;
double lat = 0;
double lng = 0;
String address = '';
TextEditingController userAddressDescriptionController =
TextEditingController();
TextEditingController emergencyContactName1Controller =
TextEditingController();
TextEditingController emergencyContactRelation1Controller =
TextEditingController();
TextEditingController emergencyContactNumber1Controller =
TextEditingController();
TextEditingController emergencyContactName2Controller =
TextEditingController();
TextEditingController emergencyContactRelation2Controller =
TextEditingController();
TextEditingController emergencyContactNumber2Controller =
TextEditingController();
TextEditingController bloodGroupController = TextEditingController();
List emergenyContacts = [];
void initRenderer() {
if (_mapsInitialized) return;
if (mapsImplementation is GoogleMapsFlutterAndroid) {
switch (_mapsRenderer) {
case "legacy":
(mapsImplementation as GoogleMapsFlutterAndroid)
.initializeWithRenderer(AndroidMapRenderer.legacy);
break;
case "latest":
(mapsImplementation as GoogleMapsFlutterAndroid)
.initializeWithRenderer(AndroidMapRenderer.latest);
break;
}
}
setState(() {
_mapsInitialized = true;
});
}
@override
void initState() {
isPwdObscureText = true;
isConfirmPwdObscureText = true;
nameController.text = AppSettings.userName;
mobileNumberController.text = AppSettings.phoneNumber;
emailController.text = AppSettings.email;
ageController.text = AppSettings.age;
address = AppSettings.userAddress;
userAddressDescriptionController.text = AppSettings.detailedAddress;
lat = AppSettings.userLatitude;
lng = AppSettings.userLongitude;
emergencyContactName1Controller.text = AppSettings.emergencyRelationName1;
emergencyContactRelation1Controller.text = AppSettings.emergencyRelation1;
emergencyContactNumber1Controller.text =
AppSettings.emergencyContactNumber1;
emergencyContactName2Controller.text = AppSettings.emergencyRelationName2;
emergencyContactRelation2Controller.text = AppSettings.emergencyRelation2;
emergencyContactNumber2Controller.text =
AppSettings.emergencyContactNumber2;
bloodGroupController.text = AppSettings.bloodGroup;
super.initState();
}
final ImagePicker _picker = ImagePicker();
Future pickImageFromGallery() async {
try {
final image = await _picker.pickImage(source: ImageSource.gallery);
if (image == null) return;
final imageTemp = File(image.path);
AppSettings.preLoaderDialog(context);
var res = await AppSettings.uploadImageHTTPNew(image);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
setState(() {
AppSettings.profilePictureUrl = jsonDecode(res)['picture'];
});
AppSettings.saveData('profile', jsonDecode(res)['picture'], 'STRING');
} 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);
AppSettings.preLoaderDialog(context);
var res = await AppSettings.uploadImageHTTPNew(image);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
setState(() {
AppSettings.profilePictureUrl = jsonDecode(res)['picture'];
});
AppSettings.saveData('profile', jsonDecode(res)['picture'], 'STRING');
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
Future pickBloodGroupImageFromGallery() async {
try {
final image = await _picker.pickImage(source: ImageSource.gallery);
if (image == null) return;
final imageTemp = File(image.path);
AppSettings.preLoaderDialog(context);
var res = await AppSettings.uploadBloodGroupImage(image);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
setState(() {
AppSettings.bloodGroupPictureUrl = jsonDecode(res)['blood_Group_picture'];
});
AppSettings.saveData(
'bloodGroupPicture', jsonDecode(res)['blood_Group_picture'], 'STRING');
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
Future takeBloodGroupImageFromCamera() async {
try {
final image = await _picker.pickImage(source: ImageSource.camera);
if (image == null) return;
final imageTemp = File(image.path);
AppSettings.preLoaderDialog(context);
var res = await AppSettings.uploadBloodGroupImage(image);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
setState(() {
AppSettings.bloodGroupPictureUrl = jsonDecode(res)['blood_Group_picture'];
});
AppSettings.saveData(
'bloodGroupPicture', jsonDecode(res)['blood_Group_picture'], 'STRING');
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppSettings.appBar('Edit Profile'),
body: Stack(children: <Widget>[
Container(
decoration: const BoxDecoration(
/* image: DecorationImage(
image: AssetImage("images/backgroundimage.png"),
fit: BoxFit.cover,
),*/
),
),
GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: SafeArea(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
SizedBox(height: MediaQuery.of(context).size.height * .05),
/*Container(
child: Image(
image: AssetImage('images/logo.png'),
height: MediaQuery.of(context).size.height * .10,
)),*/
Container(
child: GestureDetector(
child: Container(
width: MediaQuery.of(context).size.width * .30,
height: MediaQuery.of(context).size.height * .20,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: (AppSettings.profilePictureUrl != '' &&
AppSettings.profilePictureUrl !=
'null')
? NetworkImage(
AppSettings.profilePictureUrl)
as ImageProvider
: AssetImage(
"images/profile_pic.png"), // picked file
fit: BoxFit.fitWidth)),
),
onTap: () {
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: primaryColor,
),
onTap: () async {
await takeImageFromCamera();
Navigator.pop(context);
},
),
SizedBox(
width: MediaQuery.of(context)
.size
.width *
.20,
),
GestureDetector(
child: Icon(
Icons.photo,
size: 100,
color: primaryColor,
),
onTap: () async {
await pickImageFromGallery();
Navigator.pop(context);
},
),
],
),
),
);
});
},
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
child: TextFormField(
cursorColor: greyColor,
controller: nameController,
decoration: textFormFieldDecorationGrey(
Icons.person, 'Username'),
),
), //name
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
child: TextFormField(
cursorColor: greyColor,
controller: emailController,
decoration: textFormFieldDecorationGrey(
Icons.mail, 'Enter email ID'),
),
), //email
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
child: TextFormField(
cursorColor: greyColor,
controller: mobileNumberController,
decoration: textFormFieldDecorationGrey(
Icons.phone_android, 'Mobile Number'),
),
), //mobile
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
child: TextFormField(
cursorColor: greyColor,
controller: ageController,
decoration: textFormFieldDecorationGrey(
Icons.person, 'Enter age'),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
child: TextFormField(
cursorColor: greyColor,
controller: bloodGroupController,
decoration: textFormFieldDecorationGrey(
Icons.bloodtype, 'Enter Blood Group'),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: AppSettings.bloodGroupPictureUrl != '' &&
AppSettings.bloodGroupPictureUrl != 'null'
? Row(
children: [
Container(
width:
MediaQuery.of(context).size.width * .30,
height: MediaQuery.of(context).size.height *
.20,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
image: DecorationImage(
image: NetworkImage(AppSettings
.bloodGroupPictureUrl)
as ImageProvider, // picked file
fit: BoxFit.fitWidth)),
),
IconButton(
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: primaryColor,
),
onTap: () async {
await takeBloodGroupImageFromCamera();
Navigator.pop(context);
},
),
SizedBox(
width:
MediaQuery.of(context)
.size
.width *
.20,
),
GestureDetector(
child: Icon(
Icons.photo,
size: 100,
color: primaryColor,
),
onTap: () async {
await pickBloodGroupImageFromGallery();
Navigator.pop(context);
},
),
],
),
),
);
});
},
icon: Icon(
Icons.edit,
color: Colors.red,
size: 40,
),
),
],
)
: Visibility(
visible: true,
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: primaryColor,
),
onTap: () async {
await takeBloodGroupImageFromCamera();
Navigator.pop(context);
},
),
SizedBox(
width:
MediaQuery.of(context)
.size
.width *
.20,
),
GestureDetector(
child: Icon(
Icons.photo,
size: 100,
color: primaryColor,
),
onTap: () async {
await pickBloodGroupImageFromGallery();
Navigator.pop(context);
},
),
],
),
),
);
});
},
child: const Text('Upload Blood Group Image'),
),
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Text(
'Emergency Contact Details',
style: TextStyle(
color: primaryColor,
fontSize: 14,
fontWeight: FontWeight.bold),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
width: double.infinity,
decoration: BoxDecoration(
color: secondaryColor,
border: Border.all(
//width: 10,
color: Colors.white,
),
borderRadius: BorderRadius.circular(
20,
)),
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextFormField(
cursorColor: greyColor,
controller: emergencyContactName1Controller,
decoration: textFormFieldDecorationGrey(
Icons.person, 'Enter Name'),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
TextFormField(
cursorColor: greyColor,
controller: emergencyContactRelation1Controller,
decoration: textFormFieldDecorationGrey(
Icons.reduce_capacity, 'Relation'),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
TextFormField(
cursorColor: greyColor,
controller: emergencyContactNumber1Controller,
decoration: textFormFieldDecorationGrey(
Icons.phone_android, 'Contact Number'),
),
],
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
width: double.infinity,
decoration: BoxDecoration(
color: secondaryColor,
border: Border.all(
//width: 10,
color: Colors.white,
),
borderRadius: BorderRadius.circular(
20,
)),
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextFormField(
cursorColor: greyColor,
controller: emergencyContactName2Controller,
decoration: textFormFieldDecorationGrey(
Icons.person, 'Enter Name'),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
TextFormField(
cursorColor: greyColor,
controller: emergencyContactRelation2Controller,
decoration: textFormFieldDecorationGrey(
Icons.reduce_capacity, 'Relation'),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
TextFormField(
cursorColor: greyColor,
controller: emergencyContactNumber2Controller,
decoration: textFormFieldDecorationGrey(
Icons.phone_android, 'Contact Number'),
),
],
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Row(
children: [
Text(
'Current Location :',
style: labelTextStyle(),
),
SizedBox(
width: MediaQuery.of(context).size.width * .02,
),
Expanded(
child: Text(
address,
style: valuesTextStyle(),
))
],
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: TextButton(
onPressed: () {
location.serviceEnabled().then((value) {
if (value) {
initRenderer();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return PlacePicker(
resizeToAvoidBottomInset: false,
hintText: "Find a place ...",
searchingText: "Please wait ...",
selectText: "Select place",
outsideOfPickAreaText:
"Place not in area",
initialPosition: kInitialPosition,
useCurrentLocation: true,
selectInitialPosition: true,
usePinPointingSearch: true,
usePlaceDetailSearch: true,
zoomGesturesEnabled: true,
zoomControlsEnabled: true,
onMapCreated:
(GoogleMapController controller) {},
onPlacePicked:
(PickResult result) async {
setState(() {
selectedPlace = result;
lat = selectedPlace!
.geometry!.location.lat;
lng = selectedPlace!
.geometry!.location.lng;
if (selectedPlace!.types!.length ==
1) {
address = selectedPlace!
.formattedAddress!;
} else {
address = selectedPlace!.name! +
', ' +
selectedPlace!
.formattedAddress!;
}
Navigator.of(context).pop();
});
},
onMapTypeChanged: (MapType mapType) {},
apiKey: Platform.isAndroid
? APIKeys.androidApiKey
: APIKeys.iosApiKey,
forceAndroidLocationManager: true,
);
},
),
);
} else {
showGeneralDialog(
context: context,
pageBuilder: (context, x, y) {
return Scaffold(
backgroundColor:
Colors.grey.withOpacity(.5),
body: Center(
child: Container(
width: double.infinity,
height: 150,
padding: const EdgeInsets.symmetric(
horizontal: 20),
child: Card(
child: Padding(
padding:
const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
const Text(
"Please enable the location",
style: TextStyle(
fontSize: 18,
fontWeight:
FontWeight.w500,
),
),
const SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Cancel"),
),
],
),
),
),
),
),
);
},
);
}
});
},
child: const Text(
'Change',
style: TextStyle(
color: primaryColor,
decoration: TextDecoration.underline,
),
),
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .02,
),
Container(
child: TextFormField(
cursorColor: greyColor,
controller: userAddressDescriptionController,
keyboardType: TextInputType.emailAddress,
decoration: textFormFieldDecorationGrey(
Icons.plagiarism_outlined,
'Address Description (Ex: Flat No)'),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * .04,
),
/*Container(
width:double.infinity,
height: MediaQuery.of(context).size.height * .05,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: buttonColors, // background
onPrimary: Colors.black, // foreground
),
onPressed: () async{
var payload = new Map<String, dynamic>();
payload["latitude"] = lat;
payload["longitude"] = lng;
payload["address1"] = address;
payload["address2"] = userAddressDescriptionController.text;
bool updateStatus = await AppSettings.updateLocation(payload);
if(updateStatus){
AppSettings.longSuccessToast("Location Updated");
}
else{
AppSettings.longFailedToast("Failed to update location");
}
},
child: const Text('Update My Location'),
)),*/
Container(
width: double.infinity,
height: MediaQuery.of(context).size.height * .05,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor, // background
onPrimary: Colors.white, // foreground
),
onPressed: () async {
emergenyContacts = [];
int age = 0;
emergenyContacts.add({
"name": emergencyContactName1Controller.text,
"relationship":
emergencyContactRelation1Controller.text,
"contactNumber":
emergencyContactNumber1Controller.text,
});
emergenyContacts.add({
"name": emergencyContactName2Controller.text,
"relationship":
emergencyContactRelation2Controller.text,
"contactNumber":
emergencyContactNumber2Controller.text,
});
if (ageController.text.toString() != '') {
age = int.parse(ageController.text.toString());
} else {
age = 0;
}
var payload = new Map<String, dynamic>();
payload["username"] =
nameController.text.toString();
payload["phone"] =
mobileNumberController.text.toString();
payload["age"] = age;
payload["latitude"] = lat;
payload["longitude"] = lng;
payload["address1"] = address;
payload["address2"] =
userAddressDescriptionController.text;
payload["bloodGroup"] = bloodGroupController.text;
payload["emails"] = [
{"email": emailController.text.toString()}
];
payload["emergencyContacts"] = emergenyContacts;
payload["blood_Group_picture"] =
AppSettings.bloodGroupPictureUrl;
bool signUpStatus =
await AppSettings.updateProfile(payload);
try {
if (signUpStatus) {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Dashboard()),
);
AppSettings.longSuccessToast(
"profile updated");
} else {
AppSettings.longFailedToast(
"profile not updated");
}
} catch (exception) {
print(exception);
AppSettings.longFailedToast(
"Please enter valid details");
}
},
child: Text('Update'),
)),
],
),
),
)),
),
]));
}
}