-
Notifications
You must be signed in to change notification settings - Fork 31.2k
Expand file tree
/
Copy pathsliver_fixed_extent_list.dart
More file actions
555 lines (513 loc) · 19.9 KB
/
Copy pathsliver_fixed_extent_list.dart
File metadata and controls
555 lines (513 loc) · 19.9 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// 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.
/// @docImport 'sliver_fill.dart';
/// @docImport 'sliver_list.dart';
library;
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'box.dart';
import 'sliver.dart';
import 'sliver_multi_box_adaptor.dart';
/// A sliver that contains multiple box children that have the explicit extent in
/// the main axis.
///
/// [RenderSliverFixedExtentBoxAdaptor] places its children in a linear array
/// along the main axis. Each child is forced to have the returned value of [itemExtentBuilder]
/// when the [itemExtentBuilder] is non-null or the [itemExtent] when [itemExtentBuilder]
/// is null in the main axis and the [SliverConstraints.crossAxisExtent] in the cross axis.
///
/// Subclasses should override [itemExtent] or [itemExtentBuilder] to control
/// the size of the children in the main axis. For a concrete subclass with a
/// configurable [itemExtent], see [RenderSliverFixedExtentList] or [RenderSliverVariedExtentList].
///
/// [RenderSliverFixedExtentBoxAdaptor] is more efficient than
/// [RenderSliverList] because [RenderSliverFixedExtentBoxAdaptor] does not need
/// to perform layout on its children to obtain their extent in the main axis.
///
/// See also:
///
/// * [RenderSliverFixedExtentList], which has a configurable [itemExtent].
/// * [RenderSliverFillViewport], which determines the [itemExtent] based on
/// [SliverConstraints.viewportMainAxisExtent].
/// * [RenderSliverFillRemaining], which determines the [itemExtent] based on
/// [SliverConstraints.remainingPaintExtent].
/// * [RenderSliverList], which does not require its children to have the same
/// extent in the main axis.
abstract class RenderSliverFixedExtentBoxAdaptor extends RenderSliverMultiBoxAdaptor {
/// Creates a sliver that contains multiple box children that have the same
/// extent in the main axis.
RenderSliverFixedExtentBoxAdaptor({required super.childManager});
/// The main-axis extent of each item.
///
/// If this is non-null, the [itemExtentBuilder] must be null.
/// If this is null, the [itemExtentBuilder] must be non-null.
double? get itemExtent;
/// The main-axis extent builder of each item.
///
/// If this is non-null, the [itemExtent] must be null.
/// If this is null, the [itemExtent] must be non-null.
ItemExtentBuilder? get itemExtentBuilder => null;
/// The layout offset for the child with the given index.
///
/// This function uses the returned value of [itemExtentBuilder] or the
/// [itemExtent] to avoid recomputing item size repeatedly during layout.
///
/// By default, places the children in order, without gaps, starting from
/// layout offset zero.
@visibleForTesting
@protected
double indexToLayoutOffset(
@Deprecated(
'The itemExtent is already available within the scope of this function. '
'This feature was deprecated after v3.20.0-7.0.pre.',
)
double itemExtent,
int index,
) {
if (itemExtentBuilder == null) {
itemExtent = this.itemExtent!;
return itemExtent * index;
} else {
var offset = 0.0;
double? itemExtent;
for (var i = 0; i < index; i++) {
final int? childCount = childManager.estimatedChildCount;
if (childCount != null && i > childCount - 1) {
break;
}
itemExtent = itemExtentBuilder!(i, layoutDimensions);
if (itemExtent == null) {
break;
}
offset += itemExtent;
}
return offset;
}
}
/// The minimum child index that is visible at the given scroll offset.
///
/// This function uses the returned value of [itemExtentBuilder] or the
/// [itemExtent] to avoid recomputing item size repeatedly during layout.
///
/// By default, returns a value consistent with the children being placed in
/// order, without gaps, starting from layout offset zero.
@visibleForTesting
@protected
int getMinChildIndexForScrollOffset(
double scrollOffset,
@Deprecated(
'The itemExtent is already available within the scope of this function. '
'This feature was deprecated after v3.20.0-7.0.pre.',
)
double itemExtent,
) {
if (itemExtentBuilder == null) {
itemExtent = this.itemExtent!;
if (itemExtent > 0.0) {
final double actual = scrollOffset / itemExtent;
final int round = actual.round();
if ((actual * itemExtent - round * itemExtent).abs() < precisionErrorTolerance) {
return round;
}
return actual.floor();
}
return 0;
} else {
return _getChildIndexForScrollOffset(scrollOffset, itemExtentBuilder!);
}
}
/// The maximum child index that is visible at the given scroll offset.
///
/// This function uses the returned value of [itemExtentBuilder] or the
/// [itemExtent] to avoid recomputing item size repeatedly during layout.
///
/// By default, returns a value consistent with the children being placed in
/// order, without gaps, starting from layout offset zero.
@visibleForTesting
@protected
int getMaxChildIndexForScrollOffset(
double scrollOffset,
@Deprecated(
'The itemExtent is already available within the scope of this function. '
'This feature was deprecated after v3.20.0-7.0.pre.',
)
double itemExtent,
) {
if (itemExtentBuilder == null) {
itemExtent = this.itemExtent!;
if (itemExtent > 0.0) {
final double actual = scrollOffset / itemExtent - 1;
final int round = actual.round();
if ((actual * itemExtent - round * itemExtent).abs() < precisionErrorTolerance) {
return math.max(0, round);
}
return math.max(0, actual.ceil());
}
return 0;
} else {
return _getChildIndexForScrollOffset(scrollOffset, itemExtentBuilder!);
}
}
/// Called to estimate the total scrollable extents of this object.
///
/// Must return the total distance from the start of the child with the
/// earliest possible index to the end of the child with the last possible
/// index.
///
/// By default, defers to [RenderSliverBoxChildManager.estimateMaxScrollOffset].
///
/// See also:
///
/// * [computeMaxScrollOffset], which is similar but must provide a precise
/// value.
@protected
double estimateMaxScrollOffset(
SliverConstraints constraints, {
int? firstIndex,
int? lastIndex,
double? leadingScrollOffset,
double? trailingScrollOffset,
}) {
return childManager.estimateMaxScrollOffset(
constraints,
firstIndex: firstIndex,
lastIndex: lastIndex,
leadingScrollOffset: leadingScrollOffset,
trailingScrollOffset: trailingScrollOffset,
);
}
/// Called to obtain a precise measure of the total scrollable extents of this
/// object.
///
/// Must return the precise total distance from the start of the child with
/// the earliest possible index to the end of the child with the last possible
/// index.
///
/// This is used when no child is available for the index corresponding to the
/// current scroll offset, to determine the precise dimensions of the sliver.
/// It must return a precise value. It will not be called if the
/// [childManager] returns an infinite number of children for positive
/// indices.
///
/// If [itemExtentBuilder] is null, multiplies the [itemExtent] by the number
/// of children reported by [RenderSliverBoxChildManager.childCount].
/// If [itemExtentBuilder] is non-null, sum the extents of the first
/// [RenderSliverBoxChildManager.childCount] children.
///
/// See also:
///
/// * [estimateMaxScrollOffset], which is similar but may provide inaccurate
/// values.
@visibleForTesting
@protected
double computeMaxScrollOffset(
SliverConstraints constraints,
@Deprecated(
'The itemExtent is already available within the scope of this function. '
'This feature was deprecated after v3.20.0-7.0.pre.',
)
double itemExtent,
) {
if (itemExtentBuilder == null) {
itemExtent = this.itemExtent!;
return childManager.childCount * itemExtent;
} else {
var offset = 0.0;
double? itemExtent;
for (var i = 0; i < childManager.childCount; i++) {
itemExtent = itemExtentBuilder!(i, layoutDimensions);
if (itemExtent == null) {
break;
}
offset += itemExtent;
}
return offset;
}
}
int _getChildIndexForScrollOffset(double scrollOffset, ItemExtentBuilder callback) {
if (scrollOffset == 0.0) {
return 0;
}
var position = 0.0;
var index = 0;
double? itemExtent;
while (position < scrollOffset) {
final int? childCount = childManager.estimatedChildCount;
if (childCount != null && index > childCount - 1) {
break;
}
itemExtent = callback(index, layoutDimensions);
if (itemExtent == null) {
break;
}
position += itemExtent;
++index;
}
return index - 1;
}
BoxConstraints _getChildConstraints(int index) {
double extent;
if (itemExtentBuilder == null) {
extent = itemExtent!;
} else {
extent = itemExtentBuilder!(index, layoutDimensions)!;
}
return constraints.asBoxConstraints(minExtent: extent, maxExtent: extent);
}
/// The layout dimensions for the sliver.
///
/// If the sliver has not been laid out yet, this returns a
/// [SliverLayoutDimensions] based on the current [constraints].
SliverLayoutDimensions get layoutDimensions {
return _currentLayoutDimensions ??
SliverLayoutDimensions(
scrollOffset: constraints.scrollOffset,
precedingScrollExtent: constraints.precedingScrollExtent,
viewportMainAxisExtent: constraints.viewportMainAxisExtent,
crossAxisExtent: constraints.crossAxisExtent,
);
}
@override
double paintExtentOf(RenderBox child) {
if (itemExtentBuilder == null) {
return itemExtent!;
}
return itemExtentBuilder!(indexOf(child), layoutDimensions)!;
}
SliverLayoutDimensions? _currentLayoutDimensions;
@override
void debugAssertDoesMeetConstraints() {
super.debugAssertDoesMeetConstraints();
assert(() {
if (itemExtentBuilder == null && geometry!.scrollExtent.isFinite) {
final double itemExtent = this.itemExtent!;
final double scrollExtent = geometry!.scrollExtent;
final double count = scrollExtent / itemExtent;
final double diff = (count.roundToDouble() - count).abs();
if (diff * itemExtent > precisionErrorTolerance && diff > precisionErrorTolerance) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'RenderSliverFixedExtentBoxAdaptor.computeMaxScrollOffset() returned a value that is not an even multiple of its itemExtent.',
),
ErrorDescription(
'The itemExtent was $itemExtent, but the scrollExtent was $scrollExtent.',
),
ErrorDescription(
'The difference was $diff, which is greater than precisionErrorTolerance ($precisionErrorTolerance).',
),
describeForError('The render object in question was'),
]);
}
}
return true;
}());
}
@override
void performLayout() {
assert(
(itemExtent != null && itemExtentBuilder == null) ||
(itemExtent == null && itemExtentBuilder != null),
);
assert(itemExtentBuilder != null || (itemExtent!.isFinite && itemExtent! >= 0));
final SliverConstraints constraints = this.constraints;
childManager.didStartLayout();
childManager.setDidUnderflow(false);
final double scrollOffset = constraints.scrollOffset + constraints.cacheOrigin;
assert(scrollOffset >= 0.0);
final double remainingExtent = constraints.remainingCacheExtent;
assert(remainingExtent >= 0.0);
final double targetEndScrollOffset = scrollOffset + remainingExtent;
_currentLayoutDimensions = SliverLayoutDimensions(
scrollOffset: constraints.scrollOffset,
precedingScrollExtent: constraints.precedingScrollExtent,
viewportMainAxisExtent: constraints.viewportMainAxisExtent,
crossAxisExtent: constraints.crossAxisExtent,
);
// TODO(Piinks): Clean up when deprecation expires.
const double deprecatedExtraItemExtent = -1;
final int firstIndex = getMinChildIndexForScrollOffset(scrollOffset, deprecatedExtraItemExtent);
final int? targetLastIndex = targetEndScrollOffset.isFinite
? getMaxChildIndexForScrollOffset(targetEndScrollOffset, deprecatedExtraItemExtent)
: null;
if (firstChild != null) {
final int leadingGarbage = calculateLeadingGarbage(firstIndex: firstIndex);
final int trailingGarbage = targetLastIndex != null
? calculateTrailingGarbage(lastIndex: targetLastIndex)
: 0;
collectGarbage(leadingGarbage, trailingGarbage);
} else {
collectGarbage(0, 0);
}
if (firstChild == null) {
final double layoutOffset = indexToLayoutOffset(deprecatedExtraItemExtent, firstIndex);
if (!addInitialChild(index: firstIndex, layoutOffset: layoutOffset)) {
// There are either no children, or we are past the end of all our children.
final double max;
if (firstIndex <= 0) {
max = 0.0;
} else {
max = computeMaxScrollOffset(constraints, deprecatedExtraItemExtent);
}
geometry = SliverGeometry(scrollExtent: max, maxPaintExtent: max);
childManager.didFinishLayout();
return;
}
}
RenderBox? trailingChildWithLayout;
for (int index = indexOf(firstChild!) - 1; index >= firstIndex; --index) {
final RenderBox? child = insertAndLayoutLeadingChild(_getChildConstraints(index));
if (child == null) {
// Items before the previously first child are no longer present.
// Reset the scroll offset to offset all items prior and up to the
// missing item. Let parent re-layout everything.
geometry = SliverGeometry(
scrollOffsetCorrection: indexToLayoutOffset(deprecatedExtraItemExtent, index),
);
return;
}
final childParentData = child.parentData! as SliverMultiBoxAdaptorParentData;
childParentData.layoutOffset = indexToLayoutOffset(deprecatedExtraItemExtent, index);
assert(childParentData.index == index);
trailingChildWithLayout ??= child;
}
if (trailingChildWithLayout == null) {
firstChild!.layout(_getChildConstraints(indexOf(firstChild!)));
final childParentData = firstChild!.parentData! as SliverMultiBoxAdaptorParentData;
childParentData.layoutOffset = indexToLayoutOffset(deprecatedExtraItemExtent, firstIndex);
trailingChildWithLayout = firstChild;
}
double estimatedMaxScrollOffset = double.infinity;
for (
int index = indexOf(trailingChildWithLayout!) + 1;
targetLastIndex == null || index <= targetLastIndex;
++index
) {
RenderBox? child = childAfter(trailingChildWithLayout!);
if (child == null || indexOf(child) != index) {
child = insertAndLayoutChild(_getChildConstraints(index), after: trailingChildWithLayout);
if (child == null) {
// We have run out of children.
estimatedMaxScrollOffset = indexToLayoutOffset(deprecatedExtraItemExtent, index);
break;
}
} else {
child.layout(_getChildConstraints(index));
}
trailingChildWithLayout = child;
final childParentData = child.parentData! as SliverMultiBoxAdaptorParentData;
assert(childParentData.index == index);
childParentData.layoutOffset = indexToLayoutOffset(
deprecatedExtraItemExtent,
childParentData.index!,
);
}
final int lastIndex = indexOf(lastChild!);
final double leadingScrollOffset = indexToLayoutOffset(deprecatedExtraItemExtent, firstIndex);
final double trailingScrollOffset = indexToLayoutOffset(
deprecatedExtraItemExtent,
lastIndex + 1,
);
assert(
firstIndex == 0 || childScrollOffset(firstChild!)! - scrollOffset <= precisionErrorTolerance,
);
assert(debugAssertChildListIsNonEmptyAndContiguous());
assert(indexOf(firstChild!) == firstIndex);
assert(targetLastIndex == null || lastIndex <= targetLastIndex);
estimatedMaxScrollOffset = math.min(
estimatedMaxScrollOffset,
estimateMaxScrollOffset(
constraints,
firstIndex: firstIndex,
lastIndex: lastIndex,
leadingScrollOffset: leadingScrollOffset,
trailingScrollOffset: trailingScrollOffset,
),
);
final double paintExtent = calculatePaintOffset(
constraints,
from: leadingScrollOffset,
to: trailingScrollOffset,
);
final double cacheExtent = calculateCacheOffset(
constraints,
from: leadingScrollOffset,
to: trailingScrollOffset,
);
final double targetEndScrollOffsetForPaint =
constraints.scrollOffset + constraints.remainingPaintExtent;
final int? targetLastIndexForPaint = targetEndScrollOffsetForPaint.isFinite
? getMaxChildIndexForScrollOffset(targetEndScrollOffsetForPaint, deprecatedExtraItemExtent)
: null;
geometry = SliverGeometry(
scrollExtent: estimatedMaxScrollOffset,
paintExtent: paintExtent,
cacheExtent: cacheExtent,
maxPaintExtent: estimatedMaxScrollOffset,
// Conservative to avoid flickering away the clip during scroll.
hasVisualOverflow:
(targetLastIndexForPaint != null && lastIndex >= targetLastIndexForPaint) ||
constraints.scrollOffset > 0.0,
);
// We may have started the layout while scrolled to the end, which would not
// expose a new child.
if (estimatedMaxScrollOffset == trailingScrollOffset) {
childManager.setDidUnderflow(true);
}
childManager.didFinishLayout();
}
}
/// A sliver that places multiple box children with the same main axis extent in
/// a linear array.
///
/// [RenderSliverFixedExtentList] places its children in a linear array along
/// the main axis starting at offset zero and without gaps. Each child is forced
/// to have the [itemExtent] in the main axis and the
/// [SliverConstraints.crossAxisExtent] in the cross axis.
///
/// [RenderSliverFixedExtentList] is more efficient than [RenderSliverList]
/// because [RenderSliverFixedExtentList] does not need to perform layout on its
/// children to obtain their extent in the main axis.
///
/// See also:
///
/// * [RenderSliverList], which does not require its children to have the same
/// extent in the main axis.
/// * [RenderSliverFillViewport], which determines the [itemExtent] based on
/// [SliverConstraints.viewportMainAxisExtent].
/// * [RenderSliverFillRemaining], which determines the [itemExtent] based on
/// [SliverConstraints.remainingPaintExtent].
class RenderSliverFixedExtentList extends RenderSliverFixedExtentBoxAdaptor {
/// Creates a sliver that contains multiple box children that have a given
/// extent in the main axis.
RenderSliverFixedExtentList({required super.childManager, required this._itemExtent});
@override
double get itemExtent => _itemExtent;
double _itemExtent;
set itemExtent(double value) {
if (_itemExtent == value) {
return;
}
_itemExtent = value;
markNeedsLayout();
}
}
/// A sliver that places multiple box children with the corresponding main axis extent in
/// a linear array.
class RenderSliverVariedExtentList extends RenderSliverFixedExtentBoxAdaptor {
/// Creates a sliver that contains multiple box children that have a explicit
/// extent in the main axis.
RenderSliverVariedExtentList({required super.childManager, required this._itemExtentBuilder});
@override
ItemExtentBuilder get itemExtentBuilder => _itemExtentBuilder;
ItemExtentBuilder _itemExtentBuilder;
set itemExtentBuilder(ItemExtentBuilder value) {
if (_itemExtentBuilder == value) {
return;
}
_itemExtentBuilder = value;
markNeedsLayout();
}
@override
double? get itemExtent => null;
}