Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs for [PageStorage] #63634

Merged
merged 3 commits into from Aug 21, 2020
Merged

Conversation

xu-baolin
Copy link
Member

@xu-baolin xu-baolin commented Aug 13, 2020

Description

Improve docs for [PageStorage]
Because the default value of [ScrollController.keepScrollOffset] is true, When more than one scrollable ([ListView], [SingleChildScrollView], [TextField], etc.) appears in the same route, if forgot to give PageStorageKey or set keepScrollOffset false, the behavior of 'PageStorage' is strange. like this #62332

For example:
We have three scrollable widgets in one route(The sample code see #63656

A -> B -> C

Case 1:
If we only want to save A's state, we should do this:

A(key:PageStorageKey, keepScrollOffset:true) -> B(key:null, keepScrollOffset:false)-> C(key:null, keepScrollOffset:false)

Case 2:
If we want to save all of their states, we should do this:
A(key1:PageStorageKey, keepScrollOffset:true) -> B(key2:PageStorageKey, keepScrollOffset:true)-> C(key3:PageStorageKey, keepScrollOffset:true)

A _StorageEntryIdentifier:[key1]
B _StorageEntryIdentifier:[key2,key1]
C _StorageEntryIdentifier:[key3,key2,key1]
So, in this situation, the key1, key2, and key3 can be equal.

Case 3:
Wrong code demo:
If we only want to save A's state, but we do below(keepScrollOffset default value is true):
A(key:PageStorageKey, keepScrollOffset:true) -> B(key:null, keepScrollOffset:true)-> C(key:null, keepScrollOffset:true)

A _StorageEntryIdentifier:[key1]
B _StorageEntryIdentifier:[key1]
C _StorageEntryIdentifier:[key1]
They have the same _StorageEntryIdentifier, so the save position will be overridden by each other.

Related Issues

#62332

Tests

Not needed.

Checklist

Before you create this PR, confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Aug 13, 2020
/// [PageStorageKey] is used by [Scrollable] if [ScrollController.keepScrollOffset]
/// is enabled to save their [ScrollPosition]s. When more than one
/// scrollable ([ListView], [SingleChildScrollView], [TextField], etc.) appears
/// in the same route, if you want to save all of their positions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure if it's only when they appear in the same route, or should it be global unique?

Copy link
Member Author

@xu-baolin xu-baolin Aug 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not need global unique, but should be equal between rebuilding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like you can give them the same key, it works well. Because the identifier is a key chain

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, "in the same route" is not precise enough, isn't it?

Suggested change
/// in the same route, if you want to save all of their positions,
/// within the widget's closest ancestor [PageStorage] (such as within the same route), if you want to save all of their positions,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also "if you want to save all of their positions" should be "if you want to save all of their positions independently" followed by "unique [PageStorageKey]s". Otherwise, their positions ARE stored, but overridden by each other.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE suggesting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@dkwingsmt dkwingsmt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@flutter flutter deleted a comment from xu-baolin Aug 14, 2020
Copy link
Contributor

@dkwingsmt dkwingsmt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions after a 2nd read through; feel free to correct me if I'm wrong.

(Sorry for deleting your comment by mistake)

/// [PageStorageKey] is used by [Scrollable] if [ScrollController.keepScrollOffset]
/// is enabled to save their [ScrollPosition]s. When more than one
/// scrollable ([ListView], [SingleChildScrollView], [TextField], etc.) appears
/// in the same route, if you want to save all of their positions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, "in the same route" is not precise enough, isn't it?

Suggested change
/// in the same route, if you want to save all of their positions,
/// within the widget's closest ancestor [PageStorage] (such as within the same route), if you want to save all of their positions,

/// [PageStorageKey] is used by [Scrollable] if [ScrollController.keepScrollOffset]
/// is enabled to save their [ScrollPosition]s. When more than one
/// scrollable ([ListView], [SingleChildScrollView], [TextField], etc.) appears
/// in the same route, if you want to save all of their positions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also "if you want to save all of their positions" should be "if you want to save all of their positions independently" followed by "unique [PageStorageKey]s". Otherwise, their positions ARE stored, but overridden by each other.

@dkwingsmt
Copy link
Contributor

@HansMuller Was it you who wrote PageStorageKey? I wonder why it isn't the default behavior that each scrollable uses an independent key. I would expect scrolling position overriding each other to be the rare use case here.

@xu-baolin
Copy link
Member Author

@HansMuller Was it you who wrote PageStorageKey? I wonder why it isn't the default behavior that each scrollable uses an independent key. I would expect scrolling position overriding each other to be the rare use case here.

I have the same doubt, Such an implementation also will cause some strange behavior when modifying the middle of the key chain, like this #63656

Copy link
Contributor

@dkwingsmt dkwingsmt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice job

@dkwingsmt
Copy link
Contributor

Let's get this PR merged and discuss the changing proposal in another issue.

@fluttergithubbot fluttergithubbot merged commit 2f2130a into flutter:master Aug 21, 2020
smadey pushed a commit to smadey/flutter that referenced this pull request Aug 27, 2020
mingwandroid pushed a commit to mingwandroid/flutter that referenced this pull request Sep 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants