-
Notifications
You must be signed in to change notification settings - Fork 725
/
Copy pathslivers.dart
573 lines (523 loc) · 18.3 KB
/
slivers.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Author: Jpeng
* Email: [email protected]
* Time: 2019/5/2 下午5:09
*/
import 'package:flutter/widgets.dart';
import 'dart:math' as Math;
import 'package:flutter/rendering.dart';
import '../smart_refresher.dart';
/// Render header sliver widget
class SliverRefresh extends SingleChildRenderObjectWidget {
const SliverRefresh({
Key? key,
this.paintOffsetY,
this.refreshIndicatorLayoutExtent = 0.0,
this.floating = false,
Widget? child,
this.refreshStyle,
}) : assert(refreshIndicatorLayoutExtent >= 0.0),
super(key: key, child: child);
/// The amount of space the indicator should occupy in the sliver in a
/// resting state when in the refreshing mode.
final double refreshIndicatorLayoutExtent;
/// _RenderSliverRefresh will paint the child in the available
/// space either way but this instructs the _RenderSliverRefresh
/// on whether to also occupy any layoutExtent space or not.
final bool floating;
/// header indicator display style
final RefreshStyle? refreshStyle;
/// headerOffset Head indicator layout deviation Y coordinates, mostly for FrontStyle
final double? paintOffsetY;
@override
RenderSliverRefresh createRenderObject(BuildContext context) {
return RenderSliverRefresh(
refreshIndicatorExtent: refreshIndicatorLayoutExtent,
hasLayoutExtent: floating,
paintOffsetY: paintOffsetY,
refreshStyle: refreshStyle,
);
}
@override
void updateRenderObject(
BuildContext context, covariant RenderSliverRefresh renderObject) {
final RefreshStatus mode =
SmartRefresher.of(context)!.controller.headerMode!.value;
renderObject
..refreshIndicatorLayoutExtent = refreshIndicatorLayoutExtent
..hasLayoutExtent = floating
..context = context
..refreshStyle = refreshStyle
..updateFlag = mode == RefreshStatus.twoLevelOpening ||
mode == RefreshStatus.twoLeveling ||
mode == RefreshStatus.idle
..paintOffsetY = paintOffsetY;
}
}
class RenderSliverRefresh extends RenderSliverSingleBoxAdapter {
RenderSliverRefresh(
{required double refreshIndicatorExtent,
required bool hasLayoutExtent,
RenderBox? child,
this.paintOffsetY,
this.refreshStyle})
: assert(refreshIndicatorExtent >= 0.0),
_refreshIndicatorExtent = refreshIndicatorExtent,
_hasLayoutExtent = hasLayoutExtent {
this.child = child;
}
RefreshStyle? refreshStyle;
late BuildContext context;
// The amount of layout space the indicator should occupy in the sliver in a
// resting state when in the refreshing mode.
double get refreshIndicatorLayoutExtent => _refreshIndicatorExtent;
double _refreshIndicatorExtent;
double? paintOffsetY;
// need to trigger shouldAceppty user offset ,else it will not limit scroll when enter twolevel or exit
// also it will crash if you call applyNewDimession when the state change
// I don't know why flutter limit it, no choice
bool _updateFlag = false;
set refreshIndicatorLayoutExtent(double value) {
assert(value >= 0.0);
if (value == _refreshIndicatorExtent) return;
_refreshIndicatorExtent = value;
markNeedsLayout();
}
// The child box will be laid out and painted in the available space either
// way but this determines whether to also occupy any
// [SliverGeometry.layoutExtent] space or not.
bool get hasLayoutExtent => _hasLayoutExtent;
bool _hasLayoutExtent;
set hasLayoutExtent(bool value) {
if (value == _hasLayoutExtent) return;
if (!value) {
_updateFlag = true;
}
_hasLayoutExtent = value;
markNeedsLayout();
}
// This keeps track of the previously applied scroll offsets to the scrollable
// so that when [refreshIndicatorLayoutExtent] or [hasLayoutExtent] changes,
// the appropriate delta can be applied to keep everything in the same place
// visually.
double layoutExtentOffsetCompensation = 0.0;
@override
void performResize() {
// TODO: implement performResize
super.performResize();
}
@override
// TODO: implement centerOffsetAdjustment
double get centerOffsetAdjustment {
if (refreshStyle == RefreshStyle.Front) {
final RenderViewportBase renderViewport =
parent as RenderViewportBase<ContainerParentDataMixin<RenderSliver>>;
return Math.max(0.0, -renderViewport.offset.pixels);
}
return 0.0;
}
@override
void layout(Constraints constraints, {bool parentUsesSize = false}) {
// TODO: implement layout
if (refreshStyle == RefreshStyle.Front) {
final RenderViewportBase renderViewport =
parent as RenderViewportBase<ContainerParentDataMixin<RenderSliver>>;
super.layout(
(constraints as SliverConstraints)
.copyWith(overlap: Math.min(0.0, renderViewport.offset.pixels)),
parentUsesSize: true);
} else {
super.layout(constraints, parentUsesSize: parentUsesSize);
}
}
set updateFlag(u) {
_updateFlag = u;
markNeedsLayout();
}
@override
void debugAssertDoesMeetConstraints() {
assert(geometry!.debugAssertIsValid(informationCollector: () sync* {
yield describeForError(
'The RenderSliver that returned the offending geometry was');
}));
assert(() {
if (geometry!.paintExtent > constraints.remainingPaintExtent) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'SliverGeometry has a paintOffset that exceeds the remainingPaintExtent from the constraints.'),
describeForError(
'The render object whose geometry violates the constraints is the following'),
ErrorDescription(
'The paintExtent must cause the child sliver to paint within the viewport, and so '
'cannot exceed the remainingPaintExtent.',
),
]);
}
return true;
}());
}
@override
void performLayout() {
if (_updateFlag) {
// ignore_for_file: INVALID_USE_OF_PROTECTED_MEMBER
// ignore_for_file: INVALID_USE_OF_VISIBLE_FOR_TESTING_MEMBER
Scrollable.of(context)!.position.activity!.applyNewDimensions();
_updateFlag = false;
}
// The new layout extent this sliver should now have.
final double layoutExtent =
(_hasLayoutExtent ? 1.0 : 0.0) * _refreshIndicatorExtent;
// If the new layoutExtent instructive changed, the SliverGeometry's
// layoutExtent will take that value (on the next performLayout run). Shift
// the scroll offset first so it doesn't make the scroll position suddenly jump.
if (refreshStyle != RefreshStyle.Front) {
if (layoutExtent != layoutExtentOffsetCompensation) {
geometry = SliverGeometry(
scrollOffsetCorrection: layoutExtent - layoutExtentOffsetCompensation,
);
layoutExtentOffsetCompensation = layoutExtent;
return;
}
}
bool active = constraints.overlap < 0.0 || layoutExtent > 0.0;
final double overscrolledExtent =
-(parent as RenderViewportBase).offset.pixels;
if (refreshStyle == RefreshStyle.Behind) {
child!.layout(
constraints.asBoxConstraints(
maxExtent: Math.max(0, overscrolledExtent + layoutExtent)),
parentUsesSize: true,
);
} else
child!.layout(
constraints.asBoxConstraints(),
parentUsesSize: true,
);
final double boxExtent = (constraints.axisDirection == AxisDirection.up ||
constraints.axisDirection == AxisDirection.down)
? child!.size.height
: child!.size.width;
if (active) {
final double needPaintExtent = Math.min(
Math.max(
Math.max(
(constraints.axisDirection == AxisDirection.up ||
constraints.axisDirection == AxisDirection.down)
? child!.size.height
: child!.size.width,
layoutExtent) -
constraints.scrollOffset,
0.0,
),
constraints.remainingPaintExtent);
switch (refreshStyle) {
case RefreshStyle.Follow:
geometry = SliverGeometry(
scrollExtent: layoutExtent,
paintOrigin: -boxExtent - constraints.scrollOffset + layoutExtent,
paintExtent: needPaintExtent,
hitTestExtent: needPaintExtent,
hasVisualOverflow: overscrolledExtent < boxExtent,
maxPaintExtent: needPaintExtent,
layoutExtent: Math.min(needPaintExtent,
Math.max(layoutExtent - constraints.scrollOffset, 0.0)),
);
break;
case RefreshStyle.Behind:
geometry = SliverGeometry(
scrollExtent: layoutExtent,
paintOrigin: -overscrolledExtent - constraints.scrollOffset,
paintExtent: needPaintExtent,
maxPaintExtent: needPaintExtent,
layoutExtent:
Math.max(layoutExtent - constraints.scrollOffset, 0.0),
);
break;
case RefreshStyle.UnFollow:
geometry = SliverGeometry(
scrollExtent: layoutExtent,
paintOrigin: Math.min(
-overscrolledExtent - constraints.scrollOffset,
-boxExtent - constraints.scrollOffset + layoutExtent),
paintExtent: needPaintExtent,
hasVisualOverflow: overscrolledExtent < boxExtent,
maxPaintExtent: needPaintExtent,
layoutExtent: Math.min(needPaintExtent,
Math.max(layoutExtent - constraints.scrollOffset, 0.0)),
);
break;
case RefreshStyle.Front:
geometry = SliverGeometry(
paintOrigin: constraints.axisDirection == AxisDirection.up ||
constraints.crossAxisDirection == AxisDirection.left
? boxExtent
: 0.0,
visible: true,
hasVisualOverflow: true,
);
break;
case null:
break;
}
setChildParentData(child!, constraints, geometry!);
} else {
geometry = SliverGeometry.zero;
}
}
@override
void paint(PaintingContext paintContext, Offset offset) {
paintContext.paintChild(
child!, Offset(offset.dx, offset.dy + paintOffsetY!));
}
@override
void applyPaintTransform(RenderObject child, Matrix4 transform) {}
}
/// Render footer sliver widget
class SliverLoading extends SingleChildRenderObjectWidget {
/// when not full one page,whether it should be hide and disable loading
final bool? hideWhenNotFull;
final bool? floating;
/// load state
final LoadStatus? mode;
final double? layoutExtent;
/// when not full one page,whether it should follow content
final bool? shouldFollowContent;
SliverLoading({
Key? key,
this.mode,
this.floating,
this.shouldFollowContent,
this.layoutExtent,
this.hideWhenNotFull,
Widget? child,
}) : super(key: key, child: child);
@override
RenderSliverLoading createRenderObject(BuildContext context) {
return RenderSliverLoading(
hideWhenNotFull: hideWhenNotFull,
mode: mode,
hasLayoutExtent: floating,
shouldFollowContent: shouldFollowContent,
layoutExtent: layoutExtent);
}
@override
void updateRenderObject(
BuildContext context, covariant RenderSliverLoading renderObject) {
renderObject
..mode = mode
..hasLayoutExtent = floating!
..layoutExtent = layoutExtent
..shouldFollowContent = shouldFollowContent
..hideWhenNotFull = hideWhenNotFull;
}
}
class RenderSliverLoading extends RenderSliverSingleBoxAdapter {
RenderSliverLoading({
RenderBox? child,
this.mode,
double? layoutExtent,
bool? hasLayoutExtent,
this.shouldFollowContent,
this.hideWhenNotFull,
}) {
_hasLayoutExtent = hasLayoutExtent;
this.layoutExtent = layoutExtent;
this.child = child;
}
bool? shouldFollowContent;
bool? hideWhenNotFull;
LoadStatus? mode;
double? _layoutExtent;
set layoutExtent(extent) {
if (extent == _layoutExtent) return;
_layoutExtent = extent;
markNeedsLayout();
}
get layoutExtent => _layoutExtent;
bool get hasLayoutExtent => _hasLayoutExtent!;
bool? _hasLayoutExtent;
set hasLayoutExtent(bool value) {
if (value == _hasLayoutExtent) return;
_hasLayoutExtent = value;
markNeedsLayout();
}
bool _computeIfFull(SliverConstraints cons) {
final RenderViewport viewport = parent as RenderViewport;
RenderSliver? sliverP = viewport.firstChild;
double totalScrollExtent = cons.precedingScrollExtent;
while (sliverP != this) {
if (sliverP is RenderSliverRefresh) {
totalScrollExtent -= sliverP.geometry!.scrollExtent;
break;
}
sliverP = viewport.childAfter(sliverP!);
}
// consider about footer layoutExtent,it should be subtracted it's height
return totalScrollExtent > cons.viewportMainAxisExtent;
}
// many sitiuation: 1. reverse 2. not reverse
// 3. follow content 4. unfollow content
//5. not full 6. full
double? computePaintOrigin(double? layoutExtent, bool reverse, bool follow) {
if (follow) {
if (reverse) {
return layoutExtent;
}
return 0.0;
} else {
if (reverse) {
return Math.max(
constraints.viewportMainAxisExtent -
constraints.precedingScrollExtent,
0.0) +
layoutExtent!;
} else {
return Math.max(
constraints.viewportMainAxisExtent -
constraints.precedingScrollExtent,
0.0);
}
}
}
@override
void debugAssertDoesMeetConstraints() {
assert(geometry!.debugAssertIsValid(informationCollector: () sync* {
yield describeForError(
'The RenderSliver that returned the offending geometry was');
}));
assert(() {
if (geometry!.paintExtent > constraints.remainingPaintExtent) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'SliverGeometry has a paintOffset that exceeds the remainingPaintExtent from the constraints.'),
describeForError(
'The render object whose geometry violates the constraints is the following'),
ErrorDescription(
'The paintExtent must cause the child sliver to paint within the viewport, and so '
'cannot exceed the remainingPaintExtent.',
),
]);
}
return true;
}());
}
@override
void performLayout() {
assert(constraints.growthDirection == GrowthDirection.forward);
if (child == null) {
geometry = SliverGeometry.zero;
return;
}
bool active;
if (hideWhenNotFull! && mode != LoadStatus.noMore) {
active = _computeIfFull(constraints);
} else {
active = true;
}
if (active) {
child!.layout(constraints.asBoxConstraints(), parentUsesSize: true);
} else {
child!.layout(
constraints.asBoxConstraints(maxExtent: 0.0, minExtent: 0.0),
parentUsesSize: true);
}
double childExtent = constraints.axis == Axis.vertical
? child!.size.height
: child!.size.width;
final double paintedChildSize =
calculatePaintOffset(constraints, from: 0.0, to: childExtent);
final double cacheExtent =
calculateCacheOffset(constraints, from: 0.0, to: childExtent);
assert(paintedChildSize.isFinite);
assert(paintedChildSize >= 0.0);
if (active) {
// consider reverse loading and HideAlways==loadStyle
geometry = SliverGeometry(
scrollExtent: !_hasLayoutExtent! || (!_computeIfFull(constraints) && mode != LoadStatus.noMore)
? 0
: layoutExtent,
paintExtent: paintedChildSize,
// this need to fix later
paintOrigin: computePaintOrigin(
!_hasLayoutExtent! || !_computeIfFull(constraints)
? layoutExtent
: 0.0,
constraints.axisDirection == AxisDirection.up ||
constraints.axisDirection == AxisDirection.left,
_computeIfFull(constraints) || shouldFollowContent!)!,
cacheExtent: cacheExtent,
maxPaintExtent: childExtent,
hitTestExtent: paintedChildSize,
visible: true,
hasVisualOverflow: true,
);
setChildParentData(child!, constraints, geometry!);
} else {
geometry = SliverGeometry.zero;
}
}
}
class SliverRefreshBody extends SingleChildRenderObjectWidget {
/// Creates a sliver that contains a single box widget.
const SliverRefreshBody({
Key? key,
Widget? child,
}) : super(key: key, child: child);
@override
RenderSliverRefreshBody createRenderObject(BuildContext context) =>
RenderSliverRefreshBody();
}
class RenderSliverRefreshBody extends RenderSliverSingleBoxAdapter {
/// Creates a [RenderSliver] that wraps a [RenderBox].
RenderSliverRefreshBody({
RenderBox? child,
}) : super(child: child);
@override
void performLayout() {
if (child == null) {
geometry = SliverGeometry.zero;
return;
}
child!.layout(constraints.asBoxConstraints(maxExtent: 1111111),
parentUsesSize: true);
double? childExtent;
switch (constraints.axis) {
case Axis.horizontal:
childExtent = child!.size.width;
break;
case Axis.vertical:
childExtent = child!.size.height;
break;
}
if (childExtent == 1111111) {
child!.layout(
constraints.asBoxConstraints(
maxExtent: constraints.viewportMainAxisExtent),
parentUsesSize: true);
}
switch (constraints.axis) {
case Axis.horizontal:
childExtent = child!.size.width;
break;
case Axis.vertical:
childExtent = child!.size.height;
break;
}
final double paintedChildSize =
calculatePaintOffset(constraints, from: 0.0, to: childExtent);
final double cacheExtent =
calculateCacheOffset(constraints, from: 0.0, to: childExtent);
assert(paintedChildSize.isFinite);
assert(paintedChildSize >= 0.0);
geometry = SliverGeometry(
scrollExtent: childExtent,
paintExtent: paintedChildSize,
cacheExtent: cacheExtent,
maxPaintExtent: childExtent,
hitTestExtent: paintedChildSize,
hasVisualOverflow: childExtent > constraints.remainingPaintExtent ||
constraints.scrollOffset > 0.0,
);
setChildParentData(child!, constraints, geometry!);
}
}