Search code examples
fluttergesturedetectoriconbutton

How to prevent clicking on GestureDetector child Flutter


I'm having a button inside a GestureDetector. Both the GestureDetector and the IconButton have their own function. However, I just want to execute the function of GestureDetector instead of ```IconButton`` when tapping on the icon. I know some solutions for this proble, but I still wonder in the case above, is there any solution that prevent flutter from executing the IconButton's function?

Thanks


Solution

  • Tap priority child> GesuterDetector. If you have a single child as IconButton on GestureDetector only IconButton will work.

    Let's say you have a column instead.

    • you can pass null on onPressed of IconButton based on condition.
    • using AbsorbPointer will prevent tap event of its child, and GestureDetector tap event will work in this case.
    • IgnorePointer will totally ignore any tap event in its area.

    Demo Widget

    
    
    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget();
      // final String title;
    
      @override
      State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      bool _disableIconButton = false;
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          backgroundColor: Colors.white,
          body: Container(
            child: Column(
              children: [
                GestureDetector(
                  onTap: () {
                    print("GestureDetector Tapped");
                  },
                  child: IconButton(
                    onPressed: () {
                      print(" only Icon will be working here");
                    },
                    icon: Icon(Icons.ac_unit),
                  ),
                ),
                SizedBox(
                  height: 100,
                ),
                GestureDetector(
                  onTap: () {
                    print("GestureDetector Tapped");
                  },
                  child: Column(
                    children: [
                      Text("Inside Column"),
                      Switch(
                        value: _disableIconButton,
                        onChanged: (v) {
                          setState(() {
                            _disableIconButton = v;
                          });
                        },
                      ),
    
                      ///* Colors will faded on
                      IconButton(
                        onPressed: _disableIconButton
                            ? null
                            : () {
                                print("Icon null checker tapped");
                              },
                        icon: Icon(Icons.ac_unit),
                      ),
    
                      ///* Colors will faded on like disable and will work on GuesterTap
                      AbsorbPointer(
                        absorbing: _disableIconButton,
                        child: IconButton(
                          onPressed: _disableIconButton
                              ? null
                              : () {
                                  print("Icon AbsorbPointer tapped");
                                },
                          icon: Icon(Icons.ac_unit),
                        ),
                      ),
    
                      ///* it will ignore tap event
                      IgnorePointer(
                        ignoring: _disableIconButton,
                        child: IconButton(
                          onPressed: _disableIconButton
                              ? null
                              : () {
                                  print("Icon IgnorePointer tapped");
                                },
                          icon: Icon(Icons.ac_unit),
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        );
      }
    }