Unverified Commit 9459f1c5 authored by hangyu's avatar hangyu Committed by GitHub

Fix chinese text is not selected by long press (#129320)

issue:#126652

 in Chinese text, word length is 1 and (position.offset == word.end) happens a lot.

Update if (position.offset >= word.end) to if (position.offset > word.end) to resolve the issue that Chinese characters are not selected.
parent 2e5a255b
...@@ -1497,7 +1497,7 @@ class _SelectableFragment with Selectable, ChangeNotifier implements TextLayoutM ...@@ -1497,7 +1497,7 @@ class _SelectableFragment with Selectable, ChangeNotifier implements TextLayoutM
assert(word.start >= range.start && word.end <= range.end); assert(word.start >= range.start && word.end <= range.end);
late TextPosition start; late TextPosition start;
late TextPosition end; late TextPosition end;
if (position.offset >= word.end) { if (position.offset > word.end) {
start = end = TextPosition(offset: position.offset); start = end = TextPosition(offset: position.offset);
} else { } else {
start = TextPosition(offset: word.start); start = TextPosition(offset: word.start);
......
...@@ -821,6 +821,34 @@ void main() { ...@@ -821,6 +821,34 @@ void main() {
expect(paintingContext.canvas.drawnRectPaint!.color, selectionColor); expect(paintingContext.canvas.drawnRectPaint!.color, selectionColor);
}); });
// Regression test for https://github.com/flutter/flutter/issues/126652.
test('paints selection when tap at chinese character', () async {
final TestSelectionRegistrar registrar = TestSelectionRegistrar();
const Color selectionColor = Color(0xAF6694e8);
final RenderParagraph paragraph = RenderParagraph(
const TextSpan(text: '你好'),
textDirection: TextDirection.ltr,
registrar: registrar,
selectionColor: selectionColor,
);
layout(paragraph);
final MockPaintingContext paintingContext = MockPaintingContext();
paragraph.paint(paintingContext, Offset.zero);
expect(paintingContext.canvas.drawnRect, isNull);
expect(paintingContext.canvas.drawnRectPaint, isNull);
for (final Selectable selectable in (paragraph.registrar! as TestSelectionRegistrar).selectables) {
selectable.dispatchSelectionEvent(const SelectWordSelectionEvent(globalPosition: Offset(7, 0)));
}
paintingContext.canvas.clear();
paragraph.paint(paintingContext, Offset.zero);
expect(paintingContext.canvas.drawnRect,
const Rect.fromLTWH(0.0, 0.0, 14.0, 14.0));
expect(paintingContext.canvas.drawnRectPaint!.style, PaintingStyle.fill);
expect(paintingContext.canvas.drawnRectPaint!.color, selectionColor);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/61016
test('getPositionForOffset works', () async { test('getPositionForOffset works', () async {
final RenderParagraph paragraph = RenderParagraph(const TextSpan(text: '1234567'), textDirection: TextDirection.ltr); final RenderParagraph paragraph = RenderParagraph(const TextSpan(text: '1234567'), textDirection: TextDirection.ltr);
layout(paragraph); layout(paragraph);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment