0

我想知道如何提交将打印前一页以及当前页面中的用户数据的表单?当我尝试更新 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),)
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
4

1 回答 1

1

您可以使用验证当前页面本身中的表单,而不是直接转到下一页

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

于 2020-08-03T11:31:27.147 回答