flutterdartflutter-layoutdart-polymer

how to access variables from other widgets


hello I would like to know how to access variables from other widgets. for example: I would like that when I touch the button of the widget BotonLogin, it prints the user variable of the widget CamposUsuario.

  class CamposUsuario extends StatelessWidget {
  final String texto;
  CamposUsuario({required this.texto});
  final usuario = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      decoration: InputDecoration(
          labelStyle: TextStyle(fontSize: 20), labelText: "$texto"),
      obscureText: (this.texto == "Password") ? true : false,
      controller: usuario,
    );
  }
}

class BotonLogin extends StatelessWidget {
  const BotonLogin({
    Key? key,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      padding: EdgeInsets.symmetric(horizontal: 60),
      elevation: 7,
      onPressed: () {
        print('si');
      },
      color: Colors.amber[900],
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
      child: Text("Login"),
    );
  }
}

Solution

  • You can just pass an onPressed callback to your BotonLogin and pass a controller to your CamposUsuario. Then the parent widget will use the passed TextEditingController to get the value of CamposeUsuario.

    Sample...

    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final TextEditingController usernameController = TextEditingController();
        // UPDATE
        final TextEditingController passwordController = TextEditingController();
    
        return Scaffold(
          body: Column(
            children: <Widget>[
              CamposUsuario(
                texto: 'Username',
                usuario: usernameController,
              ),
              // UPDATE
              CamposUsuario(
                texto: 'Password',
                usuario: passwordController,
              ),
              BotonLogin(
                onPressed: () {
                  print('Si ${usernameController.text}');
                  // UPDATE
                  print('Password: ${passwordController.text}');
                },
              ),
            ],
          ),
        );
      }
    }
    
    class CamposUsuario extends StatelessWidget {
      CamposUsuario({
        required this.texto,
        required this.usuario,
        Key? key,
      }) : super(key: key);
    
      final String texto;
      final TextEditingController usuario;
    
      @override
      Widget build(BuildContext context) {
        return TextFormField(
          decoration: InputDecoration(
            labelStyle: const TextStyle(fontSize: 20),
            labelText: texto,
          ),
          obscureText: (this.texto == "Password") ? true : false,
          controller: usuario,
        );
      }
    }
    
    class BotonLogin extends StatelessWidget {
      BotonLogin({
        required this.onPressed,
        Key? key,
      }) : super(key: key);
    
      final VoidCallback onPressed;
    
      @override
      Widget build(BuildContext context) {
        return ElevatedButton(
          onPressed: onPressed,
          child: Text("Login"),
        );
      }
    }