From 105026cd6841c826c6c9694e7324440ca71dd1b3 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 16 Jan 2019 08:13:00 -0800 Subject: [PATCH] Fix descenders cutoff in TextField (#26309) * Include caret gaps in position calculation to avoid descender cutoff * Calculate caret offset instead of exposing it * Test that textfields scroll vertically by full line height * Only add offset to end of caret to avoid going above 0.0 * Fix and test horizontal vs. vertical scrolling * CR: docs and top caret offset logic * Take max instead of assuming caretRect.top is always a full line down * Always subtract caretOffset from caretTop since caretRect.top can be negative --- .../lib/src/widgets/editable_text.dart | 17 ++++++++- .../test/material/text_field_test.dart | 37 ++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index fa5363f7c2..ce03c934a6 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -752,8 +752,21 @@ class EditableTextState extends State with AutomaticKeepAliveClien // Calculate the new scroll offset so the cursor remains visible. double _getScrollOffsetForCaret(Rect caretRect) { - final double caretStart = _isMultiline ? caretRect.top : caretRect.left; - final double caretEnd = _isMultiline ? caretRect.bottom : caretRect.right; + double caretStart; + double caretEnd; + if (_isMultiline) { + // The caret is vertically centered within the line. Expand the caret's + // height so that it spans the line because we're going to ensure that the entire + // expanded caret is scrolled into view. + final double lineHeight = renderEditable.preferredLineHeight; + final double caretOffset = (lineHeight - caretRect.height) / 2; + caretStart = caretRect.top - caretOffset; + caretEnd = caretRect.bottom + caretOffset; + } else { + caretStart = caretRect.left; + caretEnd = caretRect.right; + } + double scrollOffset = _scrollController.offset; final double viewportExtent = _scrollController.position.viewportDimension; if (caretStart < 0.0) // cursor before start of bounds diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 805f7b563c..64bd8f6715 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -1722,7 +1722,42 @@ void main() { await skipPastScrollingAnimation(tester); scrollableState = tester.firstState(find.byType(Scrollable)); - expect(scrollableState.position.pixels, isNot(equals(0.0))); + // For a horizontal input, scrolls to the exact position of the caret. + expect(scrollableState.position.pixels, equals(222.0)); + }); + + testWidgets('Multiline text field scrolls the caret into view', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(); + + await tester.pumpWidget( + overlay( + child: Container( + child: TextField( + controller: controller, + maxLines: 6, + ), + ), + ), + ); + + const String tallText = 'a\nb\nc\nd\ne\nf\ng'; // One line over max + await tester.enterText(find.byType(TextField), tallText); + await skipPastScrollingAnimation(tester); + + ScrollableState scrollableState = tester.firstState(find.byType(Scrollable)); + expect(scrollableState.position.pixels, equals(0.0)); + + // Move the caret to the end of the text and check that the text field + // scrolls to make the caret visible. + controller.selection = const TextSelection.collapsed(offset: tallText.length); + await tester.pump(); + await skipPastScrollingAnimation(tester); + + // Should have scrolled down exactly one line height (7 lines of text in 6 + // line text field). + final double lineHeight = findRenderEditable(tester).preferredLineHeight; + scrollableState = tester.firstState(find.byType(Scrollable)); + expect(scrollableState.position.pixels, equals(lineHeight)); }); testWidgets('haptic feedback', (WidgetTester tester) async {