I am trying to validate the values of password field and confirm password field. how to validate entered password and confirm password are same in flutter.
The edited text value could not be taken as value so couldn't find the solution.
import 'package:flutter/material.dart';
import 'package:form_field_validator/form_field_validator.dart';
import 'package:loginform_validation_demo/LoginFormWithValidation.dart';
import 'validation.dart';
import 'HomePage.dart';
class SignupPage extends StatelessWidget {
GlobalKey<FormState> formkey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0,
brightness: Brightness.light,
backgroundColor: Colors.white,
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.arrow_back_ios,
size: 20,
color: Colors.black,
)),
),
body: SafeArea(
child: Form(
autovalidateMode: AutovalidateMode.always,
key: formkey,
child: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(
height: 40,
),
Text(
"Sign up",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 20,
),
Text(
"Create an Account,Its free",
style: TextStyle(
fontSize: 15,
color: Colors.grey[700],
),
),
SizedBox(
height: 30,
)
],
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 40),
child: Column(
children: [
usernameMakeInput(label: "User Name"),
passwordMakeInput(label: "Password", obsureText: true),
passwordMakeInput(label: "Confirm Password", obsureText: true)
],
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 40),
child: Container(
height: 50,
width: 200,
child: MaterialButton(
minWidth: double.infinity,
height: 50,
onPressed: () {
if (formkey.currentState.validate()) {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => HomePage()));
print("Validated");
} else {
print("Not Validated");
}
},
color: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: Text(
"Sign Up",
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18,
color: Colors.white),
),
),
),
),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Already have an account?",
style: TextStyle(fontSize: 15),
),
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LoginFormValidation()),
);
},
child: Text(
'Login',
style:
TextStyle(color: Colors.blue, fontSize: 18),
),
),
],
)
],
),
],
),
),
),
),
),
);
}
}
validation:
This is the part where I try to pass the edit text value
Widget passwordMakeInput({label, obsureText = false}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: TextStyle(
fontSize: 15, fontWeight: FontWeight.w400, color: Colors.black87),
),
SizedBox(
height: 5,
),
TextFormField(
onChanged: (value){
},
maxLength: 6,
obscureText: obsureText,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
border: OutlineInputBorder(),
),
validator: MultiValidator([
RequiredValidator(errorText: "* Required"),
MinLengthValidator(6,
errorText: "Password should contain 6 characters"),
])),
SizedBox(
height: 30,
)
],
);
}