1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/rendering.dart';
import '../flutter_test_alternative.dart';
import 'rendering_tester.dart';
class RenderFixedSize extends RenderBox {
double dimension = 100.0;
void grow() {
dimension *= 2.0;
markNeedsLayout();
}
@override
double computeMinIntrinsicWidth(double height) => dimension;
@override
double computeMaxIntrinsicWidth(double height) => dimension;
@override
double computeMinIntrinsicHeight(double width) => dimension;
@override
double computeMaxIntrinsicHeight(double width) => dimension;
@override
void performLayout() {
size = Size.square(dimension);
}
}
class RenderParentSize extends RenderProxyBox {
RenderParentSize({ required RenderBox child }) : super(child);
@override
bool get sizedByParent => true;
@override
void performResize() {
size = constraints.biggest;
}
@override
void performLayout() {
child!.layout(constraints);
}
}
class RenderIntrinsicSize extends RenderProxyBox {
RenderIntrinsicSize({ required RenderBox child }) : super(child);
@override
void performLayout() {
child!.layout(constraints);
size = Size(
child!.getMinIntrinsicWidth(double.infinity),
child!.getMinIntrinsicHeight(double.infinity),
);
}
}
class RenderInvalidIntrinsics extends RenderBox {
@override
bool get sizedByParent => true;
@override
double computeMinIntrinsicWidth(double height) => -1;
@override
double computeMaxIntrinsicWidth(double height) => -1;
@override
double computeMinIntrinsicHeight(double width) => -1;
@override
double computeMaxIntrinsicHeight(double width) => -1;
@override
Size computeDryLayout(BoxConstraints constraints) => Size.zero;
}
void main() {
test('Whether using intrinsics means you get hooked into layout', () {
RenderBox root;
RenderFixedSize inner;
layout(
root = RenderIntrinsicSize(
child: RenderParentSize(
child: inner = RenderFixedSize()
)
),
constraints: const BoxConstraints(
minWidth: 0.0,
minHeight: 0.0,
maxWidth: 1000.0,
maxHeight: 1000.0,
),
);
expect(root.size, equals(inner.size));
inner.grow();
pumpFrame();
expect(root.size, equals(inner.size));
});
test('Parent returns correct intrinsics', () {
RenderParentSize parent;
RenderFixedSize inner;
layout(
RenderIntrinsicSize(
child: parent = RenderParentSize(
child: inner = RenderFixedSize()
)
),
constraints: const BoxConstraints(
minWidth: 0.0,
minHeight: 0.0,
maxWidth: 1000.0,
maxHeight: 1000.0,
),
);
_expectIntrinsicDimensions(parent, 100);
inner.grow();
pumpFrame();
_expectIntrinsicDimensions(parent, 200);
});
test('Intrinsic checks are turned on', () async {
final List<FlutterErrorDetails> errorDetails = <FlutterErrorDetails>[];
layout(RenderInvalidIntrinsics(),
constraints: const BoxConstraints(
minWidth: 0.0,
minHeight: 0.0,
maxWidth: 1000.0,
maxHeight: 1000.0,
), onErrors: () {
errorDetails.addAll(renderer.takeAllFlutterErrorDetails());
});
expect(errorDetails, isNotEmpty);
expect(
errorDetails.map((FlutterErrorDetails details) => details.toString()),
everyElement(contains('violate the intrinsic protocol')),
);
});
}
/// Asserts that all unbounded intrinsic dimensions for [object] match
/// [dimension].
void _expectIntrinsicDimensions(RenderBox object, double dimension) {
expect(object.getMinIntrinsicWidth(double.infinity), equals(dimension));
expect(object.getMaxIntrinsicWidth(double.infinity), equals(dimension));
expect(object.getMinIntrinsicHeight(double.infinity), equals(dimension));
expect(object.getMaxIntrinsicHeight(double.infinity), equals(dimension));
}