Skip to content

CustomPainter.hitTest missing size argument #28206

@spkersten

Description

@spkersten

CustomPainter.hitTest is missing the size of the widget, making it impossible to specify custom hit test behaviour when paint uses its size argument.

Simplistic example:

class CirclePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final center = size.center(Offset.zero);
    final borderPaint = Paint()
      ..color = const Color(0xffffffff)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2;
    canvas.drawCircle(center, size.width/2, borderPaint);
  }

  @override
  bool hitTest(Offset position) {
    final size = ???;
    final center = size.center(Offset.zero);
    return (position - center).distance < 0.5;
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

A workaround would be to have paint save the size in a field, but that only works if the painter is used for a single widget only.

Is it acceptable to add a size argument to hitTest?

[✓] Flutter (Channel unknown, v1.2.2-pre.22, on Mac OS X 10.14 18A391, locale en-NL)
    • Flutter version 1.2.2-pre.22 at /Users/sander/Development/flutter
    • Framework revision dd23be3936 (9 hours ago), 2019-02-19 21:35:31 -0800
    • Engine revision f45572e95f
    • Dart version 2.1.2 (build 2.1.2-dev.0.0 c92d5ca288)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: proposalA detailed proposal for a change to Fluttercustomer: solarisf: 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