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"),
);
}
}
You can just pass an
onPressedcallback to yourBotonLoginand pass acontrollerto yourCamposUsuario. Then the parent widget will use the passedTextEditingControllerto get the value ofCamposeUsuario.Sample...