@@ -533,6 +533,20 @@ class RenderPositionedBox extends RenderAligningShiftedBox {
533533 }
534534}
535535
536+ /// How much space should be occupied by the [OverflowBox] if there is no
537+ /// overflow.
538+ enum OverflowBoxFit {
539+ /// The widget will size itself to be as large as the parent allows.
540+ max,
541+
542+ /// The widget will follow the child's size.
543+ ///
544+ /// More specifically, the render object will size itself to match the size of
545+ /// its child within the constraints of its parent, or as small as the
546+ /// parent allows if no child is set.
547+ deferToChild,
548+ }
549+
536550/// A render object that imposes different constraints on its child than it gets
537551/// from its parent, possibly allowing the child to overflow the parent.
538552///
@@ -571,12 +585,14 @@ class RenderConstrainedOverflowBox extends RenderAligningShiftedBox {
571585 double ? maxWidth,
572586 double ? minHeight,
573587 double ? maxHeight,
588+ OverflowBoxFit fit = OverflowBoxFit .max,
574589 super .alignment,
575590 super .textDirection,
576591 }) : _minWidth = minWidth,
577592 _maxWidth = maxWidth,
578593 _minHeight = minHeight,
579- _maxHeight = maxHeight;
594+ _maxHeight = maxHeight,
595+ _fit = fit;
580596
581597 /// The minimum width constraint to give the child. Set this to null (the
582598 /// default) to use the constraint from the parent instead.
@@ -626,6 +642,24 @@ class RenderConstrainedOverflowBox extends RenderAligningShiftedBox {
626642 markNeedsLayout ();
627643 }
628644
645+ /// The way to size the render object.
646+ ///
647+ /// This only affects scenario when the child does not indeed overflow.
648+ /// If set to [OverflowBoxFit.deferToChild] , the render object will size
649+ /// itself to match the size of its child within the constraints of its
650+ /// parent, or as small as the parent allows if no child is set.
651+ /// If set to [OverflowBoxFit.max] (the default), the
652+ /// render object will size itself to be as large as the parent allows.
653+ OverflowBoxFit get fit => _fit;
654+ OverflowBoxFit _fit;
655+ set fit (OverflowBoxFit value) {
656+ if (_fit == value) {
657+ return ;
658+ }
659+ _fit = value;
660+ markNeedsLayoutForSizedByParentChange ();
661+ }
662+
629663 BoxConstraints _getInnerConstraints (BoxConstraints constraints) {
630664 return BoxConstraints (
631665 minWidth: _minWidth ?? constraints.minWidth,
@@ -636,19 +670,46 @@ class RenderConstrainedOverflowBox extends RenderAligningShiftedBox {
636670 }
637671
638672 @override
639- bool get sizedByParent => true ;
673+ bool get sizedByParent {
674+ switch (fit) {
675+ case OverflowBoxFit .max:
676+ return true ;
677+ case OverflowBoxFit .deferToChild:
678+ // If deferToChild, the size will be as small as its child when non-overflowing,
679+ // thus it cannot be sizedByParent.
680+ return false ;
681+ }
682+ }
640683
641684 @override
642685 @protected
643686 Size computeDryLayout (covariant BoxConstraints constraints) {
644- return constraints.biggest;
687+ switch (fit) {
688+ case OverflowBoxFit .max:
689+ return constraints.biggest;
690+ case OverflowBoxFit .deferToChild:
691+ return child? .getDryLayout (constraints) ?? constraints.smallest;
692+ }
645693 }
646694
647695 @override
648696 void performLayout () {
649697 if (child != null ) {
650- child? .layout (_getInnerConstraints (constraints), parentUsesSize: true );
698+ child! .layout (_getInnerConstraints (constraints), parentUsesSize: true );
699+ switch (fit) {
700+ case OverflowBoxFit .max:
701+ assert (sizedByParent);
702+ case OverflowBoxFit .deferToChild:
703+ size = constraints.constrain (child! .size);
704+ }
651705 alignChild ();
706+ } else {
707+ switch (fit) {
708+ case OverflowBoxFit .max:
709+ assert (sizedByParent);
710+ case OverflowBoxFit .deferToChild:
711+ size = constraints.smallest;
712+ }
652713 }
653714 }
654715
@@ -659,6 +720,7 @@ class RenderConstrainedOverflowBox extends RenderAligningShiftedBox {
659720 properties.add (DoubleProperty ('maxWidth' , maxWidth, ifNull: 'use parent maxWidth constraint' ));
660721 properties.add (DoubleProperty ('minHeight' , minHeight, ifNull: 'use parent minHeight constraint' ));
661722 properties.add (DoubleProperty ('maxHeight' , maxHeight, ifNull: 'use parent maxHeight constraint' ));
723+ properties.add (EnumProperty <OverflowBoxFit >('fit' , fit));
662724 }
663725}
664726
0 commit comments