Commit f4f68dff authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Make the page-view remembering logic actually work. (#9905)

It wasn't tested, which is why it didn't work before.
parent 3bd2ecb2
......@@ -154,7 +154,9 @@ class _PagePosition extends ScrollPositionWithSingleContext {
this.initialPage: 0,
double viewportFraction: 1.0,
ScrollPosition oldPosition,
}) : _viewportFraction = viewportFraction, super(
}) : _viewportFraction = viewportFraction,
_pageToUseOnStartup = initialPage.toDouble(),
super(
physics: physics,
context: context,
initialPixels: null,
......@@ -166,6 +168,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
}
final int initialPage;
double _pageToUseOnStartup;
double get viewportFraction => _viewportFraction;
double _viewportFraction;
......@@ -198,7 +201,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
if (pixels == null) {
final double value = PageStorage.of(context.storageContext)?.readState(context.storageContext);
if (value != null)
correctPixels(getPixelsFromPage(value));
_pageToUseOnStartup = value;
}
}
......@@ -207,7 +210,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
final double oldViewportDimensions = this.viewportDimension;
final bool result = super.applyViewportDimension(viewportDimension);
final double oldPixels = pixels;
final double page = (oldPixels == null || oldViewportDimensions == 0.0) ? initialPage.toDouble() : getPageFromPixels(oldPixels, oldViewportDimensions);
final double page = (oldPixels == null || oldViewportDimensions == 0.0) ? _pageToUseOnStartup : getPageFromPixels(oldPixels, oldViewportDimensions);
final double newPixels = getPixelsFromPage(page);
if (newPixels != oldPixels) {
correctPixels(newPixels);
......
......@@ -300,6 +300,9 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
/// The default implementation reads the value from the nearest [PageStorage]
/// found from the [context]'s [ScrollContext.storageContext] property, and
/// sets it using [correctPixels], if [pixels] is still null.
///
/// This method is called from the constructor, so layout has not yet
/// occurred, and the viewport dimensions aren't yet known when it is called.
@protected
void restoreScrollOffset() {
if (pixels == null) {
......
......@@ -398,4 +398,63 @@ void main() {
await tester.pump();
expect(changeIndex, 0);
});
testWidgets('PageView can restore page',
(WidgetTester tester) async {
final PageController controller = new PageController();
final PageStorageBucket bucket = new PageStorageBucket();
await tester.pumpWidget(
new PageStorage(
bucket: bucket,
child: new PageView(
controller: controller,
children: <Widget>[
const Placeholder(),
const Placeholder(),
const Placeholder(),
],
),
),
);
expect(controller.page, 0);
controller.jumpToPage(2);
expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 1);
expect(controller.page, 2);
await tester.pumpWidget(
new PageStorage(
bucket: bucket,
child: new Container(),
),
);
expect(() => controller.page, throwsAssertionError);
await tester.pumpWidget(
new PageStorage(
bucket: bucket,
child: new PageView(
controller: controller,
children: <Widget>[
const Placeholder(),
const Placeholder(),
const Placeholder(),
],
),
),
);
expect(controller.page, 2);
await tester.pumpWidget(
new PageStorage(
bucket: bucket,
child: new PageView(
key: const Key('Check it again against your list and see consistency!'),
controller: controller,
children: <Widget>[
const Placeholder(),
const Placeholder(),
const Placeholder(),
],
),
),
);
expect(controller.page, 0);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment