Commit b58f850e authored by Eric Seidel's avatar Eric Seidel

Add a Widget wrapper around Grid and test RenderGrid

@abarth
parent 0d4b9970
......@@ -10,11 +10,14 @@ class GridParentData extends BoxParentData with ContainerParentDataMixin<RenderB
class GridMetrics {
// Grid is width-in, height-out. We fill the max width and adjust height
// accordingly.
factory GridMetrics({ double width, int childCount, double childExtent }) {
factory GridMetrics({ double width, int childCount, double maxChildExtent }) {
assert(width != null);
assert(childCount != null);
assert(childExtent != null);
int childrenPerRow = (width / childExtent).floor() + 1;
assert(maxChildExtent != null);
double childExtent = maxChildExtent;
int childrenPerRow = (width / childExtent).floor();
// If the child extent divides evenly into the width use that, otherwise + 1
if (width / childExtent != childrenPerRow.toDouble()) childrenPerRow += 1;
double totalPadding = 0.0;
if (childrenPerRow * childExtent > width) {
// TODO(eseidel): We should snap to pixel bounderies.
......@@ -50,6 +53,14 @@ class RenderGrid extends RenderBox with ContainerRenderObjectMixin<RenderBox, Gr
double _maxChildExtent;
bool _hasVisualOverflow = false;
double get maxChildExtent => _maxChildExtent;
void set maxChildExtent (double value) {
if (_maxChildExtent != value) {
_maxChildExtent = value;
markNeedsLayout();
}
}
void setupParentData(RenderBox child) {
if (child.parentData is! GridParentData)
child.parentData = new GridParentData();
......@@ -93,7 +104,7 @@ class RenderGrid extends RenderBox with ContainerRenderObjectMixin<RenderBox, Gr
return new GridMetrics(
width: constraints.maxWidth,
childCount: childCount,
childExtent: _maxChildExtent
maxChildExtent: _maxChildExtent
);
}
......
......@@ -14,6 +14,7 @@ import 'package:sky/painting/paragraph_painter.dart';
import 'package:sky/rendering/block.dart';
import 'package:sky/rendering/box.dart';
import 'package:sky/rendering/flex.dart';
import 'package:sky/rendering/grid.dart';
import 'package:sky/rendering/image.dart';
import 'package:sky/rendering/object.dart';
import 'package:sky/rendering/paragraph.dart';
......@@ -454,6 +455,21 @@ class Stack extends MultiChildRenderObjectWrapper {
RenderStack get renderObject => super.renderObject;
}
class Grid extends MultiChildRenderObjectWrapper {
Grid(List<Widget> children, { Key key, this.maxChildExtent })
: super(key: key, children: children);
final double maxChildExtent;
RenderGrid createNode() => new RenderGrid(maxChildExtent: maxChildExtent);
RenderGrid get renderObject => super.renderObject;
void syncRenderObject(Widget old) {
super.syncRenderObject(old);
renderObject.maxChildExtent = maxChildExtent;
}
}
class Positioned extends ParentDataNode {
Positioned({
Key key,
......@@ -495,7 +511,6 @@ class Flex extends MultiChildRenderObjectWrapper {
renderObject.alignItems = alignItems;
renderObject.textBaseline = textBaseline;
}
}
class Row extends Flex {
......
import 'package:sky/rendering.dart';
import 'package:test/test.dart';
import 'rendering_tester.dart';
void main() {
test('Basic grid layout test', () {
List<RenderBox> children = [
new RenderDecoratedBox(decoration: new BoxDecoration()),
new RenderDecoratedBox(decoration: new BoxDecoration()),
new RenderDecoratedBox(decoration: new BoxDecoration()),
new RenderDecoratedBox(decoration: new BoxDecoration())
];
RenderBox grid = new RenderGrid(children: children, maxChildExtent: 100.0);
RenderingTester tester = layout(grid, constraints: const BoxConstraints(maxWidth: 200.0));
children.forEach((child) {
expect(child.size.width, equals(100.0), reason: "child width");
expect(child.size.height, equals(100.0), reason: "child height");
});
expect(grid.size.width, equals(200.0), reason: "grid width");
expect(grid.size.height, equals(200.0), reason: "grid height");
expect(grid.needsLayout, equals(false));
grid.maxChildExtent = 60.0;
expect(grid.needsLayout, equals(true));
tester.pumpFrame(phase: EnginePhase.layout);
children.forEach((child) {
expect(child.size.width, equals(50.0), reason: "child width");
expect(child.size.height, equals(50.0), reason: "child height");
});
expect(grid.size.width, equals(200.0), reason: "grid width");
expect(grid.size.height, equals(50.0), reason: "grid height");
});
}
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