I am making a login page with a inkwell that navigate to forget password page. without adding the code of navigation, It works well. Once I put the navigation code, an error appear " setState() or markNeedsBuild() called during build." and then the next page appear without tapping the button.
this is the start of Login page:
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String email = "", password = "", passwordErrorText, emailErrorText;
FocusNode emailFocus, passwordFocus;
@override
void initState() {
super.initState();
emailFocus = FocusNode();
passwordFocus = FocusNode();
passwordErrorText = null;
emailErrorText = null;
emailFocus.addListener(() {
if (!emailFocus.hasFocus) {
if (!TextFieldValidator().emailValidator.isValid(email.toLowerCase())) {
setState(() {
emailErrorText = TextFieldValidator().emailValidator.errorText;
});
} else {
setState(() {
emailErrorText = null;
});
}
}
});
}
and this is the button:
Padding(
padding: EdgeInsets.only(top: 20),
child: InkWell(
child: Text(
forgetPassText,
style: TextStyle(color: Colors.blue),
),
onTap: Router.goToForgetPassword(context),
))
])),
and this is the function that navigate
static goToForgetPassword(BuildContext context) {
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) =>
ForgetPasswordPage(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return child;
},
));
}