diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 400d1a4488..75b5de9957 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -294,6 +294,7 @@ class CupertinoTextField extends StatefulWidget { this.scrollController, this.scrollPhysics, this.autofillHints = const [], + this.clipBehavior = Clip.hardEdge, this.restorationId, this.enableIMEPersonalizedLearning = true, }) : assert(textAlign != null), @@ -450,6 +451,7 @@ class CupertinoTextField extends StatefulWidget { this.scrollController, this.scrollPhysics, this.autofillHints = const [], + this.clipBehavior = Clip.hardEdge, this.restorationId, this.enableIMEPersonalizedLearning = true, }) : assert(textAlign != null), @@ -493,6 +495,7 @@ class CupertinoTextField extends StatefulWidget { !identical(keyboardType, TextInputType.text), 'Use keyboardType TextInputType.multiline when using TextInputAction.newline on a multiline TextField.', ), + assert(clipBehavior != null), assert(enableIMEPersonalizedLearning != null), keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline), toolbarOptions = toolbarOptions ?? (obscureText ? @@ -786,6 +789,11 @@ class CupertinoTextField extends StatefulWidget { /// {@macro flutter.services.AutofillConfiguration.autofillHints} final Iterable? autofillHints; + /// {@macro flutter.material.Material.clipBehavior} + /// + /// Defaults to [Clip.hardEdge]. + final Clip clipBehavior; + /// {@macro flutter.material.textfield.restorationId} final String? restorationId; @@ -833,6 +841,7 @@ class CupertinoTextField extends StatefulWidget { properties.add(EnumProperty('textAlign', textAlign, defaultValue: TextAlign.start)); properties.add(DiagnosticsProperty('textAlignVertical', textAlignVertical, defaultValue: null)); properties.add(EnumProperty('textDirection', textDirection, defaultValue: null)); + properties.add(DiagnosticsProperty('clipBehavior', clipBehavior, defaultValue: Clip.hardEdge)); properties.add(DiagnosticsProperty('enableIMEPersonalizedLearning', enableIMEPersonalizedLearning, defaultValue: true)); } } @@ -1265,6 +1274,7 @@ class _CupertinoTextFieldState extends State with Restoratio scrollPhysics: widget.scrollPhysics, enableInteractiveSelection: widget.enableInteractiveSelection, autofillClient: this, + clipBehavior: widget.clipBehavior, restorationId: 'editable', enableIMEPersonalizedLearning: widget.enableIMEPersonalizedLearning, ), diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index caa6c63c90..5222ef4e6c 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -346,6 +346,7 @@ class TextField extends StatefulWidget { this.scrollController, this.scrollPhysics, this.autofillHints = const [], + this.clipBehavior = Clip.hardEdge, this.restorationId, this.enableIMEPersonalizedLearning = true, }) : assert(textAlign != null), @@ -387,6 +388,7 @@ class TextField extends StatefulWidget { !identical(keyboardType, TextInputType.text), 'Use keyboardType TextInputType.multiline when using TextInputAction.newline on a multiline TextField.', ), + assert(clipBehavior != null), assert(enableIMEPersonalizedLearning != null), keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline), toolbarOptions = toolbarOptions ?? (obscureText ? @@ -761,6 +763,11 @@ class TextField extends StatefulWidget { /// {@macro flutter.services.AutofillConfiguration.autofillHints} final Iterable? autofillHints; + /// {@macro flutter.material.Material.clipBehavior} + /// + /// Defaults to [Clip.hardEdge]. + final Clip clipBehavior; + /// {@template flutter.material.textfield.restorationId} /// Restoration ID to save and restore the state of the text field. /// @@ -823,6 +830,7 @@ class TextField extends StatefulWidget { properties.add(DiagnosticsProperty('selectionControls', selectionControls, defaultValue: null)); properties.add(DiagnosticsProperty('scrollController', scrollController, defaultValue: null)); properties.add(DiagnosticsProperty('scrollPhysics', scrollPhysics, defaultValue: null)); + properties.add(DiagnosticsProperty('clipBehavior', clipBehavior, defaultValue: Clip.hardEdge)); properties.add(DiagnosticsProperty('enableIMEPersonalizedLearning', enableIMEPersonalizedLearning, defaultValue: true)); } } @@ -1265,6 +1273,7 @@ class _TextFieldState extends State with RestorationMixin implements scrollPhysics: widget.scrollPhysics, autofillClient: this, autocorrectionTextRectColor: autocorrectionTextRectColor, + clipBehavior: widget.clipBehavior, restorationId: 'editable', enableIMEPersonalizedLearning: widget.enableIMEPersonalizedLearning, ), diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index 1b165dac65..83296b05a4 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -23,6 +23,7 @@ import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; import '../widgets/clipboard_utils.dart'; +import '../widgets/editable_text_utils.dart' show OverflowWidgetTextEditingController; import '../widgets/semantics_tester.dart'; // On web, the context menu (aka toolbar) is provided by the browser. @@ -4819,4 +4820,46 @@ void main() { final EditableText rtlWidget = tester.widget(find.byType(EditableText)); expect(rtlWidget.textDirection, TextDirection.rtl); }); + + testWidgets('clipBehavior has expected defaults', (WidgetTester tester) async { + await tester.pumpWidget( + const CupertinoApp( + home: CupertinoTextField( + ), + ), + ); + + final CupertinoTextField textField = tester.firstWidget(find.byType(CupertinoTextField)); + expect(textField.clipBehavior, Clip.hardEdge); + }); + + testWidgets('Overflow clipBehavior none golden', (WidgetTester tester) async { + final Widget widget = CupertinoApp( + home: RepaintBoundary( + key: const ValueKey(1), + child: SizedBox( + height: 200.0, + width: 200.0, + child: Center( + child: CupertinoTextField( + controller: OverflowWidgetTextEditingController(), + clipBehavior: Clip.none, + ), + ), + ), + ), + ); + await tester.pumpWidget(widget); + + final CupertinoTextField textField = tester.firstWidget(find.byType(CupertinoTextField)); + expect(textField.clipBehavior, Clip.none); + + final EditableText editableText = tester.firstWidget(find.byType(EditableText)); + expect(editableText.clipBehavior, Clip.none); + + await expectLater( + find.byKey(const ValueKey(1)), + matchesGoldenFile('overflow_clipbehavior_none.cupertino.0.png'), + ); + }); } diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 3b04f5965d..4fa99060d6 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -24,7 +24,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import '../widgets/clipboard_utils.dart'; -import '../widgets/editable_text_utils.dart' show findRenderEditable, globalize, textOffsetToPosition; +import '../widgets/editable_text_utils.dart' show findRenderEditable, globalize, textOffsetToPosition, OverflowWidgetTextEditingController; import '../widgets/semantics_tester.dart'; import 'feedback_tester.dart'; @@ -552,6 +552,48 @@ void main() { expect(textField.cursorRadius, const Radius.circular(3.0)); }); + testWidgets('clipBehavior has expected defaults', (WidgetTester tester) async { + await tester.pumpWidget( + overlay( + child: const TextField( + ), + ), + ); + + final TextField textField = tester.firstWidget(find.byType(TextField)); + expect(textField.clipBehavior, Clip.hardEdge); + }); + + testWidgets('Overflow clipBehavior none golden', (WidgetTester tester) async { + final Widget widget = overlay( + child: RepaintBoundary( + key: const ValueKey(1), + child: SizedBox( + height: 200, + width: 200, + child: Center( + child: TextField( + controller: OverflowWidgetTextEditingController(), + clipBehavior: Clip.none, + ), + ), + ), + ), + ); + await tester.pumpWidget(widget); + + final TextField textField = tester.firstWidget(find.byType(TextField)); + expect(textField.clipBehavior, Clip.none); + + final EditableText editableText = tester.firstWidget(find.byType(EditableText)); + expect(editableText.clipBehavior, Clip.none); + + await expectLater( + find.byKey(const ValueKey(1)), + matchesGoldenFile('overflow_clipbehavior_none.material.0.png'), + ); + }); + testWidgets('Material cursor android golden', (WidgetTester tester) async { final Widget widget = overlay( child: const RepaintBoundary( diff --git a/packages/flutter/test/widgets/editable_text_utils.dart b/packages/flutter/test/widgets/editable_text_utils.dart index 4ac39b49b9..03628e8aa8 100644 --- a/packages/flutter/test/widgets/editable_text_utils.dart +++ b/packages/flutter/test/widgets/editable_text_utils.dart @@ -45,3 +45,27 @@ Offset textOffsetToPosition(WidgetTester tester, int offset) { expect(endpoints.length, 1); return endpoints[0].point + const Offset(0.0, -2.0); } + +// Simple controller that builds a WidgetSpan with 100 height. +class OverflowWidgetTextEditingController extends TextEditingController { + @override + TextSpan buildTextSpan({ + required BuildContext context, + TextStyle? style, + required bool withComposing, + }) { + return TextSpan( + style: style, + children: [ + const TextSpan(text: 'Hi'), + WidgetSpan( + alignment: PlaceholderAlignment.bottom, + child: Container( + color: Colors.redAccent, + height: 100.0, + ), + ), + ], + ); + } +}