spot icon indicating copy to clipboard operation
spot copied to clipboard

Support for hitTestable

Open MaxLap opened this issue 1 month ago • 1 comments

Right now, there is a work around of:

spotText('Top 1').finder.hitTestable().spot().existsOnce();

It would be nice to have a function to do that more cleanly. Even if behind the scene, it does the same thing. Example:

spotText('Top 1').hitTestable().existsOnce();

Thanks!

MaxLap avatar Nov 22 '25 14:11 MaxLap

Interesting. What is the use case you're using it for?

spot already does the hit testing excessivly, but only during act.tap().

passsy avatar Nov 22 '25 15:11 passsy

I have widgets which affect layouts, and I tent to test them using Text. For example, is my widget not currently visible on screen (because you would need to scroll to see it).

I discovered today that my work-around was super slow... I saw cases where each calls to the workaround was taking half a second.

So I wrote a little extension for this:

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:spot/spot.dart';
import 'package:spot/src/spot/selectors.dart';

extension HittestableSpot<W extends Widget> on ChainableSelectors<W> {
  WidgetSelector<W> hitTestable({Alignment alignment = Alignment.center}) {
    final selector = self!.addStage(
      PredicateFilter(
        predicate: (Element e) {
          final int viewId = e.findAncestorWidgetOfExactType<View>()!.view.viewId;
          final RenderObject? object = e.renderObject;
          if (object is! RenderBox) {
            return false;
          }
          final Offset absoluteOffset = object.localToGlobal(alignment.alongSize(object.size));
          final HitTestResult hitResult = HitTestResult();
          WidgetsBinding.instance.hitTestInView(hitResult, absoluteOffset, viewId);
          for (final HitTestEntry entry in hitResult.path) {
            if (entry.target == e.renderObject) {
              return true;
            }
          }

          return false;
        },
        description: 'HitTestable Widget',
      ),
    );
    return selector;
  }
}

MaxLap avatar Nov 23 '25 21:11 MaxLap