Home > Software design >  How do I save or send information from 2 forms to a single class in fluttter
How do I save or send information from 2 forms to a single class in fluttter

Time:09-07

I have an inconvenience. I have two different classes, one for each form with its respective text box and I want to store the information in the class called CustomerRegistrationModel without the need to create a CustomerRegistrationModel object in each class.

The idea is to save the information of the 2 forms in RegistroClienteModel and then send that to the database

registrousuario11 and registrousuario22 are similar.

code and image::

class RegistroClienteModel{



 String contrasena ;
  String get Contrasena => contrasena;

  String correo ;
  String get Correo => correo;

  set setContrasena(String usucon) {
    contrasena = usucon;
  }

  set setCorreo(String corre) {
    this.correo = corre;
  }


String apodo;
  String get Apodo => apodo;

set setApodo (String usuapodo) {
    this.apodo = usuapodo;
}

String apellido;
  String get Apellido => apellido;

set setApellido (String usuapellido) {
    this.apellido= usuapellido;


int edad;
 int  get Edad => edad;

set setApellido (int usuedad) {
    this.edad = usuedad;


}

class registrousuario11 extends State<>{
Widget ContrasenaF2(Contrasena){
  return new Container(
      child: new TextFormField(
        // validator: (value)=>value.length < 6?"Contrasena muy corta": null,
        controller:Contrasena,
        obscureText: true,
        decoration: const InputDecoration(
          icon: Icon(Icons.lock),
      //    hintText: 'Ej:123xxx',
          labelText: 'Domicilio',
        ),
      )
  );
}

Widget CorreoF2(Correo){
  return new Container(
      child: new TextFormField(
        controller: Correo,
        // validator: (value)=>!value.contains("@") && value.contains("mail")?"Correo Invalido":null, //para validar si el correo esta correoto
        decoration: const InputDecoration(
          icon: Icon(Icons.email),
          // hintText: 'Ej:[email protected]',
          labelText: 'Apodo',
        ),
      )
  );
}

Widget  ConfirmarContrasenaF2(ConfirmarContrasena){
  return new Container(
      child: new TextFormField(
        controller: ConfirmarContrasena,
        //obscureText: true,
        decoration: const InputDecoration(
          icon: Icon(Icons.check),
        //  hintText: 'Validar su contraseña',
          labelText: 'Edad',
        ),
      )
  );
}

@override
  Widget build(BuildContext context) {
    return new Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: new AppBar(
          title: new Text("Formulario2"),
          centerTitle: true,
          automaticallyImplyLeading: false,
        ),
        body: Center(
          child: new Container(
            //padding: EdgeInsets.all(20.0),
            padding: EdgeInsets.only(
                left: 10,right: 10,// Para que el teclado no este sobre el texto
                bottom: MediaQuery.of(context).viewInsets.bottom),
            // padding: const EdgeInsets.all(20.0), // Margen a todos los componentes(textfield,botones etc...)
            child: new ListView(

                children:[
                  SizedBox(height: 8,),
                
                  SizedBox(height: 28,),
                  CorreoF2(Correo),
                  SizedBox(height: 28,),
                  ContrasenaF2(Contrasena),
                  SizedBox(height: 28,),
                  ConfirmarContrasenaF2(ConfirmarContrasena),
                 
                 
                ]
            ),
          ),
        )
    );
    throw UnimplementedError();
  }
}
}

class registrousuario22 extends State<>{}

enter image description here

enter image description here

CodePudding user response:

You can pass data from one class to another using with constructor.

CodePudding user response:

first
you can implement that suing state management as provider
get started with provider

you can store class object on provider and make them available on widget tree

or you can use this package to create form reactive form

it's contain Good docs to get started here example show you how to start

final form = FormGroup({
  'first': FormControl(validators: [Validators.required]),
  'second': FormControl(),
  'third': FormControl(),
  });

this how to use it . very similar to TextField

    class Example extends StatelessWidget {

  const Example({Key? key}) : super(key: key);



  @override

  Widget build(BuildContext context) {

    return ReactiveForm(
      formGroup: form,
      child: TextField(
        formControlName: 'first',
        validationMessages: {}, //here can add message to show user if feailed required
      ),
    );

  }

}

and now you can create many form and access to information to any form

hope this help you to create forms

  • Related