Skip to content

Commit a0e333b

Browse files
dnfieldPiinks
andauthored
Make RenderSliverGrid more accurately report overflow (#104064)
* Make RenderSliverGrid more accurately report overflow * Update packages/flutter/lib/src/rendering/sliver_grid.dart Co-authored-by: Kate Lovett <[email protected]> Co-authored-by: Kate Lovett <[email protected]>
1 parent 589dd80 commit a0e333b

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

packages/flutter/lib/src/rendering/sliver_grid.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ class RenderSliverGrid extends RenderSliverMultiBoxAdaptor {
658658
leadingScrollOffset: leadingScrollOffset,
659659
trailingScrollOffset: trailingScrollOffset,
660660
);
661-
662661
final double paintExtent = calculatePaintOffset(
663662
constraints,
664663
from: math.min(constraints.scrollOffset, leadingScrollOffset),
@@ -675,8 +674,7 @@ class RenderSliverGrid extends RenderSliverMultiBoxAdaptor {
675674
paintExtent: paintExtent,
676675
maxPaintExtent: estimatedTotalExtent,
677676
cacheExtent: cacheExtent,
678-
// Conservative to avoid complexity.
679-
hasVisualOverflow: true,
677+
hasVisualOverflow: estimatedTotalExtent > paintExtent || constraints.scrollOffset > 0.0 || constraints.overlap != 0.0,
680678
);
681679

682680
// We may have started the layout while scrolled to the end, which

packages/flutter/test/widgets/grid_view_test.dart

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,50 @@ void main() {
645645
expect(counters[4], 2);
646646
});
647647

648+
testWidgets('GridView does not report visual overflow unnecessarily', (WidgetTester tester) async {
649+
await tester.pumpWidget(
650+
Directionality(
651+
textDirection: TextDirection.ltr,
652+
child: GridView(
653+
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
654+
children: <Widget>[
655+
Container(height: 200.0),
656+
],
657+
),
658+
),
659+
);
660+
661+
// 1st, check that the render object has received the default clip behavior.
662+
final RenderViewport renderObject = tester.allRenderObjects.whereType<RenderViewport>().first;
663+
expect(renderObject.clipBehavior, equals(Clip.hardEdge));
664+
665+
// The context will get Clip.none because there is no actual visual overflow.
666+
final TestClipPaintingContext context = TestClipPaintingContext();
667+
renderObject.paint(context, Offset.zero);
668+
expect(context.clipBehavior, equals(Clip.none));
669+
});
670+
648671
testWidgets('GridView respects clipBehavior', (WidgetTester tester) async {
649672
await tester.pumpWidget(
650673
Directionality(
651674
textDirection: TextDirection.ltr,
652675
child: GridView(
653676
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
654-
children: <Widget>[Container(height: 2000.0)],
677+
children: <Widget>[
678+
Container(height: 2000.0),
679+
Container(height: 2000.0),
680+
Container(height: 2000.0),
681+
Container(height: 2000.0),
682+
Container(height: 2000.0),
683+
Container(height: 2000.0),
684+
Container(height: 2000.0),
685+
Container(height: 2000.0),
686+
Container(height: 2000.0),
687+
Container(height: 2000.0),
688+
Container(height: 2000.0),
689+
Container(height: 2000.0),
690+
Container(height: 2000.0),
691+
],
655692
),
656693
),
657694
);
@@ -672,7 +709,21 @@ void main() {
672709
child: GridView(
673710
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
674711
clipBehavior: Clip.antiAlias,
675-
children: <Widget>[Container(height: 2000.0)],
712+
children: <Widget>[
713+
Container(height: 2000.0),
714+
Container(height: 2000.0),
715+
Container(height: 2000.0),
716+
Container(height: 2000.0),
717+
Container(height: 2000.0),
718+
Container(height: 2000.0),
719+
Container(height: 2000.0),
720+
Container(height: 2000.0),
721+
Container(height: 2000.0),
722+
Container(height: 2000.0),
723+
Container(height: 2000.0),
724+
Container(height: 2000.0),
725+
Container(height: 2000.0),
726+
],
676727
),
677728
),
678729
);

0 commit comments

Comments
 (0)