diff --git a/examples/widgets/card_collection.dart b/examples/widgets/card_collection.dart index fd2d17a047..f371f24139 100644 --- a/examples/widgets/card_collection.dart +++ b/examples/widgets/card_collection.dart @@ -348,8 +348,7 @@ class CardCollectionState extends State { if (_snapToCenter) { Widget indicator = new IgnorePointer( child: new Align( - horizontal: 0.0, - vertical: 0.5, + alignment: const FractionalOffset(0.0, 0.5), child: new Container( height: 1.0, decoration: new BoxDecoration(backgroundColor: const Color(0x80FFFFFF)) diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 2341e818fa..486aac8954 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -737,3 +737,27 @@ abstract class RenderBoxContainerDefaultsMixin '$runtimeType($x, $y)'; +} diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index ecace249a8..c5bccf96e0 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -824,30 +824,6 @@ class RenderDecoratedBox extends RenderProxyBox { String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}decoration:\n${_painter.decoration.toString(prefix + " ")}\n'; } -/// An offset that's expressed as a fraction of a Size. -/// -/// FractionalOffset(0.0, 0.0) represents the top left of the Size, -/// FractionalOffset(1.0, 1.0) represents the bottom right of the Size. -class FractionalOffset { - const FractionalOffset(this.x, this.y); - final double x; - final double y; - bool operator ==(dynamic other) { - if (other is! FractionalOffset) - return false; - final FractionalOffset typedOther = other; - return x == typedOther.x && - y == typedOther.y; - } - int get hashCode { - int value = 373; - value = 37 * value + x.hashCode; - value = 37 * value + y.hashCode; - return value; - } - String toString() => '$runtimeType($x, $y)'; -} - /// Applies a transformation before painting its child class RenderTransform extends RenderProxyBox { RenderTransform({ diff --git a/packages/flutter/lib/src/rendering/shifted_box.dart b/packages/flutter/lib/src/rendering/shifted_box.dart index 89eacfb5ce..581fbbdbec 100644 --- a/packages/flutter/lib/src/rendering/shifted_box.dart +++ b/packages/flutter/lib/src/rendering/shifted_box.dart @@ -155,35 +155,22 @@ class RenderPositionedBox extends RenderShiftedBox { RenderPositionedBox({ RenderBox child, - double horizontal: 0.5, - double vertical: 0.5, + FractionalOffset alignment: const FractionalOffset(0.5, 0.5), ShrinkWrap shrinkWrap: ShrinkWrap.none - }) : _horizontal = horizontal, - _vertical = vertical, + }) : _alignment = alignment, _shrinkWrap = shrinkWrap, super(child) { - assert(horizontal != null); - assert(vertical != null); + assert(alignment != null); assert(shrinkWrap != null); } - double _horizontal; - double get horizontal => _horizontal; - void set horizontal (double value) { - assert(value != null); - if (_horizontal == value) + FractionalOffset get alignment => _alignment; + FractionalOffset _alignment; + void set alignment (FractionalOffset newAlignment) { + assert(newAlignment == null || (newAlignment.x != null && newAlignment.y != null)); + if (_alignment == newAlignment) return; - _horizontal = value; - markNeedsLayout(); - } - - double _vertical; - double get vertical => _vertical; - void set vertical (double value) { - assert(value != null); - if (_vertical == value) - return; - _vertical = value; + _alignment = newAlignment; markNeedsLayout(); } @@ -208,14 +195,14 @@ class RenderPositionedBox extends RenderShiftedBox { _shinkWrapHeight ? child.size.height : double.INFINITY)); Offset delta = size - child.size; final BoxParentData childParentData = child.parentData; - childParentData.position = (delta.scale(horizontal, vertical)).toPoint(); + childParentData.position = (delta.scale(_alignment.x, _alignment.y)).toPoint(); } else { size = constraints.constrain(new Size(_shinkWrapWidth ? 0.0 : double.INFINITY, _shinkWrapHeight ? 0.0 : double.INFINITY)); } } - String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}horizontal: $horizontal\n${prefix}vertical: $vertical\n'; + String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}alignment: $alignment\n'; } class RenderBaseline extends RenderShiftedBox { diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index f1e0dd1cc6..374f82b843 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -219,25 +219,20 @@ class Padding extends OneChildRenderObjectWidget { class Align extends OneChildRenderObjectWidget { Align({ Key key, - this.horizontal: 0.5, - this.vertical: 0.5, + this.alignment: const FractionalOffset(0.5, 0.5), this.shrinkWrap: ShrinkWrap.none, Widget child }) : super(key: key, child: child) { - assert(horizontal != null); - assert(vertical != null); assert(shrinkWrap != null); } - final double horizontal; - final double vertical; + final FractionalOffset alignment; final ShrinkWrap shrinkWrap; - RenderPositionedBox createRenderObject() => new RenderPositionedBox(horizontal: horizontal, vertical: vertical, shrinkWrap: shrinkWrap); + RenderPositionedBox createRenderObject() => new RenderPositionedBox(alignment: alignment, shrinkWrap: shrinkWrap); void updateRenderObject(RenderPositionedBox renderObject, Align oldWidget) { - renderObject.horizontal = horizontal; - renderObject.vertical = vertical; + renderObject.alignment = alignment; renderObject.shrinkWrap = shrinkWrap; } } diff --git a/packages/unit/test/widget/align_test.dart b/packages/unit/test/widget/align_test.dart index 48343cba4d..a66c2db6be 100644 --- a/packages/unit/test/widget/align_test.dart +++ b/packages/unit/test/widget/align_test.dart @@ -9,16 +9,14 @@ void main() { tester.pumpWidget( new Align( child: new Container(), - horizontal: 0.75, - vertical: 0.75 + alignment: const FractionalOffset(0.75, 0.75) ) ); tester.pumpWidget( new Align( child: new Container(), - horizontal: 0.5, - vertical: 0.5 + alignment: const FractionalOffset(0.5, 0.5) ) ); });