import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/Project/ProfilePage/EditProfilePage/edit_profile_page.dart';
import '../../utils/colors.dart';
import '../widgets/appbar_widget.dart';
class EditEmailFormPage extends StatefulWidget {
const EditEmailFormPage({Key? key}) : super(key: key);
@override
EditEmailFormPageState createState() {
return EditEmailFormPageState();
}
}
class EditEmailFormPageState extends State<EditEmailFormPage> {
final _formKey = GlobalKey<FormState>();
final CollectionReference usersCollection =
FirebaseFirestore.instance.collection('users');
final emailController = TextEditingController();
bool isValid = true;
@override
void initState() {
super.initState();
emailController.addListener(() {});
}
@override
void dispose() {
emailController.dispose();
super.dispose();
}
void clearForm() {
emailController.clear();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppBar(context),
body: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
const SizedBox(
width: 320,
child: Text(
"What's your new email?",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
textAlign: TextAlign.left,
)),
Padding(
padding: const EdgeInsets.only(top: 40),
child: SizedBox(
height: 100,
width: 320,
child: TextFormField(
keyboardType: TextInputType.emailAddress,
textCapitalization: TextCapitalization.words,
// Handles Form Validation
validator: (value) {
if (value!.isEmpty) {
return 'Please enter an email address';
} else if (!RegExp(r"^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$")
.hasMatch(value)) {
return 'Please enter a valid email address';
}
return null;
},
decoration:
const InputDecoration(labelText: 'Your email address'),
controller: emailController,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 150),
child: Align(
alignment: Alignment.bottomCenter,
child: SizedBox(
width: 320,
height: 50,
child: ElevatedButton(
onPressed: () async {
setState(() {
isValid = _formKey.currentState!.validate();
});
if (isValid) {
// Get the user ID of the current user
final String? uid =
FirebaseAuth.instance.currentUser?.uid;
if (uid != null) {
// Get the DocumentReference of the user's document
final DocumentReference userDocRef =
usersCollection.doc(uid);
final String email = emailController.text.trim();
// Update the email in Firebase Authentication
await FirebaseAuth.instance.currentUser!
.updateEmail(email);
// Update the email in Firestore
await userDocRef.update({'email': email});
// ignore: use_build_context_synchronously
Navigator.push(
context,
MaterialPageRoute(
builder: ((context) =>
const EditProfilePage())));
}
} else {
// Display an error message
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Error updating email address'),
),
);
}
},style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(kGreenColor),
),
child: const Text('Save Changes'),
),
),
),
),
],
),
),
);
}
}