I have a widget called BotonEnviar used to launch custom functions when tapped:
class BotonEnviar extends StatelessWidget {
BotonEnviar({Key? key, required this.funcion, required this.texto}) : super(key: key);
final void Function() funcion;
final String texto;
@override
Widget build(BuildContext context) {
return Container(
height: 40,
width: 180,
child: ElevatedButton(
onPressed: () {
print("pulsado enviar visto en el widget del boton");
this.funcion;
},
child: Text(
'${this.texto}' ,
style: TextStyle(color: Colors.white,fontSize: 18),
),
),
);
}
}
And here you have a class that uses BotonEnviar
:
...
SizedBox(
height: 15,
),
BotonEnviar(funcion: () {
print("pulsado boton");
PublicarTema().publicarTema(temaController.text);
}, texto: 'Enviar',
),
...
In this case, what I need is to execute PublicarTema().publicarTema(temaController.text);
when BotonEnviar is clicked.
Here you have PublicarTema()
class:
class PublicarTema {
publicarTema(String titulo){
//accion al pulsar boton ENVIAR
print("itulo recibido ${titulo}");
}
}
I guess I am not implementing anything as I should. What I want is to keep a clean code in my app.
What I am getting after clicking on BotonEnviar is only the output from:
print("pulsado enviar visto en el widget del boton");
and not the output from
print("itulo recibido ${titulo}");
What should I change?
2
Answers
You are forgetting the parenthesis when calling the function. Replace
this.funcion;
withthis.funcion();
.Adding () to the function call will fix it. add
this.funcion()