Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5dee1c7

Browse files
committed
Merge pull request #704 from abarth/scrollable_padding
Fix padding on infinite scrolling list
2 parents 821410b + 20d37cb commit 5dee1c7

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

sky/packages/sky/lib/widgets/scrollable.dart

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,30 @@ abstract class FixedHeightScrollable extends Scrollable {
384384
});
385385
}
386386

387+
double get _leadingPadding {
388+
if (scrollDirection == ScrollDirection.vertical)
389+
return padding.top;
390+
return padding.left;
391+
}
392+
393+
double get _trailingPadding {
394+
if (scrollDirection == ScrollDirection.vertical)
395+
return padding.bottom;
396+
return padding.right;
397+
}
398+
399+
EdgeDims get _crossAxisPadding {
400+
if (padding == null)
401+
return null;
402+
if (scrollDirection == ScrollDirection.vertical)
403+
return new EdgeDims.only(left: padding.left, right: padding.right);
404+
return new EdgeDims.only(top: padding.top, bottom: padding.bottom);
405+
}
406+
387407
void _updateContentsExtent() {
388408
double contentsExtent = itemExtent * itemCount;
389-
if (padding != null) {
390-
if (scrollDirection == ScrollDirection.vertical)
391-
contentsExtent += padding.top + padding.bottom;
392-
else
393-
contentsExtent += padding.left + padding.right;
394-
}
409+
if (padding != null)
410+
contentsExtent += _leadingPadding + _trailingPadding;
395411
scrollBehavior.contentsSize = contentsExtent;
396412
}
397413

@@ -413,25 +429,29 @@ abstract class FixedHeightScrollable extends Scrollable {
413429
_updateScrollOffset();
414430
}
415431

432+
double paddedScrollOffset = scrollOffset;
433+
if (padding != null)
434+
paddedScrollOffset -= _leadingPadding;
435+
416436
int itemShowIndex = 0;
417437
int itemShowCount = 0;
418438
Offset viewportOffset = Offset.zero;
419439
if (_containerExtent != null && _containerExtent > 0.0) {
420-
if (scrollOffset < 0.0) {
421-
double visibleHeight = _containerExtent + scrollOffset;
440+
if (paddedScrollOffset < 0.0) {
441+
double visibleHeight = _containerExtent + paddedScrollOffset;
422442
itemShowCount = (visibleHeight / itemExtent).round() + 1;
423-
viewportOffset = _toOffset(scrollOffset);
443+
viewportOffset = _toOffset(paddedScrollOffset);
424444
} else {
425445
itemShowCount = (_containerExtent / itemExtent).ceil();
426-
double alignmentDelta = -scrollOffset % itemExtent;
446+
double alignmentDelta = -paddedScrollOffset % itemExtent;
427447
double drawStart;
428448
if (alignmentDelta != 0.0) {
429449
alignmentDelta -= itemExtent;
430450
itemShowCount += 1;
431-
drawStart = scrollOffset + alignmentDelta;
451+
drawStart = paddedScrollOffset + alignmentDelta;
432452
viewportOffset = _toOffset(-alignmentDelta);
433453
} else {
434-
drawStart = scrollOffset;
454+
drawStart = paddedScrollOffset;
435455
}
436456
itemShowIndex = math.max(0, (drawStart / itemExtent).floor());
437457
}
@@ -453,7 +473,7 @@ abstract class FixedHeightScrollable extends Scrollable {
453473
scrollDirection: scrollDirection,
454474
scrollOffset: viewportOffset,
455475
child: new Container(
456-
padding: padding,
476+
padding: _crossAxisPadding,
457477
child: new Block(items, direction: blockDirection)
458478
)
459479
)

0 commit comments

Comments
 (0)