Skip to content

Conversation

@otto-dev
Copy link
Contributor

@otto-dev otto-dev commented Jul 31, 2022

In SliverAnimatedListState, the insertItem() and removeItem() index parameters are defined as if the removeItem() operation removed the corresponding list entry immediately. The entry is only actually removed from the ListView when the remove animation finishes.

_indexToItemIndex is used to convert the user-supplied index to the internal index, which takes item removal into account.

The user can also provide a findChildIndexCallback to the SliverAnimatedList which is passed on to the internal SliverChildBuilderDelegate. Since delegate renders all items, including items being removed, the same _indexToItemIndex needs to be applied to the indices returned by the user-provided function. Otherwise it is impossible for the user to implement findChildIndexCallback, because the internal state of the SliverAnimatedList and hence the current content of the delegate is not known to the user.

findChildIndexCallback indices originate from the same user-facing source of truth as insertItem() and removeItem(), and hence must likewise be converted to internal indices.

The call to _indexToItemIndex in the inner findChildIndexCallback was previously omitted by oversight, making it impossible for the user to implement findChildIndexCallback correctly, resulting in unwanted rebuilds and state loss. This PR fixes this.

@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Jul 31, 2022
@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 (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

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

@google-cla
Copy link

google-cla bot commented Jul 31, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@otto-dev otto-dev force-pushed the fix/SliverAnimatedList/findChildIndexCallback/add-indexToItemIndex branch from 5195766 to de4cc53 Compare August 1, 2022 02:05
@otto-dev otto-dev force-pushed the fix/SliverAnimatedList/findChildIndexCallback/add-indexToItemIndex branch from 0e1c961 to 24fca4b Compare August 1, 2022 03:02
Comment on lines +417 to +419
expect(reorderedListEntries[0].data, equals('item 3'));
expect(reorderedListEntries[1].data, equals('item 1'));
expect(reorderedListEntries[2].data, equals('item 2'));
Copy link
Contributor Author

@otto-dev otto-dev Aug 1, 2022

Choose a reason for hiding this comment

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

This fails on master - hence this PR.

@otto-dev
Copy link
Contributor Author

otto-dev commented Aug 1, 2022

CLA signed, test added, PR ready for review.

@goderbauer goderbauer requested a review from chunhtai August 2, 2022 22:10
Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

LGTM, just some nitpicking

_itemBuilder,
childCount: _itemsCount,
findChildIndexCallback: widget.findChildIndexCallback,
findChildIndexCallback: widget.findChildIndexCallback == null ? null : (Key key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should follow the other in line if else format if it is too long

findChildIndexCallback: widget.findChildIndexCallback == null
  ? null
  : (Key key) {
       ....
     }

slivers: <Widget>[
SliverAnimatedList(
key: listKey,
initialItemCount: items.length, // we will insert one item later
Copy link
Contributor

Choose a reason for hiding this comment

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

avoid we

expect(tester.getTopLeft(find.text('item 0')).dy, 200);
});

testWidgets('passes correctly derived index of findChildIndexCallback to inner SliverChildBuilderDelegate', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
testWidgets('passes correctly derived index of findChildIndexCallback to inner SliverChildBuilderDelegate', (WidgetTester tester) async {
testWidgets('passes correctly derived index of findChildIndexCallback to the inner SliverChildBuilderDelegate', (WidgetTester tester) async {

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

LGTM

@chunhtai chunhtai requested a review from Piinks August 3, 2022 23:08
@chunhtai
Copy link
Contributor

chunhtai commented Aug 3, 2022

cc @Piinks for secondary review

Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

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

Flutter_LGTM
Thank you!

@Piinks Piinks added f: scrolling Viewports, list views, slivers, etc. autosubmit Merge PR when tree becomes green via auto submit App labels Aug 5, 2022
@auto-submit auto-submit bot merged commit 5c44057 into flutter:master Aug 5, 2022
@otto-dev otto-dev deleted the fix/SliverAnimatedList/findChildIndexCallback/add-indexToItemIndex branch August 5, 2022 22:58
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Aug 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 7, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 7, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 8, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 8, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 8, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 8, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 8, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 8, 2022
camsim99 pushed a commit to camsim99/flutter that referenced this pull request Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants