From 9161ec42cb5cb9f070b177833aff0c6175e575a1 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 26 Dec 2018 08:56:02 -0800 Subject: [PATCH] Include cursor in textfield intrinsic width measurement (#25055) * Include cursor in textfield intrinsic width measurement * Add cursorWidth assertion, add comment about assumed cursor width, and test null stepWidth --- .../flutter/lib/src/rendering/editable.dart | 3 +- .../test/material/text_field_test.dart | 74 +++++++++++++++++++ .../flutter/test/rendering/editable_test.dart | 3 +- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 579885a0fb..780a43ce3d 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -160,6 +160,7 @@ class RenderEditable extends RenderBox { assert(ignorePointer != null), assert(obscureText != null), assert(textSelectionDelegate != null), + assert(cursorWidth != null && cursorWidth >= 0.0), _textPainter = TextPainter( text: text, textAlign: textAlign, @@ -1060,7 +1061,7 @@ class RenderEditable extends RenderBox { @override double computeMaxIntrinsicWidth(double height) { _layoutText(double.infinity); - return _textPainter.maxIntrinsicWidth; + return _textPainter.maxIntrinsicWidth + cursorWidth; } /// An estimate of the height of a line in the text. See [TextPainter.preferredLineHeight]. diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 31683d8a00..111b0ce96f 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -3485,6 +3485,80 @@ void main() { expect(tapCount, 0); }); + testWidgets('Includes cursor for TextField', (WidgetTester tester) async { + // This is a regression test for https://github.com/flutter/flutter/issues/24612 + + Widget buildFrame({ + double stepWidth, + double cursorWidth, + TextAlign textAlign, + }) { + return MaterialApp( + home: Scaffold( + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + IntrinsicWidth( + stepWidth: stepWidth, + child: TextField( + textAlign: textAlign, + cursorWidth: cursorWidth, + ), + ), + ], + ), + ), + ), + ); + } + + // A cursor of default size doesn't cause the TextField to increase its + // width. + const String text = '1234'; + double stepWidth = 80.0; + await tester.pumpWidget(buildFrame( + stepWidth: 80.0, + cursorWidth: 2.0, + textAlign: TextAlign.left, + )); + await tester.enterText(find.byType(TextField), text); + await tester.pumpAndSettle(); + expect(tester.getSize(find.byType(TextField)).width, stepWidth); + + // A wide cursor is counted in the width of the text and causes the + // TextField to increase to twice the stepWidth. + await tester.pumpWidget(buildFrame( + stepWidth: stepWidth, + cursorWidth: 18.0, + textAlign: TextAlign.left, + )); + await tester.enterText(find.byType(TextField), text); + await tester.pumpAndSettle(); + expect(tester.getSize(find.byType(TextField)).width, 2 * stepWidth); + + // A null stepWidth causes the TextField to perfectly wrap the text plus + // the cursor regardless of alignment. + stepWidth = null; + const double WIDTH_OF_CHAR = 16.0; + await tester.pumpWidget(buildFrame( + stepWidth: stepWidth, + cursorWidth: 18.0, + textAlign: TextAlign.left, + )); + await tester.enterText(find.byType(TextField), text); + await tester.pumpAndSettle(); + expect(tester.getSize(find.byType(TextField)).width, WIDTH_OF_CHAR * text.length + 18.0); + await tester.pumpWidget(buildFrame( + stepWidth: stepWidth, + cursorWidth: 18.0, + textAlign: TextAlign.right, + )); + await tester.enterText(find.byType(TextField), text); + await tester.pumpAndSettle(); + expect(tester.getSize(find.byType(TextField)).width, WIDTH_OF_CHAR * text.length + 18.0); + }); + testWidgets('TextField style is merged with theme', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/23994 diff --git a/packages/flutter/test/rendering/editable_test.dart b/packages/flutter/test/rendering/editable_test.dart index 100d0b22a5..07232b2506 100644 --- a/packages/flutter/test/rendering/editable_test.dart +++ b/packages/flutter/test/rendering/editable_test.dart @@ -44,7 +44,8 @@ void main() { textSelectionDelegate: delegate, ); expect(editable.getMinIntrinsicWidth(double.infinity), 50.0); - expect(editable.getMaxIntrinsicWidth(double.infinity), 50.0); + // The width includes the width of the cursor (1.0). + expect(editable.getMaxIntrinsicWidth(double.infinity), 51.0); expect(editable.getMinIntrinsicHeight(double.infinity), 10.0); expect(editable.getMaxIntrinsicHeight(double.infinity), 10.0);