Skip to content

Commit

Permalink
Fix PageView throws Null check error (#104405)
Browse files Browse the repository at this point in the history
  • Loading branch information
bleroux authored Jun 8, 2022
1 parent f377f61 commit 4dec56b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/flutter/lib/src/widgets/page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,20 @@ class _PagePosition extends ScrollPositionWithSingleContext implements PageMetri
return result;
}

@override
void absorb(ScrollPosition other) {
super.absorb(other);
assert(_cachedPage == null);

if (other is! _PagePosition) {
return;
}

if (other._cachedPage != null) {
_cachedPage = other._cachedPage;
}
}

@override
bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) {
final double newMinScrollExtent = minScrollExtent + _initialPageOffset;
Expand Down
39 changes: 39 additions & 0 deletions packages/flutter/test/widgets/page_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,45 @@ void main() {
expect(find.text('Illinois'), findsOneWidget);
});

testWidgets('_PagePosition.applyViewportDimension should not throw', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/101007
final PageController controller = PageController(
initialPage: 1,
);

// Set the starting viewportDimension to 0.0
await tester.binding.setSurfaceSize(Size.zero);
final MediaQueryData mediaQueryData = MediaQueryData.fromWindow(tester.binding.window);

Widget build(Size size) {
return MediaQuery(
data: mediaQueryData.copyWith(size: size),
child: Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox.expand(
child: PageView(
controller: controller,
onPageChanged: (int page) { },
children: kStates.map<Widget>((String state) => Text(state)).toList(),
),
),
),
),
);
}

await tester.pumpWidget(build(Size.zero));
const Size surfaceSize = Size(500,400);
await tester.binding.setSurfaceSize(surfaceSize);
await tester.pumpWidget(build(surfaceSize));

expect(tester.takeException(), isNull);

// Reset TestWidgetsFlutterBinding surfaceSize
await tester.binding.setSurfaceSize(null);
});

testWidgets('PageController cannot return page while unattached',
(WidgetTester tester) async {
final PageController controller = PageController();
Expand Down

0 comments on commit 4dec56b

Please sign in to comment.