Skip to content

Commit

Permalink
sliverGridDelegate mainAxisExtent add assert (#148470)
Browse files Browse the repository at this point in the history
* sliverGridDelegate mainAxisExtent add assert,Avoid incorrect use *

Fixes flutter/flutter#138871
  • Loading branch information
hello-coder-xu authored May 29, 2024
1 parent b154032 commit af5a1a9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/flutter/lib/src/rendering/sliver_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ class SliverGridDelegateWithFixedCrossAxisCount extends SliverGridDelegate {
}) : assert(crossAxisCount > 0),
assert(mainAxisSpacing >= 0),
assert(crossAxisSpacing >= 0),
assert(childAspectRatio > 0);
assert(childAspectRatio > 0),
assert(mainAxisExtent == null || mainAxisExtent >= 0);

/// The number of children in the cross axis.
final int crossAxisCount;
Expand Down Expand Up @@ -446,7 +447,8 @@ class SliverGridDelegateWithMaxCrossAxisExtent extends SliverGridDelegate {
}) : assert(maxCrossAxisExtent > 0),
assert(mainAxisSpacing >= 0),
assert(crossAxisSpacing >= 0),
assert(childAspectRatio > 0);
assert(childAspectRatio > 0),
assert(mainAxisExtent == null || mainAxisExtent >= 0);

/// The maximum extent of tiles in the cross axis.
///
Expand Down
49 changes: 49 additions & 0 deletions packages/flutter/test/widgets/grid_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -898,4 +898,53 @@ void main() {
expect(controller.position.maxScrollExtent, 472.0);
expect(controller.position.pixels, 472.0);
});

testWidgets('SliverGridDelegate mainAxisExtent add assert', (WidgetTester tester) async {
Widget buildGridView(SliverGridDelegate delegate) {
return Directionality(
textDirection: TextDirection.ltr,
child: GridView.builder(
gridDelegate: delegate,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 50,
alignment: Alignment.center,
child: Text('$index'),
);
},
itemCount: 50,
),
);
}

expect(
() => tester.pumpWidget(buildGridView(SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
mainAxisExtent: -100,
))),
throwsA(isAssertionError.having(
(AssertionError e) => e.toString(),
'.toString()',
contains("'mainAxisExtent == null || mainAxisExtent >= 0': is not true."),
)),
);

expect(
() => tester.pumpWidget(buildGridView(SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 100,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
mainAxisExtent: -100,
))),
throwsA(isAssertionError.having(
(AssertionError e) => e.toString(),
'.toString()',
contains("'mainAxisExtent == null || mainAxisExtent >= 0': is not true."),
)),
);
});


}

0 comments on commit af5a1a9

Please sign in to comment.