Skip to content

Commit c0921ab

Browse files
authored
Allow zero cross axis extent in SliverGridDelegateWithMaxCrossAxisExtent (flutter#194858)
`SliverGridDelegateWithMaxCrossAxisExtent` asserts `crossAxisExtent > 0.0` in `_debugAssertIsValid`. When the view size is zero, the grid gets a cross axis extent of 0.0 and the assert fails. This occurs, for example, when the app starts while the device screen is locked. `getLayout` already supports a zero cross axis extent: - It clamps `crossAxisCount` to a minimum of 1. The code comment says that the count can be zero "when the window size is 0". - It clamps `usableCrossAxisExtent` to a minimum of 0.0. Thus, only the assert rejects this value. This PR changes the assert to `crossAxisExtent >= 0.0`. A negative extent still fails the assert. `SliverGridDelegateWithFixedCrossAxisCount` does not assert on the cross axis extent, so the two delegates now agree on a zero extent. Fixes flutter#130069 ### Tests I added a regression test to `packages/flutter/test/widgets/grid_view_test.dart`. The test puts a `GridView.extent` in a `SizedBox` with a width of 0.0. It makes sure that layout throws no exception. | | New test | `test/widgets`, `test/material`, `test/cupertino`, `test/rendering` | |---|---|---| | Before | Fails: `'crossAxisExtent > 0.0': is not true` (`sliver_grid.dart` line 493) | 72 failures | | After | Passes (`grid_view_test.dart`: +30, all tests passed) | 72 failures, the same 72 tests as before | The 72 failures also occur on the base commit without this change. They are golden image tests and font tests that depend on the local environment. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant in-code documentation (doc comments with `///`). No doc change is necessary. - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). This PR does not add a feature. - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. The framework tests pass locally, except tests that also fail on the base commit. The other tests run only in CI. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 5a060fc commit c0921ab

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class SliverGridDelegateWithMaxCrossAxisExtent extends SliverGridDelegate {
490490
final double? mainAxisExtent;
491491

492492
bool _debugAssertIsValid(double crossAxisExtent) {
493-
assert(crossAxisExtent > 0.0);
493+
assert(crossAxisExtent >= 0.0);
494494
assert(maxCrossAxisExtent > 0.0);
495495
assert(mainAxisSpacing >= 0.0);
496496
assert(crossAxisSpacing >= 0.0);

‎packages/flutter/test/widgets/grid_view_test.dart‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,66 @@ void main() {
502502
expect(find.text('1'), findsNothing);
503503
});
504504

505+
// Regression test for https://github.com/flutter/flutter/issues/130069
506+
testWidgets('SliverGridDelegateWithMaxCrossAxisExtent lays out with zero cross axis extent', (
507+
WidgetTester tester,
508+
) async {
509+
var width = 0.0;
510+
late StateSetter setWidth;
511+
512+
await tester.pumpWidget(
513+
Directionality(
514+
textDirection: TextDirection.ltr,
515+
child: StatefulBuilder(
516+
builder: (BuildContext context, StateSetter setState) {
517+
setWidth = setState;
518+
return Center(
519+
child: SizedBox(
520+
width: width,
521+
height: 100.0,
522+
child: GridView.extent(
523+
maxCrossAxisExtent: 200.0,
524+
children: const <Widget>[Text('0'), Text('1'), Text('2'), Text('3')],
525+
),
526+
),
527+
);
528+
},
529+
),
530+
),
531+
);
532+
expect(tester.getSize(find.byType(GridView)), const Size(0.0, 100.0));
533+
expect(find.text('0'), findsNothing);
534+
535+
setWidth(() {
536+
width = 800.0;
537+
});
538+
await tester.pump();
539+
expect(tester.getSize(find.byType(GridView)), const Size(800.0, 100.0));
540+
expect(tester.getSize(find.text('0')), const Size(200.0, 200.0));
541+
expect(tester.getTopLeft(find.text('3')), const Offset(600.0, 250.0));
542+
543+
const delegate = SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 200.0);
544+
expect(
545+
() => delegate.getLayout(
546+
const SliverConstraints(
547+
axisDirection: AxisDirection.down,
548+
growthDirection: GrowthDirection.forward,
549+
userScrollDirection: ScrollDirection.idle,
550+
scrollOffset: 0.0,
551+
precedingScrollExtent: 0.0,
552+
overlap: 0.0,
553+
remainingPaintExtent: 100.0,
554+
crossAxisExtent: -1.0,
555+
crossAxisDirection: AxisDirection.right,
556+
viewportMainAxisExtent: 100.0,
557+
remainingCacheExtent: 100.0,
558+
cacheOrigin: 0.0,
559+
),
560+
),
561+
throwsAssertionError,
562+
);
563+
});
564+
505565
testWidgets('GridView in unbounded context', (WidgetTester tester) async {
506566
await tester.pumpWidget(
507567
Directionality(

0 commit comments

Comments
 (0)