-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathsliver_stack.dart
301 lines (273 loc) · 9.75 KB
/
sliver_stack.dart
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
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'rendering/sliver_stack.dart';
/// A [Stack] widget that can be used as a sliver
///
/// Its children can either be slivers or box children that are positioned
/// using [SliverPositioned] with at least one of its values not null
/// This means that only the sliver children have an effect on the size
/// of this sliver and the box children are meant to follow the slivers
class SliverStack extends MultiChildRenderObjectWidget {
// flutter pre 3.13 does not allow the constructor to be const
// ignore: prefer_const_constructors_in_immutables
SliverStack({
Key? key,
required List<Widget> children,
this.textDirection,
this.positionedAlignment = Alignment.center,
this.insetOnOverlap = false,
}) : super(key: key, children: children);
/// The alignment to use on any positioned children that are only partially
/// positioned
///
/// Defaults to [Alignment.center]
final AlignmentGeometry positionedAlignment;
/// The text direction with which to resolve [positionedAlignment].
///
/// Defaults to the ambient [Directionality].
final TextDirection? textDirection;
/// Whether the positioned children should be inset (made smaller) when the sliver has overlap.
///
/// This is very useful and most likely what you want when using a pinned [SliverPersistentHeader]
/// as child of the stack
///
/// Defaults to false
final bool insetOnOverlap;
@override
RenderSliverStack createRenderObject(BuildContext context) {
return RenderSliverStack()
..positionedAlignment = positionedAlignment
..textDirection = textDirection ?? Directionality.of(context)
..insetOnOverlap = insetOnOverlap;
}
@override
void updateRenderObject(
BuildContext context, covariant RenderSliverStack renderObject) {
renderObject
..positionedAlignment = positionedAlignment
..textDirection = textDirection ?? Directionality.of(context)
..insetOnOverlap = insetOnOverlap;
}
}
/// A widget that controls where a box child of a [SliverStack] is positioned.
///
/// A [SliverPositioned] widget must be a descendant of a [SliverStack], and the path from
/// the [SliverPositioned] widget to its enclosing [SliverStack] must contain only
/// [StatelessWidget]s or [StatefulWidget]s (not other kinds of widgets, like
/// [RenderObjectWidget]s).
class SliverPositioned extends ParentDataWidget<SliverStackParentData> {
/// Creates a widget that controls where a child of a [Stack] is positioned.
///
/// Only two out of the three horizontal values ([left], [right],
/// [width]), and only two out of the three vertical values ([top],
/// [bottom], [height]), can be set. In each case, at least one of
/// the three must be null.
///
/// See also:
///
/// * [Positioned.directional], which specifies the widget's horizontal
/// position using `start` and `end` rather than `left` and `right`.
/// * [PositionedDirectional], which is similar to [Positioned.directional]
/// but adapts to the ambient [Directionality].
const SliverPositioned({
Key? key,
this.left,
this.top,
this.right,
this.bottom,
this.width,
this.height,
required Widget child,
}) : assert(left == null || right == null || width == null),
assert(top == null || bottom == null || height == null),
super(key: key, child: child);
/// Creates a Positioned object with the values from the given [Rect].
///
/// This sets the [left], [top], [width], and [height] properties
/// from the given [Rect]. The [right] and [bottom] properties are
/// set to null.
SliverPositioned.fromRect({
Key? key,
required Rect rect,
required Widget child,
}) : left = rect.left,
top = rect.top,
width = rect.width,
height = rect.height,
right = null,
bottom = null,
super(key: key, child: child);
/// Creates a Positioned object with the values from the given [RelativeRect].
///
/// This sets the [left], [top], [right], and [bottom] properties from the
/// given [RelativeRect]. The [height] and [width] properties are set to null.
SliverPositioned.fromRelativeRect({
Key? key,
required RelativeRect rect,
required Widget child,
}) : left = rect.left,
top = rect.top,
right = rect.right,
bottom = rect.bottom,
width = null,
height = null,
super(key: key, child: child);
/// Creates a Positioned object with [left], [top], [right], and [bottom] set
/// to 0.0 unless a value for them is passed.
const SliverPositioned.fill({
Key? key,
this.left = 0.0,
this.top = 0.0,
this.right = 0.0,
this.bottom = 0.0,
required Widget child,
}) : width = null,
height = null,
super(key: key, child: child);
/// Creates a widget that controls where a child of a [Stack] is positioned.
///
/// Only two out of the three horizontal values (`start`, `end`,
/// [width]), and only two out of the three vertical values ([top],
/// [bottom], [height]), can be set. In each case, at least one of
/// the three must be null.
///
/// If `textDirection` is [TextDirection.rtl], then the `start` argument is
/// used for the [right] property and the `end` argument is used for the
/// [left] property. Otherwise, if `textDirection` is [TextDirection.ltr],
/// then the `start` argument is used for the [left] property and the `end`
/// argument is used for the [right] property.
///
/// The `textDirection` argument must not be null.
///
/// See also:
///
/// * [PositionedDirectional], which adapts to the ambient [Directionality].
factory SliverPositioned.directional({
Key? key,
required TextDirection textDirection,
double? start,
double? top,
double? end,
double? bottom,
double? width,
double? height,
required Widget child,
}) {
double? left;
double? right;
switch (textDirection) {
case TextDirection.rtl:
left = end;
right = start;
break;
case TextDirection.ltr:
left = start;
right = end;
break;
}
return SliverPositioned(
key: key,
left: left,
top: top,
right: right,
bottom: bottom,
width: width,
height: height,
child: child,
);
}
/// The distance that the child's left edge is inset from the left of the stack.
///
/// Only two out of the three horizontal values ([left], [right], [width]) can be
/// set. The third must be null.
///
/// If all three are null, the [Stack.alignment] is used to position the child
/// horizontally.
final double? left;
/// The distance that the child's top edge is inset from the top of the stack.
///
/// Only two out of the three vertical values ([top], [bottom], [height]) can be
/// set. The third must be null.
///
/// If all three are null, the [Stack.alignment] is used to position the child
/// vertically.
final double? top;
/// The distance that the child's right edge is inset from the right of the stack.
///
/// Only two out of the three horizontal values ([left], [right], [width]) can be
/// set. The third must be null.
///
/// If all three are null, the [Stack.alignment] is used to position the child
/// horizontally.
final double? right;
/// The distance that the child's bottom edge is inset from the bottom of the stack.
///
/// Only two out of the three vertical values ([top], [bottom], [height]) can be
/// set. The third must be null.
///
/// If all three are null, the [Stack.alignment] is used to position the child
/// vertically.
final double? bottom;
/// The child's width.
///
/// Only two out of the three horizontal values ([left], [right], [width]) can be
/// set. The third must be null.
///
/// If all three are null, the [Stack.alignment] is used to position the child
/// horizontally.
final double? width;
/// The child's height.
///
/// Only two out of the three vertical values ([top], [bottom], [height]) can be
/// set. The third must be null.
///
/// If all three are null, the [Stack.alignment] is used to position the child
/// vertically.
final double? height;
@override
void applyParentData(RenderObject renderObject) {
assert(renderObject.parentData is SliverStackParentData);
final parentData = renderObject.parentData as SliverStackParentData;
bool needsLayout = false;
if (parentData.left != left) {
parentData.left = left;
needsLayout = true;
}
if (parentData.top != top) {
parentData.top = top;
needsLayout = true;
}
if (parentData.right != right) {
parentData.right = right;
needsLayout = true;
}
if (parentData.bottom != bottom) {
parentData.bottom = bottom;
needsLayout = true;
}
if (parentData.width != width) {
parentData.width = width;
needsLayout = true;
}
if (parentData.height != height) {
parentData.height = height;
needsLayout = true;
}
if (needsLayout) {
final Object? targetParent = renderObject.parent;
if (targetParent is RenderObject) targetParent.markNeedsLayout();
}
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DoubleProperty('left', left, defaultValue: null));
properties.add(DoubleProperty('top', top, defaultValue: null));
properties.add(DoubleProperty('right', right, defaultValue: null));
properties.add(DoubleProperty('bottom', bottom, defaultValue: null));
properties.add(DoubleProperty('width', width, defaultValue: null));
properties.add(DoubleProperty('height', height, defaultValue: null));
}
@override
Type get debugTypicalAncestorWidgetClass => SliverStack;
}