From 6c70a8fef6e0a901eca00c9890a0969e7b32922b Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 17 Apr 2020 08:07:27 -0700 Subject: [PATCH] Fix right alignment TWB longestLine (#54234) Fixes an alignment bug seen when using both TextWidthBasis.longestLine and TextAlign.right. --- .../lib/src/painting/text_painter.dart | 2 +- packages/flutter/test/widgets/text_test.dart | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index 22cc9fcb46..0b3b9c9960 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -564,7 +564,7 @@ class TextPainter { _paragraph.layout(ui.ParagraphConstraints(width: maxWidth)); if (minWidth != maxWidth) { final double newWidth = maxIntrinsicWidth.clamp(minWidth, maxWidth) as double; - if (newWidth != width) { + 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 9fd4e8ea31..c307c4edfe 100644 --- a/packages/flutter/test/widgets/text_test.dart +++ b/packages/flutter/test/widgets/text_test.dart @@ -800,6 +800,56 @@ void main() { expect(textSizeLongestLine.height, equals(fontHeight * 2)); }, skip: isBrowser); // TODO(yjbanov): https://github.com/flutter/flutter/issues/44020 + testWidgets('textWidthBasis with textAlign still obeys parent alignment', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: const [ + Text( + 'LEFT ALIGNED, PARENT', + textAlign: TextAlign.left, + textWidthBasis: TextWidthBasis.parent, + ), + Text( + 'RIGHT ALIGNED, PARENT', + textAlign: TextAlign.right, + textWidthBasis: TextWidthBasis.parent, + ), + Text( + 'LEFT ALIGNED, LONGEST LINE', + textAlign: TextAlign.left, + textWidthBasis: TextWidthBasis.longestLine, + ), + Text( + 'RIGHT ALIGNED, LONGEST LINE', + textAlign: TextAlign.right, + textWidthBasis: TextWidthBasis.longestLine, + ), + ], + ), + ), + ), + ), + ); + + // All Texts have the same horizontal alignment. + final double offsetX = tester.getTopLeft(find.text('LEFT ALIGNED, PARENT')).dx; + expect(tester.getTopLeft(find.text('RIGHT ALIGNED, PARENT')).dx, equals(offsetX)); + expect(tester.getTopLeft(find.text('LEFT ALIGNED, LONGEST LINE')).dx, equals(offsetX)); + expect(tester.getTopLeft(find.text('RIGHT ALIGNED, LONGEST LINE')).dx, equals(offsetX)); + + // All Texts are less than or equal to the width of the Column. + final double width = tester.getSize(find.byType(Column)).width; + expect(tester.getSize(find.text('LEFT ALIGNED, PARENT')).width, lessThan(width)); + expect(tester.getSize(find.text('RIGHT ALIGNED, PARENT')).width, lessThan(width)); + expect(tester.getSize(find.text('LEFT ALIGNED, LONGEST LINE')).width, lessThan(width)); + expect(tester.getSize(find.text('RIGHT ALIGNED, LONGEST LINE')).width, equals(width)); + }, skip: isBrowser); // TODO(yjbanov): 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');