Here's the Button code
ButtonWidget(
text: 'Submit',
onClicked: () {
Navigator.pushAndRemoveUntil(
context, MaterialPageRoute(builder: (_) => SPAL()),
(Route<dynamic> route) => false,
);
print('Email: ${emailController.text}');
print('Password: ${password}');
},
),
How i can make the button check if the email and password is empty or not, if it is then button wont open next page but if it was filled then the button will open the next Screen
Solution 1: flutter_bee
you need to add a validator in the email and password field, then when the user clicks on the button you can check for validation. the email and password field needs to be put inside a form with a key
final formKey = GlobalKey<FormState>();
final anotherKey= GlobalKey<FormState>();
Form(
key: formKey,
child: Column(
children: <Widget>[
emailField(),
passwordField(),
ButtonWidget(),
]
),
),
Widget emailField(String value) {
return TextFormField(
//using controller
controller: emailController,
decoration: InputDecoration(
labelText: 'Email',
hintText: 'Enter Email',
validator: (value) => value == null || value.isEmpty ? "enter your email" : null,
//don't need onSaved when using controller
//onSaved: (String value) {
// emailController.text = value.toLowerCase().trim();
//},
);
}
ButtonWidget(
text: 'Submit',
onClicked: () {
if(formKey.currentState.validate() && anotherKey.currenState.validate()) {
formKey.currentState.save();
anotherKey.currentState.save();
Navigator.pushAndRemoveUntil(
context, MaterialPageRoute(builder: (_) => SPAL()),
(Route<dynamic> route) => false,
);
}
print('Email: ${emailController.text}');
print('Password: ${password}');
},
),