mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Add a Widget wrapper around Grid and test RenderGrid
@abarth
This commit is contained in:
parent
7051cf7c9e
commit
6a5e0aac93
@ -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 {
|
||||
|
||||
40
sky/unit/test/rendering/grid_test.dart
Normal file
40
sky/unit/test/rendering/grid_test.dart
Normal file
@ -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");
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user