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 { ...@@ -154,7 +154,9 @@ class _PagePosition extends ScrollPositionWithSingleContext {
this.initialPage: 0, this.initialPage: 0,
double viewportFraction: 1.0, double viewportFraction: 1.0,
ScrollPosition oldPosition, ScrollPosition oldPosition,
}) : _viewportFraction = viewportFraction, super( }) : _viewportFraction = viewportFraction,
_pageToUseOnStartup = initialPage.toDouble(),
super(
physics: physics, physics: physics,
context: context, context: context,
initialPixels: null, initialPixels: null,
...@@ -166,6 +168,7 @@ class _PagePosition extends ScrollPositionWithSingleContext { ...@@ -166,6 +168,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
} }
final int initialPage; final int initialPage;
double _pageToUseOnStartup;
double get viewportFraction => _viewportFraction; double get viewportFraction => _viewportFraction;
double _viewportFraction; double _viewportFraction;
...@@ -198,7 +201,7 @@ class _PagePosition extends ScrollPositionWithSingleContext { ...@@ -198,7 +201,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
if (pixels == null) { if (pixels == null) {
final double value = PageStorage.of(context.storageContext)?.readState(context.storageContext); final double value = PageStorage.of(context.storageContext)?.readState(context.storageContext);
if (value != null) if (value != null)
correctPixels(getPixelsFromPage(value)); _pageToUseOnStartup = value;
} }
} }
...@@ -207,7 +210,7 @@ class _PagePosition extends ScrollPositionWithSingleContext { ...@@ -207,7 +210,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
final double oldViewportDimensions = this.viewportDimension; final double oldViewportDimensions = this.viewportDimension;
final bool result = super.applyViewportDimension(viewportDimension); final bool result = super.applyViewportDimension(viewportDimension);
final double oldPixels = pixels; 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); final double newPixels = getPixelsFromPage(page);
if (newPixels != oldPixels) { if (newPixels != oldPixels) {
correctPixels(newPixels); correctPixels(newPixels);
......
...@@ -300,6 +300,9 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics { ...@@ -300,6 +300,9 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
/// The default implementation reads the value from the nearest [PageStorage] /// The default implementation reads the value from the nearest [PageStorage]
/// found from the [context]'s [ScrollContext.storageContext] property, and /// found from the [context]'s [ScrollContext.storageContext] property, and
/// sets it using [correctPixels], if [pixels] is still null. /// 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 @protected
void restoreScrollOffset() { void restoreScrollOffset() {
if (pixels == null) { if (pixels == null) {
......
...@@ -398,4 +398,63 @@ void main() { ...@@ -398,4 +398,63 @@ void main() {
await tester.pump(); await tester.pump();
expect(changeIndex, 0); 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