Skip to content

Commit

Permalink
Add a Widget wrapper around Grid and test RenderGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidelGoogle committed Aug 27, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 7051cf7 commit 6a5e0aa
Showing 3 changed files with 71 additions and 5 deletions.
19 changes: 15 additions & 4 deletions sky/packages/sky/lib/rendering/grid.dart
Original file line number Diff line number Diff line change
@@ -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
);
}

17 changes: 16 additions & 1 deletion sky/packages/sky/lib/widgets/basic.dart
Original file line number Diff line number Diff line change
@@ -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 {
40 changes: 40 additions & 0 deletions sky/unit/test/rendering/grid_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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");
});
}

0 comments on commit 6a5e0aa

Please sign in to comment.