From fa7340a328cbfbe745748cfcd5d5fe08c401c64c Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Wed, 2 Oct 2019 19:04:50 -0400 Subject: [PATCH] Return WidgetSpans from getSpanForPosition (#40635) --- .../flutter/lib/src/rendering/paragraph.dart | 10 ++++++-- .../flutter/lib/src/widgets/widget_span.dart | 4 +++ .../flutter/test/painting/text_span_test.dart | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index a65c5fe918c..c3ba0d564eb 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -442,8 +442,14 @@ class RenderParagraph extends RenderBox _layoutTextWithConstraints(constraints); final Offset offset = entry.localPosition; final TextPosition position = _textPainter.getPositionForOffset(offset); - final TextSpan span = _textPainter.text.getSpanForPosition(position); - span?.recognizer?.addPointer(event); + final InlineSpan span = _textPainter.text.getSpanForPosition(position); + if (span == null) { + return; + } + if (span is TextSpan) { + final TextSpan textSpan = span; + textSpan.recognizer?.addPointer(event); + } } bool _needsClipping = false; diff --git a/packages/flutter/lib/src/widgets/widget_span.dart b/packages/flutter/lib/src/widgets/widget_span.dart index 3cac58e5c91..250f437a95d 100644 --- a/packages/flutter/lib/src/widgets/widget_span.dart +++ b/packages/flutter/lib/src/widgets/widget_span.dart @@ -128,6 +128,10 @@ class WidgetSpan extends PlaceholderSpan { @override InlineSpan getSpanForPositionVisitor(TextPosition position, Accumulator offset) { + if (position.offset == offset.value) { + return this; + } + offset.increment(1); return null; } diff --git a/packages/flutter/test/painting/text_span_test.dart b/packages/flutter/test/painting/text_span_test.dart index 2ceae4ffbad..09edc75529d 100644 --- a/packages/flutter/test/painting/text_span_test.dart +++ b/packages/flutter/test/painting/text_span_test.dart @@ -209,4 +209,29 @@ void main() { expect(textSpan1.compareTo(textSpan1), RenderComparison.identical); expect(textSpan2.compareTo(textSpan2), RenderComparison.identical); }); + + test('GetSpanForPosition with WidgetSpan', () { + const TextSpan textSpan = TextSpan( + text: 'a', + children: [ + TextSpan(text: 'b'), + WidgetSpan( + child: Text.rich( + TextSpan( + children: [ + WidgetSpan(child: SizedBox(width: 10, height: 10)), + TextSpan(text: 'The sky is falling :)'), + ], + ), + ), + ), + TextSpan(text: 'c'), + ], + ); + + expect(textSpan.getSpanForPosition(const TextPosition(offset: 0)).runtimeType, TextSpan); + expect(textSpan.getSpanForPosition(const TextPosition(offset: 1)).runtimeType, TextSpan); + expect(textSpan.getSpanForPosition(const TextPosition(offset: 2)).runtimeType, WidgetSpan); + expect(textSpan.getSpanForPosition(const TextPosition(offset: 3)).runtimeType, TextSpan); + }); }