Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

act.tap(spotText('textSpan')) should click the exact TextSpan #46

Open
passsy opened this issue Feb 6, 2024 · 0 comments
Open

act.tap(spotText('textSpan')) should click the exact TextSpan #46

passsy opened this issue Feb 6, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@passsy
Copy link
Owner

passsy commented Feb 6, 2024

Currently, act.tap() taps the center of the RenderObject. But RenderParagraph can give the exact location of the text and there should be clicked, allowing TextSpans with gesture recognizers.

      Offset locationOfTextSpan(String text) {
        assert(text.length > 1);
        final paragraph =
            spotText(text).snapshotRenderObject() as RenderParagraph;
        final fullText = paragraph.text.toPlainText();
        final start = fullText.indexOf(text);
        final end = start + text.length;
        final barLocationStart =
            paragraph.getOffsetForCaret(TextPosition(offset: start), Rect.zero);
        final barLocationEnd =
            paragraph.getOffsetForCaret(TextPosition(offset: end), Rect.zero);
        if (barLocationStart.dy == barLocationEnd.dy) {
          // on same line, click the middle
          return Offset(
            (barLocationStart.dx + barLocationEnd.dx) / 2,
            (barLocationStart.dy + barLocationEnd.dy) / 2,
          );
        } else {
          // click one pixel inside the first character
          return barLocationStart + const Offset(1, 1);
        }
      }

      Future<void> tapTextSpan(String text) async {
        final spanLocation = locationOfTextSpan(text);

        final binding = TestWidgetsFlutterBinding.instance;
        final downEvent = PointerDownEvent(position: spanLocation);
        binding.handlePointerEvent(downEvent);

        final upEvent = PointerUpEvent(position: spanLocation);
        binding.handlePointerEvent(upEvent);
      }
      testWidgets('concatenates text spans', (tester) async {
        await tester.pumpWidget(
          _stage(
            children: [
              RichText(
                text: TextSpan(
                  children: [
                    TextSpan(text: 'foo'),
                    TextSpan(
                      text: 'bar',
                      recognizer: TapGestureRecognizer()
                        ..onTap = () => print('click'),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );

        spotText('foo').existsOnce();
        spotText('foobar').existsOnce();

        await tapTextSpan('bar');
      });

This example should be the default

@passsy passsy added the enhancement New feature or request label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant