From 3bc3ea51edc6711f040f19e14cb07aed0288a191 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Tue, 4 Aug 2020 12:43:17 -0700 Subject: [PATCH] longestLine layout width (#62657) --- .../lib/src/painting/text_painter.dart | 16 +++++++++- packages/flutter/test/widgets/text_test.dart | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index 00145db34a6..8971f63dda6 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -568,7 +568,21 @@ class TextPainter { _previousCaretPrototype = null; _paragraph.layout(ui.ParagraphConstraints(width: maxWidth)); if (minWidth != maxWidth) { - final double newWidth = maxIntrinsicWidth.clamp(minWidth, maxWidth) as double; + double newWidth; + switch (textWidthBasis) { + case TextWidthBasis.longestLine: + // The parent widget expects the paragraph to be exactly + // `TextPainter.width` wide, if that value satisfies the constraints + // it gave to the TextPainter. So when `textWidthBasis` is longestLine, + // the paragraph's width needs to be as close to the width of its + // longest line as possible. + newWidth = _applyFloatingPointHack(_paragraph.longestLine); + break; + case TextWidthBasis.parent: + newWidth = maxIntrinsicWidth; + break; + } + newWidth = newWidth.clamp(minWidth, maxWidth) as double; if (newWidth != _applyFloatingPointHack(_paragraph.width)) { _paragraph.layout(ui.ParagraphConstraints(width: newWidth)); } diff --git a/packages/flutter/test/widgets/text_test.dart b/packages/flutter/test/widgets/text_test.dart index bb0f9694c4c..3115ef2cc22 100644 --- a/packages/flutter/test/widgets/text_test.dart +++ b/packages/flutter/test/widgets/text_test.dart @@ -914,6 +914,38 @@ void main() { expect(tester.getSize(find.text('RIGHT ALIGNED, LONGEST LINE')).width, equals(width)); }, skip: isBrowser); // https://github.com/flutter/flutter/issues/44020 + testWidgets( + 'textWidthBasis.longestLine confines the width of the paragraph ' + 'when given loose constraints', + (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/62550. + await tester.pumpWidget( + Center( + child: SizedBox( + width: 400, + child: Center( + child: RichText( + text: const TextSpan(text: 'fwefwefwewfefewfwe fwfwfwefweabcdefghijklmnopqrstuvwxyz'), + textWidthBasis: TextWidthBasis.longestLine, + textDirection: TextDirection.ltr, + ), + ), + ), + ), + ); + + expect(find.byType(RichText), paints..something((Symbol method, List arguments) { + if (method != #drawParagraph) + return false; + final ui.Paragraph paragraph = arguments[0] as ui.Paragraph; + if (paragraph.width > paragraph.longestLine) + throw 'paragraph width (${paragraph.width}) greater than its longest line (${paragraph.longestLine}).'; + if (paragraph.width >= 400) + throw 'paragraph.width (${paragraph.width}) >= 400'; + return true; + })); + }, skip: isBrowser); // https://github.com/flutter/flutter/issues/44020 + testWidgets('Paragraph.getBoxesForRange returns nothing when selection range is zero length', (WidgetTester tester) async { final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle()); builder.addText('hello');