Revert "Fix scroll offset when caret larger than viewport (#93248)" (#94386)
This reverts commit 1e255b137b1603dac629d63f6b22d2bc97853111.
diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart
index 685ca5b..b6f18cf 100644
--- a/packages/flutter/lib/src/widgets/editable_text.dart
+++ b/packages/flutter/lib/src/widgets/editable_text.dart
@@ -2151,7 +2151,7 @@
if (!_isMultiline) {
additionalOffset = rect.width >= editableSize.width
// Center `rect` if it's oversized.
- ? rect.center.dx - editableSize.width / 2
+ ? editableSize.width / 2 - rect.center.dx
// Valid additional offsets range from (rect.right - size.width)
// to (rect.left). Pick the closest one if out of range.
: 0.0.clamp(rect.right - editableSize.width, rect.left);
@@ -2167,7 +2167,7 @@
);
additionalOffset = expandedRect.height >= editableSize.height
- ? expandedRect.center.dy - editableSize.height / 2
+ ? editableSize.height / 2 - expandedRect.center.dy
: 0.0.clamp(expandedRect.bottom - editableSize.height, expandedRect.top);
unitOffset = const Offset(0, 1);
}
diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart
index ee3f7c1..03b0855 100644
--- a/packages/flutter/test/widgets/editable_text_test.dart
+++ b/packages/flutter/test/widgets/editable_text_test.dart
@@ -6077,111 +6077,12 @@
state.bringIntoView(TextPosition(offset: controller.text.length));
await tester.pumpAndSettle();
- // The SingleChildScrollView is scrolled instead of the EditableText to reveal the caret.
+ // The SingleChildScrollView is scrolled instead of the EditableText to
+ // reveal the caret.
expect(outerController.offset, outerController.position.maxScrollExtent);
expect(editableScrollController.offset, 0);
});
- testWidgets('bringIntoView centers the viewport on caret when the caret is wider than the viewport', (WidgetTester tester) async {
- const String text = 'to coz ze ze Szwecji';
- final TextEditingController controller = TextEditingController(text: text);
-
- await tester.pumpWidget(MaterialApp(
- home: Align(
- alignment: Alignment.topLeft,
- child: SizedBox(
- width: 32.0,
- height: 100.0,
- child: EditableText(
- showSelectionHandles: true,
- controller: controller,
- focusNode: FocusNode(),
- style: const TextStyle(fontFamily: 'Ahem', fontSize: 48.0, height: 1.0),
- cursorColor: Colors.blue,
- cursorWidth: 48.0,
- backgroundCursorColor: Colors.grey,
- selectionControls: materialTextSelectionControls,
- keyboardType: TextInputType.text,
- ),
- ),
- ),
- ));
-
- final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
- final RenderEditable renderEditable = state.renderEditable;
- final Scrollable scrollable = tester.widget<Scrollable>(find.byType(Scrollable));
-
- expect(scrollable.controller!.position.viewportDimension, equals(32.0));
- expect(scrollable.controller!.offset, 0.0);
- expect(renderEditable.maxScrollExtent, equals(977.0));
-
- state.bringIntoView(const TextPosition(offset: 2));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 2 * 48.0 + 48.0 / 2 - 32.0 / 2);
-
- state.bringIntoView(const TextPosition(offset: 5));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 5 * 48.0 + 48.0 / 2 - 32.0 / 2);
-
- state.bringIntoView(const TextPosition(offset: 7));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 7 * 48.0 + 48.0 / 2 - 32.0 / 2);
-
- state.bringIntoView(const TextPosition(offset: 9));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 9 * 48.0 + 48.0 / 2 - 32.0 / 2);
- });
-
- testWidgets('bringIntoView centers the viewport on caret when the caret is taller than the viewport', (WidgetTester tester) async {
- const String text = 'to\ncoz\nze\nze\nSzwecji';
- final TextEditingController controller = TextEditingController(text: text);
-
- await tester.pumpWidget(MaterialApp(
- home: Align(
- alignment: Alignment.topLeft,
- child: SizedBox(
- width: 500.0,
- height: 32.0,
- child: EditableText(
- showSelectionHandles: true,
- maxLines: null,
- controller: controller,
- focusNode: FocusNode(),
- style: const TextStyle(fontFamily: 'Ahem', fontSize: 48.0, height: 1.0),
- cursorColor: Colors.blue,
- backgroundCursorColor: Colors.grey,
- selectionControls: materialTextSelectionControls,
- keyboardType: TextInputType.text,
- ),
- ),
- ),
- ));
-
- final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
- final RenderEditable renderEditable = state.renderEditable;
- final Scrollable scrollable = tester.widget<Scrollable>(find.byType(Scrollable));
-
- expect(scrollable.controller!.position.viewportDimension, equals(32.0));
- expect(scrollable.controller!.offset, 0.0);
- expect(renderEditable.maxScrollExtent, equals(208.0));
-
- state.bringIntoView(const TextPosition(offset: 3));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 48.0 + 48.0 / 2 - 32.0 / 2);
-
- state.bringIntoView(const TextPosition(offset: 7));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 2 * 48.0 + 48.0 / 2 - 32.0 / 2);
-
- state.bringIntoView(const TextPosition(offset: 10));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 3 * 48.0 + 48.0 / 2 - 32.0 / 2);
-
- state.bringIntoView(const TextPosition(offset: 13));
- await tester.pumpAndSettle();
- expect(scrollable.controller!.offset, 4 * 48.0 + 48.0 / 2 - 32.0 / 2);
- });
-
testWidgets('bringIntoView does nothing if the physics prohibits implicit scrolling', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController(text: testText * 20);
final ScrollController scrollController = ScrollController();
@@ -6208,6 +6109,7 @@
));
}
+
await buildWithPhysics();
expect(scrollController.offset, 0);