Skip to content

Commit

Permalink
[CP] Fix lower bound of children from TwoDimensionalChildBuilderDeleg…
Browse files Browse the repository at this point in the history
…ate (flutter#132764)

Found in flutter/packages#4536

The max x and max y index  should allow for a case where there are no children in the viewport. This should be CP'd into stable once it lands.
  • Loading branch information
Piinks authored Aug 24, 2023
1 parent e1e4722 commit ff5b5b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
12 changes: 7 additions & 5 deletions packages/flutter/lib/src/widgets/scroll_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,8 @@ class TwoDimensionalChildBuilderDelegate extends TwoDimensionalChildDelegate {
required this.builder,
int? maxXIndex,
int? maxYIndex,
}) : assert(maxYIndex == null || maxYIndex >= 0),
assert(maxXIndex == null || maxXIndex >= 0),
}) : assert(maxYIndex == null || maxYIndex >= -1),
assert(maxXIndex == null || maxXIndex >= -1),
_maxYIndex = maxYIndex,
_maxXIndex = maxXIndex;

Expand Down Expand Up @@ -976,7 +976,9 @@ class TwoDimensionalChildBuilderDelegate extends TwoDimensionalChildDelegate {
/// [TwoDimensionalViewport] subclass to learn how this value is applied in
/// the specific use case.
///
/// If not null, the value must be non-negative.
/// If not null, the value must be greater than or equal to -1, where -1
/// indicates there will be no children at all provided to the
/// [TwoDimensionalViewport].
///
/// If the value changes, the delegate will call [notifyListeners]. This
/// informs the [RenderTwoDimensionalViewport] that any cached information
Expand All @@ -997,7 +999,7 @@ class TwoDimensionalChildBuilderDelegate extends TwoDimensionalChildDelegate {
if (value == maxXIndex) {
return;
}
assert(value == null || value >= 0);
assert(value == null || value >= -1);
_maxXIndex = value;
notifyListeners();
}
Expand All @@ -1020,7 +1022,7 @@ class TwoDimensionalChildBuilderDelegate extends TwoDimensionalChildDelegate {
if (maxYIndex == value) {
return;
}
assert(value == null || value >= 0);
assert(value == null || value >= -1);
_maxYIndex = value;
notifyListeners();
}
Expand Down
18 changes: 10 additions & 8 deletions packages/flutter/test/widgets/two_dimensional_viewport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,37 @@ void main() {
}
);
// Update
delegate.maxXIndex = -1; // No exception.
expect(
() {
delegate.maxXIndex = -1;
delegate.maxXIndex = -2;
},
throwsA(
isA<AssertionError>().having(
(AssertionError error) => error.toString(),
'description',
contains('value == null || value >= 0'),
contains('value == null || value >= -1'),
),
),
);
delegate.maxYIndex = -1; // No exception
expect(
() {
delegate.maxYIndex = -1;
delegate.maxYIndex = -2;
},
throwsA(
isA<AssertionError>().having(
(AssertionError error) => error.toString(),
'description',
contains('value == null || value >= 0'),
contains('value == null || value >= -1'),
),
),
);
// Constructor
expect(
() {
TwoDimensionalChildBuilderDelegate(
maxXIndex: -1,
maxXIndex: -2,
maxYIndex: 0,
builder: (BuildContext context, ChildVicinity vicinity) {
return const SizedBox.shrink();
Expand All @@ -159,15 +161,15 @@ void main() {
isA<AssertionError>().having(
(AssertionError error) => error.toString(),
'description',
contains('maxXIndex == null || maxXIndex >= 0'),
contains('maxXIndex == null || maxXIndex >= -1'),
),
),
);
expect(
() {
TwoDimensionalChildBuilderDelegate(
maxXIndex: 0,
maxYIndex: -1,
maxYIndex: -2,
builder: (BuildContext context, ChildVicinity vicinity) {
return const SizedBox.shrink();
}
Expand All @@ -177,7 +179,7 @@ void main() {
isA<AssertionError>().having(
(AssertionError error) => error.toString(),
'description',
contains('maxYIndex == null || maxYIndex >= 0'),
contains('maxYIndex == null || maxYIndex >= -1'),
),
),
);
Expand Down

0 comments on commit ff5b5b5

Please sign in to comment.