Replace Align shrinkWrap parameter with widthFactor and heightFactor
This commit is contained in:
@@ -33,7 +33,8 @@ class ScrollbarAppState extends State<ScrollbarApp> {
|
|||||||
Widget scrollable = new Container(
|
Widget scrollable = new Container(
|
||||||
margin: new EdgeDims.symmetric(horizontal: 6.0), // TODO(hansmuller) 6.0 should be based on _kScrollbarThumbWidth
|
margin: new EdgeDims.symmetric(horizontal: 6.0), // TODO(hansmuller) 6.0 should be based on _kScrollbarThumbWidth
|
||||||
child: new Center(
|
child: new Center(
|
||||||
shrinkWrap: ShrinkWrap.both,
|
widthFactor: 1.0,
|
||||||
|
heightFactor: 1.0,
|
||||||
child: new Container(
|
child: new Container(
|
||||||
width: 80.0,
|
width: 80.0,
|
||||||
height: _itemExtent * 5.0,
|
height: _itemExtent * 5.0,
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ abstract class MaterialButtonState<T extends MaterialButton> extends State<T> {
|
|||||||
Widget contents = new Container(
|
Widget contents = new Container(
|
||||||
padding: new EdgeDims.symmetric(horizontal: 8.0),
|
padding: new EdgeDims.symmetric(horizontal: 8.0),
|
||||||
child: new Center(
|
child: new Center(
|
||||||
shrinkWrap: ShrinkWrap.width,
|
widthFactor: 1.0,
|
||||||
child: config.child
|
child: config.child
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -344,7 +344,7 @@ class Tab extends StatelessComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Container centeredLabel = new Container(
|
Container centeredLabel = new Container(
|
||||||
child: new Center(child: labelContent, shrinkWrap: ShrinkWrap.both),
|
child: new Center(child: labelContent, widthFactor: 1.0, heightFactor: 1.0),
|
||||||
constraints: new BoxConstraints(minWidth: _kMinTabWidth),
|
constraints: new BoxConstraints(minWidth: _kMinTabWidth),
|
||||||
padding: _kTabLabelPadding
|
padding: _kTabLabelPadding
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -143,13 +143,6 @@ class RenderPadding extends RenderShiftedBox {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ShrinkWrap {
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
both,
|
|
||||||
none
|
|
||||||
}
|
|
||||||
|
|
||||||
class RenderPositionedBox extends RenderShiftedBox {
|
class RenderPositionedBox extends RenderShiftedBox {
|
||||||
|
|
||||||
// This box aligns a child box within itself. It's only useful for
|
// This box aligns a child box within itself. It's only useful for
|
||||||
@@ -161,12 +154,13 @@ class RenderPositionedBox extends RenderShiftedBox {
|
|||||||
RenderPositionedBox({
|
RenderPositionedBox({
|
||||||
RenderBox child,
|
RenderBox child,
|
||||||
FractionalOffset alignment: const FractionalOffset(0.5, 0.5),
|
FractionalOffset alignment: const FractionalOffset(0.5, 0.5),
|
||||||
ShrinkWrap shrinkWrap: ShrinkWrap.none
|
double widthFactor,
|
||||||
|
double heightFactor
|
||||||
}) : _alignment = alignment,
|
}) : _alignment = alignment,
|
||||||
_shrinkWrap = shrinkWrap,
|
_widthFactor = widthFactor,
|
||||||
|
_heightFactor = heightFactor,
|
||||||
super(child) {
|
super(child) {
|
||||||
assert(alignment != null);
|
assert(alignment != null);
|
||||||
assert(shrinkWrap != null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FractionalOffset get alignment => _alignment;
|
FractionalOffset get alignment => _alignment;
|
||||||
@@ -179,31 +173,40 @@ class RenderPositionedBox extends RenderShiftedBox {
|
|||||||
markNeedsLayout();
|
markNeedsLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
ShrinkWrap _shrinkWrap;
|
double _widthFactor;
|
||||||
ShrinkWrap get shrinkWrap => _shrinkWrap;
|
double get widthFactor => _widthFactor;
|
||||||
void set shrinkWrap (ShrinkWrap value) {
|
void set widthFactor (double value) {
|
||||||
assert(value != null);
|
if (_widthFactor == value)
|
||||||
if (_shrinkWrap == value)
|
|
||||||
return;
|
return;
|
||||||
_shrinkWrap = value;
|
_widthFactor = value;
|
||||||
markNeedsLayout();
|
markNeedsLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
// These are only valid during performLayout() and paint(), since they rely on constraints which is only set after layout() is called.
|
double _heightFactor;
|
||||||
bool get _shinkWrapWidth => _shrinkWrap == ShrinkWrap.width || _shrinkWrap == ShrinkWrap.both || constraints.maxWidth == double.INFINITY;
|
double get heightFactor => _heightFactor;
|
||||||
bool get _shinkWrapHeight => _shrinkWrap == ShrinkWrap.height || _shrinkWrap == ShrinkWrap.both || constraints.maxHeight == double.INFINITY;
|
void set heightFactor (double value) {
|
||||||
|
if (_heightFactor == value)
|
||||||
|
return;
|
||||||
|
_heightFactor = value;
|
||||||
|
markNeedsLayout();
|
||||||
|
}
|
||||||
|
|
||||||
void performLayout() {
|
void performLayout() {
|
||||||
|
final bool shrinkWrapWidth = widthFactor != null || constraints.maxWidth == double.INFINITY;
|
||||||
|
final bool shrinkWrapHeight = heightFactor != null || constraints.maxHeight == double.INFINITY;
|
||||||
|
|
||||||
if (child != null) {
|
if (child != null) {
|
||||||
child.layout(constraints.loosen(), parentUsesSize: true);
|
child.layout(constraints.loosen(), parentUsesSize: true);
|
||||||
size = constraints.constrain(new Size(_shinkWrapWidth ? child.size.width : double.INFINITY,
|
final Size desiredSize = new Size(child.size.width * (_widthFactor ?? 1.0),
|
||||||
_shinkWrapHeight ? child.size.height : double.INFINITY));
|
child.size.height * (_heightFactor ?? 1.0));
|
||||||
Offset delta = size - child.size;
|
size = constraints.constrain(new Size(shrinkWrapWidth ? desiredSize.width : double.INFINITY,
|
||||||
|
shrinkWrapHeight ? desiredSize.height : double.INFINITY));
|
||||||
|
final Offset delta = size - desiredSize;
|
||||||
final BoxParentData childParentData = child.parentData;
|
final BoxParentData childParentData = child.parentData;
|
||||||
childParentData.position = (delta.scale(_alignment.x, _alignment.y)).toPoint();
|
childParentData.position = delta.scale(_alignment.x, _alignment.y).toPoint();
|
||||||
} else {
|
} else {
|
||||||
size = constraints.constrain(new Size(_shinkWrapWidth ? 0.0 : double.INFINITY,
|
size = constraints.constrain(new Size(shrinkWrapWidth ? 0.0 : double.INFINITY,
|
||||||
_shinkWrapHeight ? 0.0 : double.INFINITY));
|
shrinkWrapHeight ? 0.0 : double.INFINITY));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ export 'package:flutter/rendering.dart' show
|
|||||||
Rect,
|
Rect,
|
||||||
ScrollDirection,
|
ScrollDirection,
|
||||||
Shape,
|
Shape,
|
||||||
ShrinkWrap,
|
|
||||||
Size,
|
Size,
|
||||||
StyledTextSpan,
|
StyledTextSpan,
|
||||||
TextAlign,
|
TextAlign,
|
||||||
@@ -238,26 +237,27 @@ class Align extends OneChildRenderObjectWidget {
|
|||||||
Align({
|
Align({
|
||||||
Key key,
|
Key key,
|
||||||
this.alignment: const FractionalOffset(0.5, 0.5),
|
this.alignment: const FractionalOffset(0.5, 0.5),
|
||||||
this.shrinkWrap: ShrinkWrap.none,
|
this.widthFactor,
|
||||||
|
this.heightFactor,
|
||||||
Widget child
|
Widget child
|
||||||
}) : super(key: key, child: child) {
|
}) : super(key: key, child: child);
|
||||||
assert(shrinkWrap != null);
|
|
||||||
}
|
|
||||||
|
|
||||||
final FractionalOffset alignment;
|
final FractionalOffset alignment;
|
||||||
final ShrinkWrap shrinkWrap;
|
final double widthFactor;
|
||||||
|
final double heightFactor;
|
||||||
|
|
||||||
RenderPositionedBox createRenderObject() => new RenderPositionedBox(alignment: alignment, shrinkWrap: shrinkWrap);
|
RenderPositionedBox createRenderObject() => new RenderPositionedBox(alignment: alignment, widthFactor: widthFactor, heightFactor: heightFactor);
|
||||||
|
|
||||||
void updateRenderObject(RenderPositionedBox renderObject, Align oldWidget) {
|
void updateRenderObject(RenderPositionedBox renderObject, Align oldWidget) {
|
||||||
renderObject.alignment = alignment;
|
renderObject.alignment = alignment;
|
||||||
renderObject.shrinkWrap = shrinkWrap;
|
renderObject.widthFactor = widthFactor;
|
||||||
|
renderObject.heightFactor = heightFactor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Center extends Align {
|
class Center extends Align {
|
||||||
Center({ Key key, ShrinkWrap shrinkWrap: ShrinkWrap.none, Widget child })
|
Center({ Key key, widthFactor, heightFactor, Widget child })
|
||||||
: super(key: key, shrinkWrap: shrinkWrap, child: child);
|
: super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
|
||||||
}
|
}
|
||||||
|
|
||||||
class CustomOneChildLayout extends OneChildRenderObjectWidget {
|
class CustomOneChildLayout extends OneChildRenderObjectWidget {
|
||||||
|
|||||||
@@ -21,22 +21,49 @@ void main() {
|
|||||||
additionalConstraints: new BoxConstraints.tight(new Size(100.0, 100.0)),
|
additionalConstraints: new BoxConstraints.tight(new Size(100.0, 100.0)),
|
||||||
child: new RenderDecoratedBox(decoration: new BoxDecoration())
|
child: new RenderDecoratedBox(decoration: new BoxDecoration())
|
||||||
);
|
);
|
||||||
RenderPositionedBox positioner = new RenderPositionedBox(child: sizer, shrinkWrap: ShrinkWrap.width);
|
RenderPositionedBox positioner = new RenderPositionedBox(child: sizer, widthFactor: 1.0);
|
||||||
layout(positioner, constraints: new BoxConstraints.loose(new Size(200.0, 200.0)));
|
layout(positioner, constraints: new BoxConstraints.loose(new Size(200.0, 200.0)));
|
||||||
|
|
||||||
expect(positioner.size.width, equals(100.0), reason: "positioner width");
|
expect(positioner.size.width, equals(100.0), reason: "positioner width");
|
||||||
expect(positioner.size.height, equals(200.0), reason: "positioner height");
|
expect(positioner.size.height, equals(200.0), reason: "positioner height");
|
||||||
|
|
||||||
positioner.shrinkWrap = ShrinkWrap.height;
|
positioner.widthFactor = null;
|
||||||
|
positioner.heightFactor = 1.0;
|
||||||
pumpFrame();
|
pumpFrame();
|
||||||
|
|
||||||
expect(positioner.size.width, equals(200.0), reason: "positioner width");
|
expect(positioner.size.width, equals(200.0), reason: "positioner width");
|
||||||
expect(positioner.size.height, equals(100.0), reason: "positioner height");
|
expect(positioner.size.height, equals(100.0), reason: "positioner height");
|
||||||
|
|
||||||
positioner.shrinkWrap = ShrinkWrap.both;
|
positioner.widthFactor = 1.0;
|
||||||
pumpFrame();
|
pumpFrame();
|
||||||
|
|
||||||
expect(positioner.size.width, equals(100.0), reason: "positioner width");
|
expect(positioner.size.width, equals(100.0), reason: "positioner width");
|
||||||
expect(positioner.size.height, equals(100.0), reason: "positioner height");
|
expect(positioner.size.height, equals(100.0), reason: "positioner height");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('RenderPositionedBox width and height factors', () {
|
||||||
|
RenderConstrainedBox sizer = new RenderConstrainedBox(
|
||||||
|
additionalConstraints: new BoxConstraints.tight(new Size(100.0, 100.0)),
|
||||||
|
child: new RenderDecoratedBox(decoration: new BoxDecoration())
|
||||||
|
);
|
||||||
|
RenderPositionedBox positioner = new RenderPositionedBox(child: sizer, widthFactor: 1.0, heightFactor: 0.0);
|
||||||
|
layout(positioner, constraints: new BoxConstraints.loose(new Size(200.0, 200.0)));
|
||||||
|
|
||||||
|
expect(positioner.size.width, equals(100.0));
|
||||||
|
expect(positioner.size.height, equals(0.0));
|
||||||
|
|
||||||
|
positioner.widthFactor = 0.5;
|
||||||
|
positioner.heightFactor = 0.5;
|
||||||
|
pumpFrame();
|
||||||
|
|
||||||
|
expect(positioner.size.width, equals(50.0));
|
||||||
|
expect(positioner.size.height, equals(50.0));
|
||||||
|
|
||||||
|
positioner.widthFactor = null;
|
||||||
|
positioner.heightFactor = null;
|
||||||
|
pumpFrame();
|
||||||
|
|
||||||
|
expect(positioner.size.width, equals(200.0));
|
||||||
|
expect(positioner.size.height, equals(200.0));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user