sliver_container.dart
11.2 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
@immutable
class SliverContainer extends RenderObjectWidget {
SliverContainer(
{Key key,
this.padding = EdgeInsets.zero,
this.margin = EdgeInsets.zero,
this.borderRadius,
this.decoration,
this.foregroundDecoration,
this.sliver})
: super(key: key);
final EdgeInsets margin;
final BorderRadius borderRadius;
final EdgeInsets padding;
final Decoration decoration;
final Decoration foregroundDecoration;
final Widget sliver;
Widget get child => SliverPadding(padding: padding + margin, sliver: sliver);
Container get backgroundDecorationBox => Container(decoration: decoration);
Container get foregroundDecorationBox =>
Container(decoration: foregroundDecoration);
@override
_RenderSliverGroup createRenderObject(BuildContext context) {
return _RenderSliverGroup(
margin: this.margin, borderRadius: this.borderRadius, padding: padding);
}
@override
_SliverGroupElement createElement() => _SliverGroupElement(this);
@override
void updateRenderObject(
BuildContext context, _RenderSliverGroup renderObject) {
renderObject
..margin = margin
..padding = padding
..borderRadius = borderRadius;
}
}
class _SliverGroupElement extends RenderObjectElement {
_SliverGroupElement(SliverContainer widget) : super(widget);
Element _backgroundDecoration;
Element _foregroundDecoration;
Element _sliver;
@override
SliverContainer get widget => super.widget;
@override
void visitChildren(ElementVisitor visitor) {
if (_backgroundDecoration != null) visitor(_backgroundDecoration);
if (_foregroundDecoration != null) visitor(_foregroundDecoration);
if (_sliver != null) visitor(_sliver);
}
@override
void forgetChild(Element child) {
if (child == _backgroundDecoration) _backgroundDecoration = null;
if (child == _foregroundDecoration) _foregroundDecoration = null;
if (child == _sliver) _sliver = null;
}
@override
void mount(Element parent, newSlot) {
super.mount(parent, newSlot);
_backgroundDecoration =
updateChild(_backgroundDecoration, widget.backgroundDecorationBox, 0);
_foregroundDecoration =
updateChild(_foregroundDecoration, widget.foregroundDecorationBox, 1);
_sliver = updateChild(_sliver, widget.child, 2);
}
@override
void update(RenderObjectWidget newWidget) {
super.update(newWidget);
assert(widget == newWidget);
_backgroundDecoration =
updateChild(_backgroundDecoration, widget.backgroundDecorationBox, 0);
_foregroundDecoration =
updateChild(_foregroundDecoration, widget.foregroundDecorationBox, 1);
_sliver = updateChild(_sliver, widget.child, 2);
}
@override
void insertChildRenderObject(RenderObject child, int slot) {
final _RenderSliverGroup renderObject = this.renderObject;
if (slot == 0) renderObject.decoration = child;
if (slot == 1) renderObject.foregroundDecoration = child;
if (slot == 2) renderObject.child = child;
assert(renderObject == this.renderObject);
}
@override
void moveChildRenderObject(RenderObject child, slot) {
assert(false);
}
@override
void removeChildRenderObject(RenderObject child) {
final _RenderSliverGroup renderObject = this.renderObject;
if (renderObject.decoration == child) renderObject.decoration = null;
if (renderObject.foregroundDecoration == child)
renderObject.foregroundDecoration = null;
if (renderObject.child == child) renderObject.child = null;
assert(renderObject == this.renderObject);
}
}
class _RenderSliverGroup extends RenderSliver with RenderSliverHelpers {
_RenderSliverGroup(
{EdgeInsetsGeometry margin,
EdgeInsetsGeometry padding,
BorderRadius borderRadius,
RenderBox decoration,
RenderBox foregroundDecoration,
RenderSliver child}) {
this.margin = margin;
this.padding = padding;
this.borderRadius = borderRadius;
this.foregroundDecoration = foregroundDecoration;
this.decoration = decoration;
this.child = child;
}
RRect _clipRRect;
EdgeInsetsGeometry get padding => _padding;
EdgeInsetsGeometry _padding;
set padding(EdgeInsetsGeometry value) {
assert(value != null);
assert(value.isNonNegative);
if (_padding == value) return;
_padding = value;
markNeedsLayout();
}
EdgeInsetsGeometry get margin => _margin;
EdgeInsetsGeometry _margin;
set margin(EdgeInsetsGeometry value) {
assert(value != null);
assert(value.isNonNegative);
if (_margin == value) return;
_margin = value;
markNeedsLayout();
}
BorderRadiusGeometry get borderRadius => _borderRadius;
BorderRadiusGeometry _borderRadius;
set borderRadius(BorderRadiusGeometry value) {
if (value == _borderRadius) return;
_borderRadius = value;
markNeedsPaint();
}
RenderBox get decoration => _decoration;
RenderBox _decoration;
set decoration(RenderBox value) {
if (_decoration != null) dropChild(_decoration);
_decoration = value;
if (_decoration != null) adoptChild(_decoration);
}
RenderBox get foregroundDecoration => _foregroundDecoration;
RenderBox _foregroundDecoration;
set foregroundDecoration(RenderBox value) {
if (_foregroundDecoration != null) dropChild(_foregroundDecoration);
_foregroundDecoration = value;
if (_foregroundDecoration != null) adoptChild(_foregroundDecoration);
}
RenderSliver get child => _child;
RenderSliver _child;
set child(RenderSliver value) {
if (_child != null) dropChild(_child);
_child = value;
if (_child != null) adoptChild(_child);
}
@override
void setupParentData(RenderObject child) {
if (child.parentData is! SliverPhysicalParentData)
child.parentData = new SliverPhysicalParentData();
}
@override
void attach(PipelineOwner owner) {
super.attach(owner);
if (_decoration != null) _decoration.attach(owner);
if (_child != null) _child.attach(owner);
if (_foregroundDecoration != null) _foregroundDecoration.attach(owner);
}
@override
void detach() {
super.detach();
if (_decoration != null) _decoration.detach();
if (_child != null) _child.detach();
if (_foregroundDecoration != null) _foregroundDecoration.detach();
}
@override
void redepthChildren() {
if (_decoration != null) redepthChild(_decoration);
if (_child != null) redepthChild(_child);
if (_foregroundDecoration != null) redepthChild(_foregroundDecoration);
}
@override
void visitChildren(RenderObjectVisitor visitor) {
if (_decoration != null) visitor(_decoration);
if (_child != null) visitor(_child);
if (_foregroundDecoration != null) visitor(_foregroundDecoration);
}
@override
List<DiagnosticsNode> debugDescribeChildren() {
List<DiagnosticsNode> result = <DiagnosticsNode>[];
if (decoration != null) {
result.add(decoration.toDiagnosticsNode(name: 'decoration'));
}
if (foregroundDecoration != null) {
result.add(foregroundDecoration.toDiagnosticsNode(
name: 'foreground_decoration'));
}
if (child != null) {
result.add(child.toDiagnosticsNode(name: 'child'));
}
return result;
}
@override
bool hitTestChildren(HitTestResult result,
{double mainAxisPosition, double crossAxisPosition}) {
assert(geometry.hitTestExtent > 0.0);
return child.hitTest(result,
mainAxisPosition: mainAxisPosition,
crossAxisPosition: crossAxisPosition);
}
@override
void performLayout() {
if (child == null) {
geometry = SliverGeometry();
return;
}
// child not null
AxisDirection axisDirection = applyGrowthDirectionToAxisDirection(
constraints.axisDirection, constraints.growthDirection);
// layout sliver
child.layout(constraints, parentUsesSize: true);
final SliverGeometry childLayoutGeometry = child.geometry;
geometry = childLayoutGeometry;
// layout decoration with child size + margin
//Todo: Support rtl
EdgeInsets margin = this.margin.resolve(TextDirection.ltr);
final maxExtent = childLayoutGeometry.maxPaintExtent - margin.horizontal;
final crossAxisExtent = constraints.crossAxisExtent - margin.vertical;
if (decoration != null) {
decoration.layout(
constraints.asBoxConstraints(
maxExtent: maxExtent, crossAxisExtent: crossAxisExtent),
parentUsesSize: true);
}
if (foregroundDecoration != null) {
foregroundDecoration.layout(
constraints.asBoxConstraints(
maxExtent: maxExtent, crossAxisExtent: crossAxisExtent),
parentUsesSize: true);
}
// compute decoration offset
final SliverPhysicalParentData headerParentData = decoration.parentData;
final SliverPhysicalParentData foregroundParentData =
foregroundDecoration.parentData;
double scrollOffset = -constraints.scrollOffset;
Offset offset;
switch (axisDirection) {
case AxisDirection.up:
offset = Offset(0.0, geometry.paintExtent);
break;
case AxisDirection.down:
offset = Offset(0, scrollOffset);
break;
case AxisDirection.left:
offset = Offset(geometry.paintExtent, 0.0);
break;
case AxisDirection.right:
offset = Offset.zero;
break;
}
offset += Offset(margin.left, margin.top);
headerParentData.paintOffset = offset;
foregroundParentData.paintOffset = offset;
//compute child clip
if (this.borderRadius != null) {
BorderRadius borderRadius = this.borderRadius.resolve(TextDirection.ltr);
_clipRRect = borderRadius.toRRect(Rect.fromLTRB(
0, 0, constraints.crossAxisExtent, geometry.maxPaintExtent));
double offSetY = scrollOffset;
_clipRRect = _clipRRect.shift(Offset(0, offSetY));
}
}
@override
void applyPaintTransform(RenderObject child, Matrix4 transform) {
assert(child != null);
final SliverPhysicalParentData childParentData = child.parentData;
childParentData.applyPaintTransform(transform);
}
@override
void paint(PaintingContext context, Offset offset) {
if (geometry.visible) {
// paint decoration
if (decoration != null) {
final SliverPhysicalParentData childParentData = decoration.parentData;
context.paintChild(decoration, offset + childParentData.paintOffset);
}
// paint child
if (child != null && child.geometry.visible) {
final SliverPhysicalParentData childParentData = child.parentData;
final PaintingContextCallback painter =
(PaintingContext context, Offset offset) {
context.paintChild(child, offset);
};
if (_clipRRect != null && _clipRRect != RRect.zero) {
context.pushClipRRect(
needsCompositing,
offset + childParentData.paintOffset,
_clipRRect.outerRect,
_clipRRect,
painter,
);
} else {
painter(context, offset + childParentData.paintOffset);
}
}
if (foregroundDecoration != null) {
final SliverPhysicalParentData childParentData =
foregroundDecoration.parentData;
context.paintChild(
foregroundDecoration, offset + childParentData.paintOffset);
}
}
}
}