Skip to content

Preserve scroll positions on navigation #386

@pitriq

Description

@pitriq

What feature would you like to see?

As mentioned in #252, pages are currently disposed when navigating away from them — and along with that, their scroll positions are lost too.

It seems like this could eventually be solved once there’s a way to prevent pages from being disposed on navigation. In the meantime, since the package already keeps a ScrollController for each page (unless the user provides their own), would it be possible to add something to persist scroll positions when navigating away and back?

For now, I’ve worked around this with a pretty hacky setup:

final offsetNotifier = ValueNotifier<double>(0);
final scrollController = ScrollController(
  onAttach: (position) {
    SchedulerBinding.instance.addPostFrameCallback((_) {
      position.jumpTo(offsetNotifier.value);
    });
  },
  onDetach: (position) {
    offsetNotifier.value = position.pixels;
  },
);

I just pass that controller to the page to manually keep the scroll position between navigations.

Current behaviour:

wolt.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions