Search code examples
flutternavigationreset-password

Flutter: Reset Password takes me to Home Page instead of back to Login Page


I am new to Flutter, when I press Submit on Reset Password a reset email is sent however I am navigated to my Home Page instead of back to my Login Page. What am I doing wrong.

Code is below:

import 'package:flutter/material.dart';
import 'stacked_icons.dart';
import 'auth.dart';

class LoginPage extends StatefulWidget {
LoginPage({this.auth, this.onSignedIn});
final BaseAuth auth;
final VoidCallback onSignedIn;

@override
State<StatefulWidget> createState() => _LoginPage();
}

enum FormType {
login,
register,
reset
}

class _LoginPage extends State<LoginPage> {
final formKey = new GlobalKey<FormState>();

String _email;
String _password;
String _name;
FormType _formType = FormType.login;


bool validateAndSave() {
final form = formKey.currentState;
if (form.validate()) {
  form.save();
  return true;
}
return false;
}

void validateAndSubmit() async {
if (validateAndSave()) {
  try {
    if (_formType == FormType.login) {
      String userId = await widget.auth.singInWithEmailAndPassword(_email, _password);
    print('Signed in: $userId');
    } else if (_formType == FormType.reset){
      await widget.auth.sendPasswordResetEmail(_email);
      print("Password reset email sent");
      //Navigator.of(context).pushReplacementNamed ('moveToReset');
      setState(() {
       _formType = FormType.login; 
      }); 
    } else if (_formType == FormType.register){
      String userId = await widget.auth.createUserWithEmailAndPassword(_email, _password, _name);
    print('Registered user: $userId');
    setState(() {
     _formType = FormType.login; 
    });
    }
    widget.onSignedIn();
  } catch (e) {
    print('Error: $e');
    showDialog(
      context: context,
      builder: (context){
        return AlertDialog(
          title: Text('Sign in failed'),
        content: Text(e.toString()),
        actions: [
          FlatButton(
            child: Text('OK'),
            onPressed: () => Navigator.of(context).pop(),
          ),  
        ],
        );
      }
    );
  }
}
}

void moveToRegister(){
formKey.currentState.reset();
setState(() {
_formType = FormType.register;  
});
}

void moveToLogin(){
formKey.currentState.reset();
setState(() {
_formType = FormType.login;  
});
}

void moveToReset(){
formKey.currentState.reset();
setState(() {
_formType = FormType.reset;  
});
}

Here is snippet of the Submit Button

 else if (_formType == FormType.reset){
  return [
  new Row(
            children: <Widget>[
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.only(
                      left: 20.0, right: 20.0, top: 10.0),
                  child: GestureDetector(
                    onTap: () {
                      validateAndSubmit();
                    },
                    child: new Container(
                      alignment: Alignment.center,
                      height: 60.0,
                      decoration: new BoxDecoration(
                          color: Color(0xFF18D191),
                          borderRadius: BorderRadius.circular(10.0)),
                      child: new Text(
                        "Submit",
                        style: new TextStyle(
                            fontSize: 20.0, color: Colors.white),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),

Please send correct code to navigate back to login after reset.

I have tried the Navigator.pushReplacementNamed however I do not know how to implement the String.

I have also tried the Navigator.pop and I get and error message when I press my Submit Button.

My assumption was that the setState would do the job but I am seeing that its not working or maybe I did not put it in correctly.

As indicated above I am still new to Flutter and am trying to figure out where I am going wrong.


Solution

  • You don't handle your reset case properly according to your use case. There is out-commented navigation code which would navigate you to a moveToReset Page.

    //Navigator.of(context).pushReplacementNamed ('moveToReset');
    

    I suggest using your moveToLogin() method and change it's logic to include a navigation to the actual login page. It's method name is misleading to the current logic it contains.

    https://flutter.dev/docs/cookbook/navigation/named-routes

    Possible solutions:

    1. Define a ResetPage Widget and a route to it. Add the following to your validateOnSubmit in the else if(_formType == FormType.reset)

      Navigator.pushNamed(context, '/yourResetPageRoute');

    This way you'll use the Router and a new Page.

    1. Another option to simply display the reset form ist your started apporach in the second code snippet. There you return a Widget (Row) to which you can add a Reset Form. This is no separate page and doesn't use routing.