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.
This commit is contained in:
parent
2e5a255b87
commit
9459f1c517
@ -1497,7 +1497,7 @@ class _SelectableFragment with Selectable, ChangeNotifier implements TextLayoutM
|
||||
assert(word.start >= range.start && word.end <= range.end);
|
||||
late TextPosition start;
|
||||
late TextPosition end;
|
||||
if (position.offset >= word.end) {
|
||||
if (position.offset > word.end) {
|
||||
start = end = TextPosition(offset: position.offset);
|
||||
} else {
|
||||
start = TextPosition(offset: word.start);
|
||||
|
@ -821,6 +821,34 @@ void main() {
|
||||
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 {
|
||||
final RenderParagraph paragraph = RenderParagraph(const TextSpan(text: '1234567'), textDirection: TextDirection.ltr);
|
||||
layout(paragraph);
|
||||
|
Loading…
x
Reference in New Issue
Block a user