Как проверить и обновить данные с предыдущей страницы во флаттере? - PullRequest
0 голосов
/ 03 августа 2020

Я хотел знать, как я могу отправить форму, которая будет печатать пользовательские данные с предыдущей страницы, а также с текущей страницы, на которой они находятся? Это поможет мне в дальнейшем, когда я пытаюсь обновить данные пользователя в firestore. Я не мог найти решение, поэтому был бы очень признателен за любую помощь, которую могу получить! :)

редактировать: Для тех, кто не понял того, что я пытался сказать

У меня есть текстовые поля Имя, Фамилия и возраст на одной странице, для которой пользователь нажимает на следующей кнопке, когда закончите ввод. На последней странице регистрации, состоящей из текстовых полей электронной почты и пароля, находится кнопка «Зарегистрироваться», которая зарегистрирует пользователя. Я хочу нажать «Регистрация» на последней странице регистрации, которая распечатает данные, введенные на предыдущей странице, а также на текущей странице. Я хотел знать, как я могу этого добиться, чтобы позже я мог обновить пользовательские данные на firestore

предыдущая страница

class UserDetails extends StatefulWidget {
  @override
  _UserDetailsState createState() => _UserDetailsState();
}

enum Gender{
  Male, Female, Others
}

class _UserDetailsState extends State<UserDetails> {
  String userFirstName;
  String userLastName;
  String user_age;
  int group_value = -1;
  Gender _gender = Gender.Male;
   final formkeyDetails = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    //final _userProvider = Provider.of<UserProvider>(context);

    final _firstName = Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: TextFormField(
        autofocus: false,
        keyboardType: TextInputType.text,
        /*onChanged: (value){
          _userProvider.changeFirstName(value);
        },

         */
        validator: (value) {
          if(value.isEmpty)
          {
            return 'Field cannot be empty';
          }
          return null;
        },
        onSaved: (value)=> userFirstName = value,
        decoration: InputDecoration(
          hintText: 'Enter First Name',
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10)
          ),
        ),
      ),
    );

    final _lastName = Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: TextFormField(
        autofocus: false,
        keyboardType: TextInputType.text,
       /* onChanged: (value){
          _userProvider.changeLastName(value);
        }
        ,
        */
        validator: (value) {
          if(value.isEmpty)
          {
            return 'Field cannot be empty';
          }
          return null;
        },

        onSaved: (value)=> userLastName = value,
        decoration: InputDecoration(
          hintText: 'Enter Last Name',
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10)
          ),
        ),
      ),
    );





    final _userAge = Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: TextFormField(
        keyboardType: TextInputType.number,
        autofocus: false,
       /* onChanged: (value){
          _userProvider.changeAge(value);
        },

        */
        validator: (value) {
          if(value.isEmpty)
          {
            return 'Field cannot be empty';
          }
          return null;
        },
        onSaved: (value)=> user_age = value,
        decoration: InputDecoration(
          hintText: 'Enter Age',
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10)
          ),
        ),
      ),
    );




    final _male = Radio(
      value: Gender.Male,
      activeColor: Colors.black,
      groupValue: _gender,
      onChanged: (Gender value){
        setState(() {
          print(value);
          _gender = value;
        });
      },
    );


    final _female = Radio(
      activeColor: Colors.black,
    value: Gender.Female,
    groupValue: _gender,
    onChanged: (Gender value){
      setState(() {
        print(value);
        _gender = value;
      });
    },
    );

    final _others = Radio(
      activeColor: Colors.black,
        value: Gender.Others,
        groupValue: _gender,
        onChanged: (Gender value){
          setState(() {
            print(value);
            _gender = value;
          });
        },
      );


    return Scaffold(
      backgroundColor: Colors.yellow,
      body: Container(
        child: Form(
          key: formkeyDetails,
          child:  Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Register",
                style: TextStyle(fontSize: 64.0, fontWeight: FontWeight.bold),),
              SizedBox(height: 50,),
              _firstName,
              SizedBox(height: 20,),
              _lastName,
              SizedBox(height: 20,),
              _userAge,
              SizedBox(height: 30,),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center ,
                children: <Widget>[
                  Text("      Gender: ", style: TextStyle(fontSize: 20.0),),
                  _male,
                  Text("Male"),
                  _female,
                  Text("Female"),
                  _others,
                  Text("Others"),
                ],
              ),

              Row(
                crossAxisAlignment: CrossAxisAlignment.center ,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,

                children: <Widget>[
                  FloatingActionButton.extended(
                      heroTag: "prev_button",
                      backgroundColor: Colors.yellow,
                      foregroundColor: Colors.black,
                      onPressed: ()=> Navigator.push(context, MaterialPageRoute(builder: (context)=>UserLogin())),
                      label: Text("Prev", style: TextStyle(fontWeight: FontWeight.bold),)
                  ),
                  FloatingActionButton.extended(
                      heroTag: "next_button",
                      backgroundColor: Colors.yellow,
                      foregroundColor: Colors.black,
                      onPressed: ()=> Navigator.push(context, MaterialPageRoute(builder: (context)=>UserReg())),
                      label: Text("Next", style: TextStyle(fontWeight: FontWeight.bold),)
                  ),
                ],
              ),
            ],
          ),
        ),
        ),
      );

  }
}

Текущая страница (которая распечатает содержимое предыдущей страницы также)

class UserReg extends StatefulWidget {
  UserReg({this.auth});
  final BaseAuth auth;
  @override
  State<StatefulWidget> createState() => _UserRegState();
}

  class _UserRegState extends State<UserReg> {


    final formkey = GlobalKey<FormState>();

  static String emailValidator(String value) {
    Pattern pattern =
        r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
    RegExp regex = new RegExp(pattern);
    if (!regex.hasMatch(value)) {
      return 'Email format is invalid';
    } else {
      return null;
    }
  }

  static String pwdValidator(String value) {
    if (value.length <= 6) {
      return 'Password must be longer than 8 characters';
    } else {
      return null;
    }
  }

  bool _validateAndSave()
  {
    final form2 = formkey.currentState;
    final form1 = formkeyDetails.currentState;
    if(form2.validate())
      {
        form.save();
        return true;
      }
    return false;
  }

  void _validateAndSubmit() async
  {

      if(_validateAndSave()) {
        try {
          String userId = await Auth().signUp(rEmail, rPass);
          await Auth().sendEmailVerification();
          formkey.currentState.reset();
          print('Registered! $userId, sent email verification');
        }
        catch (e) {
          print('Error: $e');
        }
      }

  }

  final notValidIcon = Icon(
    Icons.error,
    color: Colors.pink,
  );

  static String rEmail;
  static String rPass;





  @override
  Widget build(BuildContext context) {
    //final userProvider = Provider.of<UserProvider>(context);

    final _regEmail = Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: TextFormField(
        keyboardType: TextInputType.emailAddress,
        autofocus: false,
        validator: (value) {
          if(value.isEmpty)
          {
            return 'Email cannot be empty';
          }
          else
            emailValidator(value);
          return null;
        },
        /*onChanged: (value){
          userProvider.changeEmail(value);
        },

         */
        onSaved: (value)=> rEmail = value,
        decoration: InputDecoration(
          hintText: 'Enter Email Address',
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10)
          ),
        ),
      ),
    );


    final _regpass = Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: TextFormField(
        obscureText: true,
        autofocus: false,
        validator: pwdValidator,
        /*onChanged: (value){
          userProvider.changePassword(value);
        },

         */
        onSaved: (value)=> rPass = value,
        decoration: InputDecoration(
          hintText: 'Enter password',
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10)
          ),
        ),
      ),
    );

    final _confPass = Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: TextFormField(
        obscureText: true,
        autofocus: false,
        validator: (value){
          if(value != rPass)
            {
              return("Password does not match");
            }
          return pwdValidator(value);
        },
        /*
        onChanged: (value){
          userProvider.changePassword(value);
        },

         */
        onSaved: (value)=> rPass = value,
        decoration: InputDecoration(
          hintText: 'Enter password',
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10)
          ),
        ),
      ),
    );

    return new Scaffold(
      resizeToAvoidBottomInset: true,
      backgroundColor: Colors.yellow,
      body: Container(
        height: MediaQuery.of(context).size.height,
        child: Form(
          key: formkey,
          child: Column(
            crossAxisAlignment:  CrossAxisAlignment.center,
            children: <Widget>[
              SizedBox(height: MediaQuery.of(context).size.height *0.2,),
              Text('Register',
                style:TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 64,
                ),
              ),
              SizedBox(height: 100,),
              _regEmail,
              SizedBox(height: 20,),
              _regpass,
              SizedBox(height:30),
              //_confRegPass,
              SizedBox(height: 30,),
              FloatingActionButton.extended(
                  heroTag: "Register_Button",
                  backgroundColor: Colors.yellow,
                  foregroundColor: Colors.black,
                  onPressed:  (){
                    _validateAndSubmit();
                   //userProvider.saveUser();
                    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=>UserLogin()));
                  },
                  label: Text("Register", style: TextStyle(fontWeight: FontWeight.bold),)
              ),

              SizedBox(height: 20,),
              FlatButton(
                child: Text('Already Registered? Sign in!'),
                onPressed: ()=> Navigator.push(context, MaterialPageRoute(builder: (context)=>UserLogin())) ,
              ),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  FloatingActionButton.extended(
                      heroTag: "prev_button1",
                      backgroundColor: Colors.yellow,
                      foregroundColor: Colors.black,
                      onPressed:  ()=>  Navigator.pop(context),  //Navigator.push(context, MaterialPageRoute(builder: (context)=>UserDetails())),
                      label: Text("Prev", style: TextStyle(fontWeight: FontWeight.bold),)
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

1 Ответ

1 голос
/ 03 августа 2020

Вместо прямого перехода на следующую страницу вы можете проверить форму на самой текущей странице, используя

onPressed: () {
    // Validate returns true if the form is valid, otherwise false.
    if (formKey.currentState.validate()) {
      //Navigation Logic
    }
  }

И если вам нужны данные с предыдущей страницы, вы можете обратиться к https://flutter.dev/docs/cookbook/navigation/passing-data

...