2

Creating an example to learn callbacks in Flutter. It's a simple programme to increase the counter onTap of a GestureDetetor But, the callback method is not working. The count increase on hot reload but not on tap. Below is the code with comments.

class BoxState extends State<ChangeBoxState>{
  int _counter = 0;

  //Callback method changes the state onTap of GestureDetector widget. It is not calling onTap.
  increaseCount(){
    setState(() {
      ++_counter;
      print(_counter);
    });
  }

  @override
  Widget build(BuildContext context) {
    // Passing the callback method,"increaseCount()" to stateless class where GestureDetector is defined.
    return BoxWidget(onPressed: increaseCount(), counter: _counter,);
  }

}

Stateless class:

class BoxWidget extends StatelessWidget{

  BoxWidget({this.onPressed, this.counter});

  final VoidCallback onPressed;
  final int counter;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      decoration: BoxDecoration(color: Colors.blue[500]),
      child: Column(
        children: <Widget>[Center(child: Text('Hello, world!')),
        GestureDetector(
          onTap: onPressed, //Passing onPressed to onTap.

          child: Container(
            margin: const EdgeInsets.only(left:0.0, top:200.0, right:0.0, bottom:0.0),
            height: 200.0,
            width: 200.0,
            decoration: BoxDecoration(
              color: Colors.teal[200],
              border: Border.all(color: Colors.yellow, width: 10.0, style: BorderStyle.solid),
              borderRadius: BorderRadius.all(Radius.circular(20.0)),
            ),
            child: Center(child: Text(counter.toString())),
          ),
        ),
        ],
      ),
    );
  }
}

2 Answers 2

6

remove the the bracket in increaseCount() because using the bracket you are creating an instance of your VoidCallback and this will run one time only so try this

return BoxWidget(onPressed: increaseCount, counter: _counter,);
4
  • Tahnks @Raouf, It would good if you could elaborate the difference a little. Please! Commented Jul 13, 2018 at 10:50
  • @PorasBhardwaj what do you want exactly to know ? Commented Jul 13, 2018 at 11:06
  • @RaoufRahiche I wanted to know what happened when we pass increaseCount() or increaseCount. Natwar elobrate a little in his answer. Please share If you have more info. Commented Jul 13, 2018 at 11:38
  • Hi @RaoufRahiche, I just had exactly same issue. I don't really understand, why it works without (). Could you please explain more in depth about it? Commented Sep 5, 2019 at 15:36
1

You should provide the reference of increaseCount to the onPressed callback.

Here you are assigning increaseCount() (check braces) to the callback which first call increaseCount() function and its return value will be assigned to the onPressed. Thats why it is only incrementing one time on hot reload.

return BoxWidget(onPressed: increaseCount, counter: _counter,);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.