profile alertbox functionality added

This commit is contained in:
venbaittech 2024-11-12 15:45:29 +05:30
parent bc8f7d7e28
commit aaffcbe362

View File

@ -149,169 +149,80 @@ class _ProfileScreenState extends State<ProfileScreen> {
});
}
void _saveProfile() async {
if (_formKey.currentState?.validate() ?? false) {
try {
String userID = 'tsgehyvgy6owypj';
// Retrieve data from text fields and other inputs
String fullName = _fullNameController.text;
// String username = 'users55538';
// String email = 'surendarsuri30@gmail.com';
String dateOfBirth = _dateController.text; // in DD/MM/YYYY format
String countryRegion = _selectedCountry ?? '';
bool termsAccepted = _termsAccepted;
void showConfirmationDialog(BuildContext context) async {
final result = await showDialog<bool>(
context: context,
builder: (context) => const ConfirmationDialog(),
);
if (result == true) {
if (_formKey.currentState?.validate() ?? false) {
try {
String userID = 'tsgehyvgy6owypj';
// Retrieve data from text fields and other inputs
String fullName = _fullNameController.text;
// String username = 'users55538';
// String email = 'surendarsuri30@gmail.com';
String dateOfBirth = _dateController.text; // in DD/MM/YYYY format
String countryRegion = _selectedCountry ?? '';
bool termsAccepted = _termsAccepted;
// Parse the date from dd/MM/yyyy format and convert to yyyy-MM-dd
// Parse the date from dd/MM/yyyy format and format to yyyy-MM-dd
DateTime dob = DateFormat('dd/MM/yyyy').parse(dateOfBirth);
String formattedDob = DateFormat('yyyy-MM-dd').format(dob);
// Parse the date from dd/MM/yyyy format and convert to yyyy-MM-dd
// Parse the date from dd/MM/yyyy format and format to yyyy-MM-dd
DateTime dob = DateFormat('dd/MM/yyyy').parse(dateOfBirth);
String formattedDob = DateFormat('yyyy-MM-dd').format(dob);
// Prepare form data
final request = http.MultipartRequest(
'PATCH',
Uri.parse(
'https://pb.venbait.in/api/collections/users/records/$userID'), // replace with actual user ID
);
// Prepare form data
final request = http.MultipartRequest(
'PATCH',
Uri.parse(
'https://pb.venbait.in/api/collections/users/records/$userID'), // replace with actual user ID
);
// Set the fields for the user profile
request.fields['full_name'] = fullName;
// request.fields['username'] = username;
// request.fields['email'] = email;
request.fields['dob'] = formattedDob; // ensure correct format
request.fields['country_region'] = countryRegion;
request.fields['terms_accepted'] = termsAccepted.toString();
// Set the fields for the user profile
request.fields['full_name'] = fullName;
// request.fields['username'] = username;
// request.fields['email'] = email;
request.fields['dob'] = formattedDob; // ensure correct format
request.fields['country_region'] = countryRegion;
request.fields['terms_accepted'] = termsAccepted.toString();
// Add image file if selected
if (_profileImage != null) {
request.files.add(
await http.MultipartFile.fromPath('avatar', _profileImage!.path));
}
final adminAuth = await _pb.admins
.authWithPassword('pb@venbainfotech.com', 'pb@venbainfotech.com');
final adminToken = adminAuth.token;
// Add PocketBase auth headers if needed (for authenticated requests)
request.headers['Authorization'] = 'Bearer ${adminToken}';
// Add image file if selected
if (_profileImage != null) {
request.files.add(await http.MultipartFile.fromPath(
'avatar', _profileImage!.path));
}
final adminAuth = await _pb.admins
.authWithPassword('pb@venbainfotech.com', 'pb@venbainfotech.com');
final adminToken = adminAuth.token;
// Add PocketBase auth headers if needed (for authenticated requests)
request.headers['Authorization'] = 'Bearer ${adminToken}';
print(request);
print(request);
// Send the request
final response = await request.send();
// Send the request
final response = await request.send();
if (response.statusCode == 200) {
print(response.statusCode);
_resetFormFields();
Navigator.pushNamed(context, 'home');
if (response.statusCode == 200) {
print(response.statusCode);
_resetFormFields();
Navigator.pushNamed(context, 'home');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Profile updated successfully!")));
} else {
// Log the response body for better debugging
final responseBody = await response.stream.bytesToString();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
"Failed to update profile: ${response.reasonPhrase}, Body: $responseBody")));
}
} catch (error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Profile updated successfully!")));
} else {
// Log the response body for better debugging
final responseBody = await response.stream.bytesToString();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
"Failed to update profile: ${response.reasonPhrase}, Body: $responseBody")));
SnackBar(content: Text("Failed to update profile: $error")));
}
} catch (error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Failed to update profile: $error")));
}
}
}
void _confirmSaveProfile() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
// title: Text("Confirm Save"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Are you sure you want to save this page?",
style: const TextStyle(
fontSize: 14,
color: Color(0xFF898C81),
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
),
textAlign: TextAlign.center,
),
SizedBox(height: 8),
Text.rich(
TextSpan(
text: "Once saved, you will not be able to change your ",
style: TextStyle(
fontSize: 14,
color: Color(0xFF898C81),
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
),
children: [
TextSpan(
text: "name",
style: TextStyle(
fontWeight: FontWeight.w700), // Bold for "name"
),
TextSpan(
text: " or ",
),
TextSpan(
text: "date of birth",
style: TextStyle(
fontWeight:
FontWeight.w700), // Bold for "date of birth"
),
TextSpan(
text: ".",
),
],
),
textAlign: TextAlign.center,
)
],
),
actionsAlignment:
MainAxisAlignment.center, // Center-align the buttons
actions: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
side: BorderSide(
color: Color(0xFF92722A)), // Set border color for "Cancel"
foregroundColor: Color(0xFF92722A), // Set text color
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(8), // Optional rounded corners
),
backgroundColor: Colors.white, // No background color
),
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
),
SizedBox(width: 8), // Spacing between buttons
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor:
Color(0xFF92722A), // Set background color for "Confirm"
foregroundColor: Colors.white, // Set text color
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(8), // Optional rounded corners
),
),
child: Text("Confirm"),
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
_saveProfile(); // Call save function
},
),
],
);
},
);
}
void _resetFormFields() {
print('reset');
setState(() {
@ -601,7 +512,17 @@ class _ProfileScreenState extends State<ProfileScreen> {
onPressed: () {
if (_formKey.currentState?.validate() ?? false) {
_formKey.currentState?.save();
_confirmSaveProfile();
if (_termsAccepted) {
_formKey.currentState?.save();
// _confirmSaveProfile();
showConfirmationDialog(context);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Please accept the terms and conditions.')),
);
}
}
},
icon: Icon(
@ -629,3 +550,107 @@ class _ProfileScreenState extends State<ProfileScreen> {
);
}
}
class ConfirmationDialog extends StatelessWidget {
const ConfirmationDialog({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Are you sure you want to save this page?",
style: const TextStyle(
fontSize: 14,
color: Color(0xFF898C81),
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
),
textAlign: TextAlign.center,
),
SizedBox(height: 8),
Text.rich(
TextSpan(
text: "Once saved, you will not be able to change your ",
style: TextStyle(
fontSize: 14,
color: Color(0xFF898C81),
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
),
children: [
TextSpan(
text: "name",
style:
TextStyle(fontWeight: FontWeight.w700), // Bold for "name"
),
TextSpan(
text: " or ",
),
TextSpan(
text: "date of birth",
style: TextStyle(
fontWeight: FontWeight.w700), // Bold for "date of birth"
),
TextSpan(
text: ".",
),
],
),
textAlign: TextAlign.center,
)
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
contentPadding: const EdgeInsets.fromLTRB(24, 20, 24, 0),
actionsPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
OutlinedButton(
onPressed: () {
Navigator.of(context).pop(false);
},
style: OutlinedButton.styleFrom(
side: BorderSide(color: Color(0xFF92722A)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(7)),
), // Set the border color here
),
child: Text(
"Cancel",
style: TextStyle(color: Color(0xFF92722A)),
),
),
ElevatedButton(
onPressed: () => Navigator.of(context).pop(true),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF92722A), // Brown color
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text(
'Confirm',
style: TextStyle(
fontWeight: FontWeight.w500,
),
),
),
],
),
),
SizedBox(
height: 5,
)
],
);
}
}