diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index 62d67bb4fc..413c7be336 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -1569,7 +1569,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { final Offset paintOffset = _paintOffset; - final List boxes = selection.isCollapsed ? [] : _textPainter.getBoxesForSelection(selection); if (boxes.isEmpty) { diff --git a/packages/flutter/lib/src/rendering/shifted_box.dart b/packages/flutter/lib/src/rendering/shifted_box.dart index 4346f374f0..8d9500f01f 100644 --- a/packages/flutter/lib/src/rendering/shifted_box.dart +++ b/packages/flutter/lib/src/rendering/shifted_box.dart @@ -14,6 +14,8 @@ import 'debug_overflow_indicator.dart'; import 'object.dart'; import 'stack.dart' show RelativeRect; +typedef BoxConstraintsTransform = BoxConstraints Function(BoxConstraints); + /// Abstract class for one-child-layout render boxes that provide control over /// the child's position. abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixin { @@ -594,59 +596,62 @@ class RenderConstrainedOverflowBox extends RenderAligningShiftedBox { } } -/// Renders a box, imposing no constraints on its child, allowing the child to -/// render at its "natural" size. +/// A [RenderBox] that applies an arbitary transform to its [constraints], and +/// sizes its child using the normalized output [BoxConstraints], treating any +/// overflow as error. /// -/// This allows a child to render at the size it would render if it were alone -/// on an infinite canvas with no constraints. This box will then attempt to -/// adopt the same size, within the limits of its own constraints. If it ends -/// up with a different size, it will align the child based on [alignment]. -/// If the box cannot expand enough to accommodate the entire child, the -/// child will be clipped. +/// This [RenderBox] sizes its child using a normalized [BoxConstraints] created +/// by applying [constraintsTransform] to this [RenderBox]'s own [constraints]. +/// This box will then attempt to adopt the same size, within the limits of its +/// own constraints. If it ends up with a different size, it will align the +/// child based on [alignment]. If the box cannot expand enough to accommodate +/// the entire child, the child will be clipped if [clipBehavior] is not +/// [Clip.none]. /// /// In debug mode, if the child overflows the box, a warning will be printed on /// the console, and black and yellow striped areas will appear where the /// overflow occurs. /// +/// This [RenderBox] can be used to allow the child to enforce some of its +/// intrinsic sizing rules, partially disregard the constraints set by its +/// parent render object, and display a warning in debug mode if the parent +/// render object fails to provide enough space. +/// /// See also: /// +/// * [ConstraintsTransformBox], the widget that makes use of this +/// [RenderObject] and exposes the same functionality. /// * [RenderConstrainedBox], which renders a box which imposes constraints /// on its child. /// * [RenderConstrainedOverflowBox], which renders a box that imposes different /// constraints on its child than it gets from its parent, possibly allowing /// the child to overflow the parent. -/// * [RenderSizedOverflowBox], a render object that is a specific size but -/// passes its original constraints through to its child, which it allows to -/// overflow. -class RenderUnconstrainedBox extends RenderAligningShiftedBox with DebugOverflowIndicatorMixin { - /// Create a render object that sizes itself to the child but does not - /// pass the [constraints] down to that child. +/// * [RenderUnconstrainedBox] which allows its children to render themselves +/// unconstrained, expands to fit them, and considers overflow to be an error. +class RenderConstraintsTransformBox extends RenderAligningShiftedBox with DebugOverflowIndicatorMixin { + /// Create a render object that sizes itself to the child and modifies the + /// [constraints] before passing it down to that child. /// - /// The [alignment] must not be null. - RenderUnconstrainedBox({ + /// The [alignment] and [clipBehavior] must not be null. + RenderConstraintsTransformBox({ @required AlignmentGeometry alignment, @required TextDirection textDirection, - Axis constrainedAxis, + @required BoxConstraintsTransform constraintsTransform, RenderBox child, Clip clipBehavior = Clip.none, }) : assert(alignment != null), assert(clipBehavior != null), - _constrainedAxis = constrainedAxis, + _constraintsTransform = constraintsTransform, _clipBehavior = clipBehavior, super.mixin(alignment, textDirection, child); - /// The axis to retain constraints on, if any. - /// - /// If not set, or set to null (the default), neither axis will retain its - /// constraints. If set to [Axis.vertical], then vertical constraints will - /// be retained, and if set to [Axis.horizontal], then horizontal constraints - /// will be retained. - Axis get constrainedAxis => _constrainedAxis; - Axis _constrainedAxis; - set constrainedAxis(Axis value) { - if (_constrainedAxis == value) + /// @{macro flutter.widgets.constraintsTransform} + BoxConstraintsTransform get constraintsTransform => _constraintsTransform; + BoxConstraintsTransform _constraintsTransform; + set constraintsTransform(BoxConstraintsTransform value) { + if (_constraintsTransform == value) return; - _constrainedAxis = value; + _constraintsTransform = value; markNeedsLayout(); } @@ -672,21 +677,10 @@ class RenderUnconstrainedBox extends RenderAligningShiftedBox with DebugOverflow void performLayout() { final BoxConstraints constraints = this.constraints; if (child != null) { - // Let the child lay itself out at it's "natural" size, but if - // constrainedAxis is non-null, keep any constraints on that axis. - BoxConstraints childConstraints; - if (constrainedAxis != null) { - switch (constrainedAxis) { - case Axis.horizontal: - childConstraints = BoxConstraints(maxWidth: constraints.maxWidth, minWidth: constraints.minWidth); - break; - case Axis.vertical: - childConstraints = BoxConstraints(maxHeight: constraints.maxHeight, minHeight: constraints.minHeight); - break; - } - } else { - childConstraints = const BoxConstraints(); - } + assert(constraintsTransform != null); + final BoxConstraints childConstraints = constraintsTransform(constraints); + assert(childConstraints != null); + assert(childConstraints.isNormalized, '$childConstraints is not normalized'); child.layout(childConstraints, parentUsesSize: true); size = constraints.constrain(child.size); alignChild(); @@ -741,6 +735,78 @@ class RenderUnconstrainedBox extends RenderAligningShiftedBox with DebugOverflow } } +/// Renders a box, imposing no constraints on its child, allowing the child to +/// render at its "natural" size. +/// +/// This allows a child to render at the size it would render if it were alone +/// on an infinite canvas with no constraints. This box will then attempt to +/// adopt the same size, within the limits of its own constraints. If it ends +/// up with a different size, it will align the child based on [alignment]. +/// If the box cannot expand enough to accommodate the entire child, the +/// child will be clipped. +/// +/// In debug mode, if the child overflows the box, a warning will be printed on +/// the console, and black and yellow striped areas will appear where the +/// overflow occurs. +/// +/// See also: +/// +/// * [RenderConstrainedBox], which renders a box which imposes constraints +/// on its child. +/// * [RenderConstrainedOverflowBox], which renders a box that imposes different +/// constraints on its child than it gets from its parent, possibly allowing +/// the child to overflow the parent. +/// * [RenderSizedOverflowBox], a render object that is a specific size but +/// passes its original constraints through to its child, which it allows to +/// overflow. +class RenderUnconstrainedBox extends RenderConstraintsTransformBox { + /// Create a render object that sizes itself to the child but does not + /// pass the [constraints] down to that child. + /// + /// The [alignment] and [clipBehavior] must not be null. + RenderUnconstrainedBox({ + @required AlignmentGeometry alignment, + @required TextDirection textDirection, + Axis constrainedAxis, + RenderBox child, + Clip clipBehavior = Clip.none, + }) : assert(alignment != null), + assert(clipBehavior != null), + _constrainedAxis = constrainedAxis, + super(alignment: alignment, textDirection: textDirection, child: child, clipBehavior: clipBehavior, constraintsTransform: null) { + _constraintsTransform = _convertAxis; + } + + /// The axis to retain constraints on, if any. + /// + /// If not set, or set to null (the default), neither axis will retain its + /// constraints. If set to [Axis.vertical], then vertical constraints will + /// be retained, and if set to [Axis.horizontal], then horizontal constraints + /// will be retained. + Axis get constrainedAxis => _constrainedAxis; + Axis _constrainedAxis; + set constrainedAxis(Axis value) { + if (_constrainedAxis == value) + return; + _constrainedAxis = value; + markNeedsLayout(); + } + + BoxConstraints _convertAxis(BoxConstraints constriants) { + if (constrainedAxis == null) { + return const BoxConstraints(); + } + switch (constrainedAxis) { + case Axis.horizontal: + return constraints.copyWith(maxHeight: double.infinity, minHeight: 0); + case Axis.vertical: + return constraints.copyWith(maxWidth: double.infinity, minWidth: 0); + } + assert(false); + return null; + } +} + /// A render object that is a specific size but passes its original constraints /// through to its child, which it allows to overflow. /// diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 1cd427a11b..0f68302e9a 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -30,6 +30,7 @@ export 'package:flutter/rendering.dart' show AlignmentGeometryTween, Axis, BoxConstraints, + BoxConstraintsTransform, CrossAxisAlignment, CustomClipper, CustomPainter, @@ -2218,6 +2219,114 @@ class ConstrainedBox extends SingleChildRenderObjectWidget { } } +/// A widget that applies an arbitary transform to its constraints, and sizes +/// its child using the resulting [BoxConstraints], treating any overflow +/// as error. +/// +/// This container sizes its child using a [BoxConstraints] created by applying +/// [constraintsTransform] to its own constraints. This container will then +/// attempt to adopt the same size, within the limits of its own constraints. If +/// it ends up with a different size, it will align the child based on +/// [alignment]. If the container cannot expand enough to accommodate the entire +/// child, the child will be clipped if [clipBehavior] is not [Clip.none]. +/// +/// In debug mode, if the child overflows the container, a warning will be +/// printed on the console, and black and yellow striped areas will appear where +/// the overflow occurs. +/// +/// {@tool snippet} +/// This snippet guarantees the child [Card] will be at least as tall as its +/// intrinsic height. Unlike [UnconstrainedBox], the child can be taller if the +/// the parent's minHeight is constrained. If parent container's maxHeight is +/// less than [Card]'s intrinsic height, in debug mode a warning will be given. +/// +/// ```dart +/// ConstraintsTransformBox( +/// constraintsTransform: (BoxConstraints constraints) => constraints.copyWith(maxHeight: double.infinity), +/// child: const Card(child: Text('Hello World!')), +/// ) +/// ``` +/// {@end-tool} +/// +/// See also: +/// +/// * [ConstrainedBox], which renders a box which imposes constraints +/// on its child. +/// * [OverflowBox], a widget that imposes additional constraints on its child, +/// and allows the child to overflow itself. +/// * [UnconstrainedBox] which allows its children to render themselves +/// unconstrained, expands to fit them, and considers overflow to be an error. +class ConstraintsTransformBox extends SingleChildRenderObjectWidget { + /// Creates a widget that uses a function to transform the constraints it + /// passes to its child. If the child overflows the parent's constraints, a + /// warning will be given in debug mode. + /// + /// The `alignment`, `clipBehavior` and `constraintsTransform` arguments must + /// not be null. + const ConstraintsTransformBox({ + Key key, + Widget child, + this.textDirection, + this.alignment = Alignment.center, + this.constraintsTransform, + this.clipBehavior = Clip.hardEdge, + }) : assert(alignment != null), + assert(clipBehavior != null), + assert(constraintsTransform != null), + super(key: key, child: child); + + /// The text direction to use when interpreting the [alignment] if it is an + /// [AlignmentDirectional]. + /// + /// Defaults to null, in which case [Directionality.of] is used to determine + /// the text direction. + final TextDirection textDirection; + + /// The alignment to use when laying out the child, if it has a different size + /// than this widget. + /// + /// If this is an [AlignmentDirectional], then [textDirection] must not be + /// null. + /// + /// See also: + /// + /// * [Alignment] for non-[Directionality]-aware alignments. + /// * [AlignmentDirectional] for [Directionality]-aware alignments. + final AlignmentGeometry alignment; + + /// @{template flutter.widgets.constraintsTransform} + /// The function used to transform the incoming [BoxConstraints], to impose on + /// [child]. + /// + /// Must not be null. The function must return a non-null and normalized + /// [BoxConstraints]. + /// @{endtemplate} + final BoxConstraintsTransform constraintsTransform; + + // TODO(liyuqian): defaults to [Clip.none] once Google references are updated. + /// {@macro flutter.widgets.Clip} + /// + /// Defaults to [Clip.hardEdge]. + final Clip clipBehavior; + + @override + void updateRenderObject(BuildContext context, covariant RenderConstraintsTransformBox renderObject) { + renderObject + ..textDirection = textDirection ?? Directionality.of(context) + ..constraintsTransform = constraintsTransform + ..alignment = alignment + ..clipBehavior = clipBehavior; + } + + @override + RenderConstraintsTransformBox createRenderObject(BuildContext context) => RenderConstraintsTransformBox( + textDirection: textDirection ?? Directionality.of(context), + alignment: alignment, + constraintsTransform: constraintsTransform, + clipBehavior: clipBehavior, + ); +} + /// A widget that imposes no constraints on its child, allowing it to render /// at its "natural" size. /// @@ -2242,20 +2351,20 @@ class ConstrainedBox extends SingleChildRenderObjectWidget { /// * [OverflowBox], a widget that imposes different constraints on its child /// than it gets from its parent, possibly allowing the child to overflow /// the parent. -class UnconstrainedBox extends SingleChildRenderObjectWidget { +class UnconstrainedBox extends StatelessWidget { /// Creates a widget that imposes no constraints on its child, allowing it to /// render at its "natural" size. If the child overflows the parents /// constraints, a warning will be given in debug mode. const UnconstrainedBox({ Key key, - Widget child, + this.child, this.textDirection, this.alignment = Alignment.center, this.constrainedAxis, this.clipBehavior = Clip.hardEdge, }) : assert(alignment != null), assert(clipBehavior != null), - super(key: key, child: child); + super(key: key); /// The text direction to use when interpreting the [alignment] if it is an /// [AlignmentDirectional]. @@ -2286,22 +2395,39 @@ class UnconstrainedBox extends SingleChildRenderObjectWidget { /// Defaults to [Clip.hardEdge]. final Clip clipBehavior; - @override - void updateRenderObject(BuildContext context, covariant RenderUnconstrainedBox renderObject) { - renderObject - ..textDirection = textDirection ?? Directionality.of(context) - ..alignment = alignment - ..constrainedAxis = constrainedAxis - ..clipBehavior = clipBehavior; - } + /// The widget below this widget in the tree. + /// + /// {@macro flutter.widgets.child} + final Widget child; + + static BoxConstraints _unconstrained(BoxConstraints constraints) => const BoxConstraints(); + static BoxConstraints _widthConstrained(BoxConstraints constraints) => constraints.widthConstraints(); + static BoxConstraints _heightConstrained(BoxConstraints constraints) => constraints.heightConstraints(); @override - RenderUnconstrainedBox createRenderObject(BuildContext context) => RenderUnconstrainedBox( - textDirection: textDirection ?? Directionality.of(context), - alignment: alignment, - constrainedAxis: constrainedAxis, - clipBehavior: clipBehavior, - ); + Widget build(BuildContext context) { + BoxConstraintsTransform constraintsTransform; + + if (constrainedAxis != null) { + switch (constrainedAxis) { + case Axis.horizontal: + constraintsTransform = _widthConstrained; + break; + case Axis.vertical: + constraintsTransform = _heightConstrained; + break; + } + } + constraintsTransform ??= _unconstrained; + + return ConstraintsTransformBox( + child: child, + textDirection: textDirection ?? Directionality.of(context), + alignment: alignment, + clipBehavior: clipBehavior, + constraintsTransform: constraintsTransform, + ); + } @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 4dc8498740..745870124f 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2392,6 +2392,9 @@ class EditableTextState extends State with AutomaticKeepAliveClien : null; } + static BoxConstraints _unmodified(BoxConstraints constraints) => constraints; + static BoxConstraints _removeMaxHeightConstraint(BoxConstraints constraints) => constraints.copyWith(maxHeight: double.infinity); + @override Widget build(BuildContext context) { assert(debugCheckHasMediaQuery(context)); @@ -2399,74 +2402,80 @@ class EditableTextState extends State with AutomaticKeepAliveClien super.build(context); // See AutomaticKeepAliveClientMixin. final TextSelectionControls controls = widget.selectionControls; - return MouseRegion( - cursor: widget.mouseCursor ?? SystemMouseCursors.text, - child: Scrollable( - excludeFromSemantics: true, - axisDirection: _isMultiline ? AxisDirection.down : AxisDirection.right, - controller: _scrollController, - physics: widget.scrollPhysics, - dragStartBehavior: widget.dragStartBehavior, - restorationId: widget.restorationId, - viewportBuilder: (BuildContext context, ViewportOffset offset) { - return CompositedTransformTarget( - link: _toolbarLayerLink, - child: Semantics( - onCopy: _semanticsOnCopy(controls), - onCut: _semanticsOnCut(controls), - onPaste: _semanticsOnPaste(controls), - child: _Editable( - key: _editableKey, - startHandleLayerLink: _startHandleLayerLink, - endHandleLayerLink: _endHandleLayerLink, - textSpan: buildTextSpan(), - value: _value, - cursorColor: _cursorColor, - backgroundCursorColor: widget.backgroundCursorColor, - showCursor: EditableText.debugDeterministicCursor - ? ValueNotifier(widget.showCursor) - : _cursorVisibilityNotifier, - forceLine: widget.forceLine, - readOnly: widget.readOnly, - hasFocus: _hasFocus, - maxLines: widget.maxLines, - minLines: widget.minLines, - expands: widget.expands, - strutStyle: widget.strutStyle, - selectionColor: widget.selectionColor, - textScaleFactor: widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context), - textAlign: widget.textAlign, - textDirection: _textDirection, - locale: widget.locale, - textHeightBehavior: widget.textHeightBehavior ?? DefaultTextHeightBehavior.of(context), - textWidthBasis: widget.textWidthBasis, - obscuringCharacter: widget.obscuringCharacter, - obscureText: widget.obscureText, - autocorrect: widget.autocorrect, - smartDashesType: widget.smartDashesType, - smartQuotesType: widget.smartQuotesType, - enableSuggestions: widget.enableSuggestions, - offset: offset, - onSelectionChanged: _handleSelectionChanged, - onCaretChanged: _handleCaretChanged, - rendererIgnoresPointer: widget.rendererIgnoresPointer, - cursorWidth: widget.cursorWidth, - cursorHeight: widget.cursorHeight, - cursorRadius: widget.cursorRadius, - cursorOffset: widget.cursorOffset, - selectionHeightStyle: widget.selectionHeightStyle, - selectionWidthStyle: widget.selectionWidthStyle, - paintCursorAboveText: widget.paintCursorAboveText, - enableInteractiveSelection: widget.enableInteractiveSelection, - textSelectionDelegate: this, - devicePixelRatio: _devicePixelRatio, - promptRectRange: _currentPromptRectRange, - promptRectColor: widget.autocorrectionTextRectColor, - clipBehavior: widget.clipBehavior, + final bool ignoreOverflow = _isMultiline || widget.clipBehavior == Clip.none; + + return ConstraintsTransformBox( + constraintsTransform: ignoreOverflow ? _unmodified : _removeMaxHeightConstraint, + clipBehavior: widget.clipBehavior, + child: MouseRegion( + cursor: widget.mouseCursor ?? SystemMouseCursors.text, + child: Scrollable( + excludeFromSemantics: true, + axisDirection: _isMultiline ? AxisDirection.down : AxisDirection.right, + controller: _scrollController, + physics: widget.scrollPhysics, + dragStartBehavior: widget.dragStartBehavior, + restorationId: widget.restorationId, + viewportBuilder: (BuildContext context, ViewportOffset offset) { + return CompositedTransformTarget( + link: _toolbarLayerLink, + child: Semantics( + onCopy: _semanticsOnCopy(controls), + onCut: _semanticsOnCut(controls), + onPaste: _semanticsOnPaste(controls), + child: _Editable( + key: _editableKey, + startHandleLayerLink: _startHandleLayerLink, + endHandleLayerLink: _endHandleLayerLink, + textSpan: buildTextSpan(), + value: _value, + cursorColor: _cursorColor, + backgroundCursorColor: widget.backgroundCursorColor, + showCursor: EditableText.debugDeterministicCursor + ? ValueNotifier(widget.showCursor) + : _cursorVisibilityNotifier, + forceLine: widget.forceLine, + readOnly: widget.readOnly, + hasFocus: _hasFocus, + maxLines: widget.maxLines, + minLines: widget.minLines, + expands: widget.expands, + strutStyle: widget.strutStyle, + selectionColor: widget.selectionColor, + textScaleFactor: widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context), + textAlign: widget.textAlign, + textDirection: _textDirection, + locale: widget.locale, + textHeightBehavior: widget.textHeightBehavior ?? DefaultTextHeightBehavior.of(context), + textWidthBasis: widget.textWidthBasis, + obscuringCharacter: widget.obscuringCharacter, + obscureText: widget.obscureText, + autocorrect: widget.autocorrect, + smartDashesType: widget.smartDashesType, + smartQuotesType: widget.smartQuotesType, + enableSuggestions: widget.enableSuggestions, + offset: offset, + onSelectionChanged: _handleSelectionChanged, + onCaretChanged: _handleCaretChanged, + rendererIgnoresPointer: widget.rendererIgnoresPointer, + cursorWidth: widget.cursorWidth, + cursorHeight: widget.cursorHeight, + cursorRadius: widget.cursorRadius, + cursorOffset: widget.cursorOffset, + selectionHeightStyle: widget.selectionHeightStyle, + selectionWidthStyle: widget.selectionWidthStyle, + paintCursorAboveText: widget.paintCursorAboveText, + enableInteractiveSelection: widget.enableInteractiveSelection, + textSelectionDelegate: this, + devicePixelRatio: _devicePixelRatio, + promptRectRange: _currentPromptRectRange, + promptRectColor: widget.autocorrectionTextRectColor, + clipBehavior: widget.clipBehavior, + ), ), - ), - ); - }, + ); + }, + ), ), ); } diff --git a/packages/flutter/test/rendering/box_test.dart b/packages/flutter/test/rendering/box_test.dart index ac73edde29..c1fe966626 100644 --- a/packages/flutter/test/rendering/box_test.dart +++ b/packages/flutter/test/rendering/box_test.dart @@ -380,6 +380,74 @@ void main() { expect(unconstrained.getMaxIntrinsicWidth(100.0), equals(200.0)); }); + group('ConstraintsTransfromBox', () { + FlutterErrorDetails firstErrorDetails; + void exhaustErrors() { + FlutterErrorDetails next; + do { + next = renderer.takeFlutterErrorDetails(); + firstErrorDetails ??= next; + } while (next != null); + } + + tearDown(() { + firstErrorDetails = null; + }); + + test('throws if the resulting constraints are not normalized', () { + final RenderConstrainedBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(height: 0)); + final RenderConstraintsTransformBox box = RenderConstraintsTransformBox( + alignment: Alignment.center, + textDirection: TextDirection.ltr, + constraintsTransform: (BoxConstraints constraints) => const BoxConstraints(maxHeight: -1, minHeight: 200), + child: child, + ); + + layout(box, constraints: const BoxConstraints(), onErrors: exhaustErrors); + + expect(firstErrorDetails?.toString(), contains('is not normalized')); + }); + + test('overflow is reported when insufficient size is given', () { + final RenderConstrainedBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: double.maxFinite)); + final RenderConstraintsTransformBox box = RenderConstraintsTransformBox( + alignment: Alignment.center, + textDirection: TextDirection.ltr, + constraintsTransform: (BoxConstraints constraints) => constraints.copyWith(maxWidth: double.infinity), + child: child, + ); + + // No error reported. + layout(box, constraints: const BoxConstraints(), phase: EnginePhase.composite, onErrors: expectOverflowedErrors); + }); + + test('throws when null is returned by the transform function', () { + final RenderConstrainedBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200)); + final RenderConstraintsTransformBox box = RenderConstraintsTransformBox( + alignment: Alignment.center, + textDirection: TextDirection.ltr, + constraintsTransform: (BoxConstraints constraints) => null, + child: child, + ); + + layout(box, constraints: const BoxConstraints(), onErrors: exhaustErrors); + expect(firstErrorDetails.toString(), contains('!= null')); + }); + + test("the transform function can't be set to null", () { + final RenderConstrainedBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200)); + final RenderConstraintsTransformBox box = RenderConstraintsTransformBox( + alignment: Alignment.center, + textDirection: TextDirection.ltr, + constraintsTransform: null, + child: child, + ); + + layout(box, constraints: const BoxConstraints(), onErrors: exhaustErrors); + expect(firstErrorDetails.toString(), contains('!= null')); + }); + }); + test ('getMinIntrinsicWidth error handling', () { final RenderUnconstrainedBox unconstrained = RenderUnconstrainedBox( textDirection: TextDirection.ltr, diff --git a/packages/flutter/test/rendering/debug_overflow_indicator_test.dart b/packages/flutter/test/rendering/debug_overflow_indicator_test.dart index 5929fda362..04a26ba070 100644 --- a/packages/flutter/test/rendering/debug_overflow_indicator_test.dart +++ b/packages/flutter/test/rendering/debug_overflow_indicator_test.dart @@ -38,7 +38,7 @@ void main() { final dynamic exception = tester.takeException(); expect(exception, isFlutterError); expect(exception.diagnostics.first.level, DiagnosticLevel.summary); - expect(exception.diagnostics.first.toString(), startsWith('A RenderUnconstrainedBox overflowed by ')); + expect(exception.diagnostics.first.toString(), startsWith('A RenderConstraintsTransformBox overflowed by ')); expect(find.byType(UnconstrainedBox), paints..rect()); await tester.pumpWidget( diff --git a/packages/flutter/test/widgets/basic_test.dart b/packages/flutter/test/widgets/basic_test.dart index bf04033493..c2ddf6874c 100644 --- a/packages/flutter/test/widgets/basic_test.dart +++ b/packages/flutter/test/widgets/basic_test.dart @@ -339,7 +339,7 @@ void main() { testWidgets('UnconstrainedBox can set and update clipBehavior', (WidgetTester tester) async { await tester.pumpWidget(const UnconstrainedBox()); - final RenderUnconstrainedBox renderObject = tester.allRenderObjects.whereType().first; + final RenderConstraintsTransformBox renderObject = tester.allRenderObjects.whereType().first; expect(renderObject.clipBehavior, equals(Clip.hardEdge)); await tester.pumpWidget(const UnconstrainedBox(clipBehavior: Clip.antiAlias)); diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index e51d970906..a6e7a8e01a 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -5342,6 +5342,130 @@ void main() { expectToAssert(const TextEditingValue(text: 'test', composing: TextRange(start: -1, end: 9)), false); }); + testWidgets('shows overflow indicator for single line text with insufficient height', (WidgetTester tester) async { + await tester.pumpWidget(MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: SizedBox( + height: 10, + child: EditableText( + maxLines: 1, + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + )); + + final dynamic exception = tester.takeException(); + expect(exception, isFlutterError); + expect(exception.diagnostics.first.level, DiagnosticLevel.summary); + expect(exception.diagnostics.first.toString(), startsWith('A RenderConstraintsTransformBox overflowed by ')); + + // Can be fixed by given enough height; + await tester.pumpWidget(MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: SizedBox( + height: 10, + child: EditableText( + maxLines: 1, + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + )); + + expect(tester.takeException(), isNull); + }); + + testWidgets('No overflow indicator for multiline line text or no clipping', (WidgetTester tester) async { + await tester.pumpWidget(MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: SizedBox( + height: 10, + child: EditableText( + maxLines: 1, + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + clipBehavior: Clip.none, + ), + ), + ), + ), + )); + + expect(tester.takeException(), isNull); + + await tester.pumpWidget(MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: SizedBox( + height: 50, + child: EditableText( + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + )); + + expect(tester.takeException(), isNull); + }); + + testWidgets('RenderEditable can stretch vertically', (WidgetTester tester) async { + await tester.pumpWidget(MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: SizedBox( + height: 400, + child: EditableText( + maxLines: 1, + backgroundCursorColor: Colors.grey, + controller: controller, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + )); + + final dynamic exception = tester.takeException(); + expect(exception, isNull); + + final EditableTextState state = tester.state(find.byType(EditableText)); + expect(state.renderEditable.size.height, 400); + }); + // Regression test for https://github.com/flutter/flutter/issues/65374. testWidgets('Length formatter will not cause crash while the TextEditingValue is composing', (WidgetTester tester) async { final TextInputFormatter formatter = LengthLimitingTextInputFormatter(5); diff --git a/packages/flutter/test/widgets/text_golden_test.dart b/packages/flutter/test/widgets/text_golden_test.dart index 3f38ee19af..4f15b59eca 100644 --- a/packages/flutter/test/widgets/text_golden_test.dart +++ b/packages/flutter/test/widgets/text_golden_test.dart @@ -632,11 +632,11 @@ void main() { ), children: [ WidgetSpan( - child: SizedBox(width: 70, height: 25, child: TextField()), + child: SizedBox(width: 70, height: 55, child: TextField()), ), TextSpan(text: ', and my favorite city is: ', style: TextStyle(fontSize: 20)), WidgetSpan( - child: SizedBox(width: 70, height: 25, child: TextField()), + child: SizedBox(width: 70, height: 55, child: TextField()), ), ], ), @@ -743,7 +743,7 @@ void main() { ), TextSpan(text: 'outer', style: TextStyle(fontSize: 20)), WidgetSpan( - child: SizedBox(width: 70, height: 25, child: TextField()), + child: SizedBox(width: 70, height: 55, child: TextField()), ), WidgetSpan( child: SizedBox( diff --git a/packages/flutter_driver/test/src/real_tests/extension_test.dart b/packages/flutter_driver/test/src/real_tests/extension_test.dart index b00abea68b..c6e9168f6d 100644 --- a/packages/flutter_driver/test/src/real_tests/extension_test.dart +++ b/packages/flutter_driver/test/src/real_tests/extension_test.dart @@ -570,7 +570,7 @@ void main() { ), ), Container( - height: 25.0, + height: 40.0, child: EditableText( key: const ValueKey('text3'), controller: TextEditingController(text: 'Hello3'), @@ -581,14 +581,14 @@ void main() { ), ), Container( - height: 25.0, + height: 40.0, child: TextField( key: const ValueKey('text4'), controller: TextEditingController(text: 'Hello4'), ), ), Container( - height: 25.0, + height: 40.0, child: TextFormField( key: const ValueKey('text5'), controller: TextEditingController(text: 'Hello5'),