Make TextSpan hit testing precise. (#139717)
Fixes https://github.com/flutter/flutter/issues/131435, #104594, #43400 Needs https://github.com/flutter/engine/pull/48774 (to fix the web test failure). Currently the method we use for text span hit testing `TextPainter.getPositionForOffset` always returns the closest `TextPosition`, even when the given offset is far away from the text. The new TextPaintes method tells you the layout bounds (`width = letterspacing / 2 + x_advance + letterspacing / 2`, `height = font ascent + font descent`) of a character, the PR changes the hit testing implementation such that a TextSpan is only considered hit if the point-down event landed in one of it's character's layout bounds. Potential issues: 1. In theory since the text is baseline aligned, we should use the max ascent and max descent of each character to calculate the height of the text span's hit-test region, in case some characters in the span have to fall back to a different font, but that will be slower and it typically doesn't make a huge difference. This is a breaking change. It also introduces a new finder and a new method `WidgetTester.tapOnText`: `await tester.tapOnText('string to match')` for ease of migration.
This commit is contained in:
@@ -1482,6 +1482,172 @@ void main() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('WidgetTester.tapOnText', () {
|
||||
final List<String > tapLogs = <String>[];
|
||||
final TapGestureRecognizer tapA = TapGestureRecognizer()..onTap = () { tapLogs.add('A'); };
|
||||
final TapGestureRecognizer tapB = TapGestureRecognizer()..onTap = () { tapLogs.add('B'); };
|
||||
final TapGestureRecognizer tapC = TapGestureRecognizer()..onTap = () { tapLogs.add('C'); };
|
||||
tearDown(tapLogs.clear);
|
||||
tearDownAll(() {
|
||||
tapA.dispose();
|
||||
tapB.dispose();
|
||||
tapC.dispose();
|
||||
});
|
||||
|
||||
testWidgets('basic test', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Text.rich(TextSpan(text: 'match', recognizer: tapA)),
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tapOnText(find.textRange.ofSubstring('match'));
|
||||
expect(tapLogs, <String>['A']);
|
||||
});
|
||||
|
||||
testWidgets('partially obstructed: find a hit-testable Offset', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: <Widget>[
|
||||
Positioned(
|
||||
left: 100.0 - 9 * 10.0, // Only the last character is visible.
|
||||
child: Text.rich(TextSpan(text: 'text match', style: const TextStyle(fontSize: 10), recognizer: tapA)),
|
||||
),
|
||||
const Positioned(
|
||||
left: 0.0,
|
||||
right: 100.0,
|
||||
child: MetaData(behavior: HitTestBehavior.opaque),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
() => tester.tapOnText(find.textRange.ofSubstring('text match')),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('multiline text partially obstructed: find a hit-testable Offset', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: <Widget>[
|
||||
Positioned(
|
||||
width: 100.0,
|
||||
top: 23.0,
|
||||
left: 0.0,
|
||||
child: Text.rich(
|
||||
TextSpan(
|
||||
style: const TextStyle(fontSize: 10),
|
||||
children: <InlineSpan>[
|
||||
TextSpan(text: 'AAAAAAAAA ', recognizer: tapA),
|
||||
TextSpan(text: 'BBBBBBBBB ', recognizer: tapB), // The only visible line
|
||||
TextSpan(text: 'CCCCCCCCC ', recognizer: tapC),
|
||||
]
|
||||
)
|
||||
),
|
||||
),
|
||||
const Positioned(
|
||||
top: 23.0, // Some random offset to test the global to local Offset conversion
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
height: 10.0,
|
||||
child: MetaData(behavior: HitTestBehavior.opaque, child: SizedBox.expand()),
|
||||
),
|
||||
const Positioned(
|
||||
top: 43.0,
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
height: 10.0,
|
||||
child: MetaData(behavior: HitTestBehavior.opaque, child: SizedBox.expand()),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tapOnText(find.textRange.ofSubstring('AAAAAAAAA BBBBBBBBB CCCCCCCCC '));
|
||||
expect(tapLogs, <String>['B']);
|
||||
});
|
||||
|
||||
testWidgets('error message: no matching text', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await expectLater(
|
||||
() => tester.tapOnText(find.textRange.ofSubstring('nonexistent')),
|
||||
throwsA(isFlutterError.having(
|
||||
(FlutterError error) => error.message,
|
||||
'message',
|
||||
contains('Found 0 non-overlapping TextRanges that match the Pattern "nonexistent": []'),
|
||||
)),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('error message: too many matches', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Text.rich(
|
||||
TextSpan(
|
||||
text: 'match',
|
||||
recognizer: tapA,
|
||||
children: <InlineSpan>[TextSpan(text: 'another match', recognizer: tapB)],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
() => tester.tapOnText(find.textRange.ofSubstring('match')),
|
||||
throwsA(isFlutterError.having(
|
||||
(FlutterError error) => error.message,
|
||||
'message',
|
||||
stringContainsInOrder(<String>[
|
||||
'Found 2 non-overlapping TextRanges that match the Pattern "match"',
|
||||
'TextRange(start: 0, end: 5)',
|
||||
'TextRange(start: 13, end: 18)',
|
||||
'The "tapOnText" method needs a single non-empty TextRange.',
|
||||
])
|
||||
)),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('error message: not hit-testable', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: <Widget>[
|
||||
Text.rich(TextSpan(text: 'match', recognizer: tapA)),
|
||||
const MetaData(behavior: HitTestBehavior.opaque),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
() => tester.tapOnText(find.textRange.ofSubstring('match')),
|
||||
throwsA(isFlutterError.having(
|
||||
(FlutterError error) => error.message,
|
||||
'message',
|
||||
stringContainsInOrder(<String>[
|
||||
'The finder used was: A finder that searches for non-overlapping TextRanges that match the Pattern "match".',
|
||||
'Found a matching substring in a static text widget, within TextRange(start: 0, end: 5).',
|
||||
'But the "tapOnText" method could not find a hit-testable Offset with in that text range.',
|
||||
])
|
||||
)),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class _SemanticsTestWidget extends StatelessWidget {
|
||||
|
||||
@@ -331,6 +331,100 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('text range finders', () {
|
||||
testWidgets('basic text span test', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
_boilerplate(const IndexedStack(
|
||||
sizing: StackFit.expand,
|
||||
children: <Widget>[
|
||||
Text.rich(TextSpan(
|
||||
text: 'sub',
|
||||
children: <InlineSpan>[
|
||||
TextSpan(text: 'stringsub'),
|
||||
TextSpan(text: 'stringsub'),
|
||||
TextSpan(text: 'stringsub'),
|
||||
],
|
||||
)),
|
||||
Text('substringsub'),
|
||||
],
|
||||
)),
|
||||
);
|
||||
|
||||
expect(find.textRange.ofSubstring('substringsub'), findsExactly(2)); // Pattern skips overlapping matches.
|
||||
expect(find.textRange.ofSubstring('substringsub').first.evaluate().single.textRange, const TextRange(start: 0, end: 12));
|
||||
expect(find.textRange.ofSubstring('substringsub').last.evaluate().single.textRange, const TextRange(start: 18, end: 30));
|
||||
|
||||
expect(
|
||||
find.textRange.ofSubstring('substringsub').first.evaluate().single.renderObject,
|
||||
find.textRange.ofSubstring('substringsub').last.evaluate().single.renderObject,
|
||||
);
|
||||
|
||||
expect(find.textRange.ofSubstring('substringsub', skipOffstage: false), findsExactly(3));
|
||||
});
|
||||
|
||||
testWidgets('basic text span test', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
_boilerplate(const IndexedStack(
|
||||
sizing: StackFit.expand,
|
||||
children: <Widget>[
|
||||
Text.rich(TextSpan(
|
||||
text: 'sub',
|
||||
children: <InlineSpan>[
|
||||
TextSpan(text: 'stringsub'),
|
||||
TextSpan(text: 'stringsub'),
|
||||
TextSpan(text: 'stringsub'),
|
||||
],
|
||||
)),
|
||||
Text('substringsub'),
|
||||
],
|
||||
)),
|
||||
);
|
||||
|
||||
expect(find.textRange.ofSubstring('substringsub'), findsExactly(2)); // Pattern skips overlapping matches.
|
||||
expect(find.textRange.ofSubstring('substringsub').first.evaluate().single.textRange, const TextRange(start: 0, end: 12));
|
||||
expect(find.textRange.ofSubstring('substringsub').last.evaluate().single.textRange, const TextRange(start: 18, end: 30));
|
||||
|
||||
expect(
|
||||
find.textRange.ofSubstring('substringsub').first.evaluate().single.renderObject,
|
||||
find.textRange.ofSubstring('substringsub').last.evaluate().single.renderObject,
|
||||
);
|
||||
|
||||
expect(find.textRange.ofSubstring('substringsub', skipOffstage: false), findsExactly(3));
|
||||
});
|
||||
|
||||
testWidgets('descendentOf', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
_boilerplate(
|
||||
const Column(
|
||||
children: <Widget>[
|
||||
Text.rich(TextSpan(text: 'text')),
|
||||
Text.rich(TextSpan(text: 'text')),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.textRange.ofSubstring('text'), findsExactly(2));
|
||||
expect(find.textRange.ofSubstring('text', descendentOf: find.text('text').first), findsOne);
|
||||
});
|
||||
|
||||
testWidgets('finds only static text for now', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
_boilerplate(
|
||||
EditableText(
|
||||
controller: TextEditingController(text: 'text'),
|
||||
focusNode: FocusNode(),
|
||||
style: const TextStyle(),
|
||||
cursorColor: const Color(0x00000000),
|
||||
backgroundCursorColor: const Color(0x00000000),
|
||||
)
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.textRange.ofSubstring('text'), findsNothing);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('ChainedFinders chain properly', (WidgetTester tester) async {
|
||||
final GlobalKey key1 = GlobalKey();
|
||||
await tester.pumpWidget(
|
||||
|
||||
Reference in New Issue
Block a user