Commit 715b304e authored by Adam Barth's avatar Adam Barth

Convert Align to use FractionalOffset instead of two doubles

It was confusing as to whether these were fractions or pixels.

Fixes #1622
parent 0ef9da85
...@@ -348,8 +348,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -348,8 +348,7 @@ class CardCollectionState extends State<CardCollection> {
if (_snapToCenter) { if (_snapToCenter) {
Widget indicator = new IgnorePointer( Widget indicator = new IgnorePointer(
child: new Align( child: new Align(
horizontal: 0.0, alignment: const FractionalOffset(0.0, 0.5),
vertical: 0.5,
child: new Container( child: new Container(
height: 1.0, height: 1.0,
decoration: new BoxDecoration(backgroundColor: const Color(0x80FFFFFF)) decoration: new BoxDecoration(backgroundColor: const Color(0x80FFFFFF))
......
...@@ -737,3 +737,27 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare ...@@ -737,3 +737,27 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare
} }
} }
} }
/// 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)';
}
...@@ -863,30 +863,6 @@ class RenderDecoratedBox extends RenderProxyBox { ...@@ -863,30 +863,6 @@ class RenderDecoratedBox extends RenderProxyBox {
String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}decoration:\n${_painter.decoration.toString(prefix + " ")}\n'; 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 /// Applies a transformation before painting its child
class RenderTransform extends RenderProxyBox { class RenderTransform extends RenderProxyBox {
RenderTransform({ RenderTransform({
......
...@@ -155,35 +155,22 @@ class RenderPositionedBox extends RenderShiftedBox { ...@@ -155,35 +155,22 @@ class RenderPositionedBox extends RenderShiftedBox {
RenderPositionedBox({ RenderPositionedBox({
RenderBox child, RenderBox child,
double horizontal: 0.5, FractionalOffset alignment: const FractionalOffset(0.5, 0.5),
double vertical: 0.5,
ShrinkWrap shrinkWrap: ShrinkWrap.none ShrinkWrap shrinkWrap: ShrinkWrap.none
}) : _horizontal = horizontal, }) : _alignment = alignment,
_vertical = vertical,
_shrinkWrap = shrinkWrap, _shrinkWrap = shrinkWrap,
super(child) { super(child) {
assert(horizontal != null); assert(alignment != null);
assert(vertical != null);
assert(shrinkWrap != null); assert(shrinkWrap != null);
} }
double _horizontal; FractionalOffset get alignment => _alignment;
double get horizontal => _horizontal; FractionalOffset _alignment;
void set horizontal (double value) { void set alignment (FractionalOffset newAlignment) {
assert(value != null); assert(newAlignment == null || (newAlignment.x != null && newAlignment.y != null));
if (_horizontal == value) 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; return;
_vertical = value; _alignment = newAlignment;
markNeedsLayout(); markNeedsLayout();
} }
...@@ -208,14 +195,14 @@ class RenderPositionedBox extends RenderShiftedBox { ...@@ -208,14 +195,14 @@ class RenderPositionedBox extends RenderShiftedBox {
_shinkWrapHeight ? child.size.height : double.INFINITY)); _shinkWrapHeight ? child.size.height : double.INFINITY));
Offset delta = size - child.size; Offset delta = size - child.size;
final BoxParentData childParentData = child.parentData; final BoxParentData childParentData = child.parentData;
childParentData.position = (delta.scale(horizontal, vertical)).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(_shinkWrapWidth ? 0.0 : double.INFINITY,
_shinkWrapHeight ? 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 { class RenderBaseline extends RenderShiftedBox {
......
...@@ -237,25 +237,20 @@ class Padding extends OneChildRenderObjectWidget { ...@@ -237,25 +237,20 @@ class Padding extends OneChildRenderObjectWidget {
class Align extends OneChildRenderObjectWidget { class Align extends OneChildRenderObjectWidget {
Align({ Align({
Key key, Key key,
this.horizontal: 0.5, this.alignment: const FractionalOffset(0.5, 0.5),
this.vertical: 0.5,
this.shrinkWrap: ShrinkWrap.none, this.shrinkWrap: ShrinkWrap.none,
Widget child Widget child
}) : super(key: key, child: child) { }) : super(key: key, child: child) {
assert(horizontal != null);
assert(vertical != null);
assert(shrinkWrap != null); assert(shrinkWrap != null);
} }
final double horizontal; final FractionalOffset alignment;
final double vertical;
final ShrinkWrap shrinkWrap; 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) { void updateRenderObject(RenderPositionedBox renderObject, Align oldWidget) {
renderObject.horizontal = horizontal; renderObject.alignment = alignment;
renderObject.vertical = vertical;
renderObject.shrinkWrap = shrinkWrap; renderObject.shrinkWrap = shrinkWrap;
} }
} }
......
...@@ -9,16 +9,14 @@ void main() { ...@@ -9,16 +9,14 @@ void main() {
tester.pumpWidget( tester.pumpWidget(
new Align( new Align(
child: new Container(), child: new Container(),
horizontal: 0.75, alignment: const FractionalOffset(0.75, 0.75)
vertical: 0.75
) )
); );
tester.pumpWidget( tester.pumpWidget(
new Align( new Align(
child: new Container(), child: new Container(),
horizontal: 0.5, alignment: const FractionalOffset(0.5, 0.5)
vertical: 0.5
) )
); );
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment