mohammed nabil
mohammed nabil

Reputation: 107

How to ontap screen listener in flutter

How to on tap screen listener in flutter which increments the number on the screen.I try this code which is below and it increments the number but by tapping at the middle of the screen,what is the code so that where i tap anywhere on the screen and it increment by 1.

class _MyHomePageState extends State<MyHomePage> {
  int counter=0;
  void _increment(){
    setState(() {
      counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Flutter Counter"),
      ),
      body: GestureDetector(
        onTap: () {
          setState(() { _increment(); });
        },
        child: Center(
          child: Text('$counter'),
        ),
      )

Upvotes: 0

Views: 3111

Answers (1)

Mazin Ibrahim
Mazin Ibrahim

Reputation: 7869

Use a Container for your body segment wrapped in an InkWell Widget:

 body: InkWell(
 onTap: () {
      setState(() { _increment();});
    },
     child: Container(
         width: MediaQuery.of(context).size.width,
         height: MediaQuery.of(context).size.height,
     child: Center(
        child: Text('$counter'),
     ),
   ),
 ),

Upvotes: 1

Related Questions