Reputation: 240
I just learned Flutter. Here I want to ask how to match passwords and confirm passwords. Here I will give my code. I also use TextField. and I don't use a validator here to validate
TextField(
key: passkey,
style: new TextStyle(color: Colors.white),
controller: password,
decoration: InputDecoration(
labelText: 'Password',
labelStyle: TextStyle(color: Colors.white),
hintStyle: TextStyle(color: Colors.white),
icon: const Padding(
padding: const EdgeInsets.only(top: 15.0),
child: const Icon(
Icons.lock_outline,
color: Colors.white,
)
),
errorText: validate ? 'Password Can\'t Be Empty' : null,
),
obscureText: _obscureText,
),
TextField(
style: new TextStyle(color: Colors.white),
controller: confirmpassword,
decoration: InputDecoration(
labelText: 'Retype Password',
labelStyle: TextStyle(color: Colors.white),
hintStyle: TextStyle(color: Colors.white),
icon: const Padding(
padding: const EdgeInsets.only(top: 15.0),
child: const Icon(
Icons.lock_outline,
color: Colors.white,
)),
// errorText:
// validate ? 'Password Can\'t Be Empty' : null,
),
obscureText: _obscureText,
),
Upvotes: 18
Views: 49539
Reputation: 19
A simpler way to create a confirm password on your flutter. 'showSnackBar' bottom pop-up to alert users for errors. [1]: https://i.sstatic.net/zjXLd.png
var _password = '';
var _confirmPassword = '';
// this will called 'onTap'
void signUpUser() async {
if (_password != _confirmPassword){ // you can add your statements here
showSnackBar(context,
"Password does not match. Please re-type again.");
} else {
FirebaseAuthMethods(FirebaseAuth.instance).signUpWithEmail(
email: emailController.text,
password: passwordController.text,
context: context,
);
}
}
// Insert inside your build()
// to you password field.
child: TextFormField(
controller: passwordController,
obscureText: true,
onChanged: (value) {
_password = value;
},
// to your confirm password field.
child: TextFormField(
controller: confirmPasswordController,
obscureText: true,
onChanged: (value) {
_confirmPassword = value;
},
// add this to your Sign-up button
onTap: signUpUser,
Upvotes: 0
Reputation: 169
This is dart validations for forms you can use as below
String _password = '';
String _confirmPassword = '';
final _formKey = GlobalKey<FormState>();
Form(
key: _formKey,
child: Column(
children: [
const SizedBox(
height: 30,
),
TextFormField(
decoration: const InputDecoration(
hintText: "Password",
),
onChanged: (value){
_password = value;
},
validator: (value) {
if (value != null && value.isEmpty) {
return 'Password is required please enter';
}
// you can check password length and specifications
return null;
}
),
const SizedBox(
height: 30,
),
TextFormField(
decoration: const InputDecoration(
hintText: "Confirm Password",
),
onChanged: (value){
_confirmPassword = value;
},
validator: (value) {
if (value != null && value.isEmpty) {
return 'Conform password is required please enter';
}
if(value != _password){
return 'Confirm password not matching';
}
return null;
}
),
const SizedBox(
height: 40,
),
ButtonWidget(
btnText: "Create an Account",
onButtonPres: () => {
// validate all forms fields
if (_formKey.currentState!.validate()) {
// do the API call here
}
},
),
],
),
),
Upvotes: 1
Reputation: 8560
I did a validation using RxDart and Streams. Although it is a little bit more work I guarantee that the final results improve the performance and also the UX. Check it out on medium
Upvotes: 1
Reputation: 61
Just simply declare a variable first then assign it's value to the value which we are using.
Then compare it down as shown below:
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
var confirmPass;
TextFormField(
validator: (String value) {
confirmPass = value;
if (value.isEmpty) {
return "Please Enter New Password";
} else if (value.length < 8) {
return "Password must be atleast 8 characters long";
} else {
return null;
}
},
decoration: InputDecoration(
hintText: "Enter New Password",
hintStyle: TextStyle(color: Colors.grey),
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(40.0),
),
),
),
),
),
SizedBox(height: 20),
Container(
child: TextFormField(
validator: (String value) {
if (value.isEmpty) {
return "Please Re-Enter New Password";
} else if (value.length < 8) {
return "Password must be atleast 8 characters long";
} else if (value != confirmPass) {
return "Password must be same as above";
} else {
return null;
}
},
decoration: InputDecoration(
hintText: "Re-Enter New Password",
hintStyle: TextStyle(color: Colors.grey),
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(40.0),
),
),
),
),
),
Upvotes: 5
Reputation: 3263
Use TextFormField widget which consists of a builtin validator
// Form
final GlobalKey<FormState> _form = GlobalKey<FormState>();
final TextEditingController _pass = TextEditingController();
final TextEditingController _confirmPass = TextEditingController();
Form(
key: _form,
child: ListView(
children: <Widget>[
TextFormField(
controller: _pass,
validator: (val){
if(val.isEmpty)
return 'Empty';
return null;
}
),
TextFormField(
controller: _confirmPass,
validator: (val){
if(val.isEmpty)
return 'Empty';
if(val != _pass.text)
return 'Not Match'
return null;
}
),
]
)
)
// To validate call
_form.currentState.validate()
Upvotes: 49