import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_application_1/Project/ProfilePage/EditProfilePage/edit_profile_page.dart';
import '../widgets/appbar_widget.dart';
const Color cColor = Color.fromARGB(255, 58, 168, 50);
class ChangePasswordPage extends StatefulWidget {
const ChangePasswordPage({Key? key}) : super(key: key);
@override
_ChangePasswordPageState createState() => _ChangePasswordPageState();
}
class _ChangePasswordPageState extends State<ChangePasswordPage> {
final _formKey = GlobalKey<FormState>();
final _oldPasswordController = TextEditingController();
final _newPasswordController = TextEditingController();
final _confirmPasswordController = TextEditingController();
bool _isValid = false;
bool _isLoading = false;
String _errorMessage = '';
bool _obscureTextCurrent = true;
bool _obscureTextNew = true;
bool _obscureTextConfirm = true;
@override
void dispose() {
_oldPasswordController.dispose();
_newPasswordController.dispose();
_confirmPasswordController.dispose();
super.dispose();
}
void _onSubmit() async {
setState(() {
_isLoading = true;
});
try {
// Verify that the current password is correct
final currentUser = FirebaseAuth.instance.currentUser;
final credential = EmailAuthProvider.credential(
email: currentUser!.email!,
password: _oldPasswordController.text,
);
await currentUser.reauthenticateWithCredential(credential);
// Update the user's password
await currentUser.updatePassword(_newPasswordController.text);
setState(() {
_isLoading = false;
_errorMessage = '';
});
// Navigate back to the previous screen
// ignore: use_build_context_synchronously
Navigator.push(context,
MaterialPageRoute(builder: ((context) => const EditProfilePage())));
} on FirebaseAuthException catch (e) {
setState(() {
_isLoading = false;
_errorMessage = e.message ?? 'An error occurred';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppBar(context),
body: Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
const SizedBox(
width: 320,
child: Text(
"Change your password:",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
textAlign: TextAlign.left,
),
),
Padding(
padding: const EdgeInsets.only(top: 40),
child: TextFormField(
controller: _oldPasswordController,
textInputAction: TextInputAction.next,
obscureText: _obscureTextCurrent,
decoration: InputDecoration(
labelText: 'Current Password',
suffixIcon: IconButton(
icon: Icon(
_obscureTextCurrent
? Icons.visibility
: Icons.visibility_off,
color: Colors.grey,
),
onPressed: () {
setState(() {
_obscureTextCurrent = !_obscureTextCurrent;
});
},
),
),
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your current password.';
}
if (value.length < 8) {
return 'Password must be at least 8 characters long.';
}
return null;
},
),
),
const SizedBox(height: 16.0),
TextFormField(
controller: _newPasswordController,
textInputAction: TextInputAction.next,
obscureText: _obscureTextNew,
decoration: InputDecoration(
labelText: 'New Password',
suffixIcon: IconButton(
icon: Icon(
_obscureTextNew ? Icons.visibility : Icons.visibility_off,
color: Colors.grey,
),
onPressed: () {
setState(() {
_obscureTextNew = !_obscureTextNew;
});
},
),
),
validator: (value) {
if (value!.isEmpty) {
return 'Please enter a new password.';
}
if (value.length < 8) {
return 'Password must be at least 8 characters long.';
}
return null;
},
),
const SizedBox(height: 16.0),
TextFormField(
controller: _confirmPasswordController,
textInputAction: TextInputAction.done,
obscureText: _obscureTextConfirm,
decoration: InputDecoration(
labelText: 'Confirm New Password',
suffixIcon: IconButton(
icon: Icon(
_obscureTextConfirm
? Icons.visibility
: Icons.visibility_off,
color: Colors.grey,
),
onPressed: () {
setState(() {
_obscureTextConfirm = !_obscureTextConfirm;
});
},
),
),
validator: (value) {
if (value!.isEmpty) {
return 'Please confirm new password.';
}
if (value != _newPasswordController.text) {
return 'New password and confirmation do not match.';
}
return null;
},
),
if (_errorMessage.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Text(
_errorMessage,
style: const TextStyle(
color: Colors.red,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 120),
child: Align(
alignment: Alignment.bottomCenter,
child: SizedBox(
width: 320,
height: 50,
child: ElevatedButton(
onPressed: _isLoading
? null
: () {
setState(() {
_isValid = _formKey.currentState!.validate();
});
if (_isValid) {
_onSubmit();
}
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(cColor),
),
child: _isLoading
? const CircularProgressIndicator()
: const Text(
'Change Password',
style: TextStyle(fontSize: 15),
),
),
),
),
),
],
),
),
),
);
}
}