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> {
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))
......
......@@ -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 {
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({
......
......@@ -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)
return;
_horizontal = value;
markNeedsLayout();
}
double _vertical;
double get vertical => _vertical;
void set vertical (double value) {
assert(value != null);
if (_vertical == 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;
_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 {
......
......@@ -237,25 +237,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;
}
}
......
......@@ -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)
)
);
});
......
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