Skip to content

HitTestBehavior.opaque documentation is wrong #74733

@nt4f04uNd

Description

@nt4f04uNd

docs say

/// How to behave during hit tests.
enum HitTestBehavior {
/// Targets that defer to their children receive events within their bounds
/// only if one of their children is hit by the hit test.
deferToChild,
/// Opaque targets can be hit by hit tests, causing them to both receive
/// events within their bounds and prevent targets visually behind them from
/// also receiving events.
opaque,
/// Translucent targets both receive events within their bounds and permit
/// targets visually behind them to also receive events.
translucent,
}

but the actual behavior seems to be like what @Hixie said in here
#18450 (comment)

opaque just means the entire size of the gesture detector is the hit region, it doesn't stop the child from being hit. To block hits on children use an IgnorePointer or AbsorbPointer.

upd: this differs from #18450 as mine does not propose any additional explanations, but rather solely states that the current docs are wrong

reproducible sample
import 'package:flutter/material.dart';

void main() => runApp(
      MaterialApp(
        home: Home(),
      ),
    );

class Home extends StatelessWidget {
  const Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          print(1);
        },
        child: Container(
          color: Colors.red,
          width: 50,
          height: 50,
          child: Align(
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                print(2);
              },
              child: Container(
                width: 25,
                height: 25,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

logs

2
2
2
2
2
2
2
2
2
2

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listd: api docsIssues with https://api.flutter.dev/f: gesturesflutter/packages/flutter/gestures repository.frameworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions