skip to Main Content

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


  1. You are forgetting the parenthesis when calling the function. Replace this.funcion; with this.funcion();.

    Login or Signup to reply.
  2. Adding () to the function call will fix it. add this.funcion()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search