Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

A GestureDetector inside a Stack never fires onTap

ErrorGestureDetector onTap is never called

Short answer

A tap does nothing when the detector has nothing to hit, when another widget sits on top and takes the hit, or when a nested detector wins the gesture. Turn on debugPaintPointersEnabled to see the hit areas, then set behavior to opaque, wrap the overlay in IgnorePointer, or remove the competing detector.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Make Flutter draw the area each pointer target covers. Add this line before runApp, hot restart, and tap. If your detector has no box on screen, it has no hit area.

    import 'package:flutter/rendering.dart';
    
    void main() {
      debugPaintPointersEnabled = true;
      runApp(const MyApp());
    }
  2. 2Match what you see, then jump to its fix

    1. 1No hit box is drawn over the detector, or only over the part that has visible contentThe detector's child is empty or transparentFix
    2. 2The tap reaches nothing, or reaches the wrong layer, and the pointer debug box sits on the top widgetA widget stacked on top takes the hitFix
    3. 3The inner onTap runs and the outer onTap never doesA nested detector wins the gestureFix

What does the error look like?

  • GestureDetector onTap is never called
  • Container inside Stack does not respond to taps
  • Tap goes to the widget underneath instead of the top one

Cause 1 of 3

The detector's child is empty or transparent

1Check

By default a GestureDetector only takes hits where its child takes hits. A Container with no color and no child, or a SizedBox, gives it nothing to hit. A GestureDetector with no child at all expands to fill its parent.

GestureDetector(
  onTap: () => debugPrint('tap'),
  child: Container(width: 120, height: 120), // no color: nothing to hit
)

2Fix

Set behavior to HitTestBehavior.opaque, so the detector claims its whole box. Use translucent if widgets behind it must also get the touch.

GestureDetector(
  behavior: HitTestBehavior.opaque,
  onTap: () => debugPrint('tap'),
  child: Container(width: 120, height: 120),
)

3Verify

Tap the empty area and look for the print in the debug console.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 2 of 3

A widget stacked on top takes the hit

1Check

Children of a Stack paint in order, and the first child is at the bottom. Whatever comes later covers the earlier ones and is hit first. A full-size overlay, gradient or blur layer often sits on top of the button without being obvious.

Stack(
  children: [
    GestureDetector(onTap: () {}, child: const Text('Buy')), // bottom
    Container(color: Colors.black26),                         // covers it
  ],
)

2Fix

Wrap the decorative overlay in IgnorePointer, so it still paints but is skipped by hit testing. Or move the detector to a later child.

Stack(
  children: [
    const Text('Buy'),
    IgnorePointer(child: Container(color: Colors.black26)),
  ],
)

3Verify

Tap through the overlay area. The widget below now answers.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 3 of 3

A nested detector wins the gesture

1Check

When detectors overlap, each enters the gesture arena and only one wins. The child enters first and wins, so a parent's onTap does not run for taps on the child. Setting behavior on the parent does not change this contest.

grep -rn "GestureDetector\|InkWell" lib/ | head -30

2Fix

Keep one tap handler for that area: remove the inner detector, or move the logic into the inner one. To inspect the contest, print the arena diagnostics.

import 'package:flutter/gestures.dart';

void main() {
  debugPrintGestureArenaDiagnostics = true;
  runApp(const MyApp());
}

3Verify

Tap once and read the arena lines in the console to see which recognizer won.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

How do I stop it coming back?

  • Give any tappable area a visible size or set behavior explicitly instead of relying on the default.
  • Put purely decorative layers inside IgnorePointer when you add them to a Stack.
  • Keep debugPaintPointersEnabled in a debug-only toggle so hit areas are one switch away.
  • Use one tap handler per area.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.