How can I save and restore the scroll position of a Flutter ListView between app restarts?

1,100

Solution 1

We now have out of the box support for state restoration in Flutter. Check out restorationId property inside ScrollView widget. Check out this link to understand how to implement restoration in Flutter. https://dev.to/pedromassango/what-is-state-restoration-and-how-to-use-it-in-flutter-5blm

Solution 2

If I have to do it manually, when should I save the scroll position? (In a native Android app I would do it in the onPause Activity life-cycle method.)

You can use mixin WidgetsBindingObserver to detect when your app goes to background and when it comes to foreground by listening AppLifecycleState

The observable lifecycle events are:

  • paused — The application is not currently visible to the user, not responding to user input, and running in the background. It is like onPause() in Android
  • inactive — The application is in an inactive state and is not receiving user input. (only IOS)
  • resumed — The application is visible and responding to user input. It is like onPostResume() in Android
  • suspending — The application will be suspended momentarily. (only Android)

In example I save date instead of scroll position

class _TestPageState extends State<TestPage> with WidgetsBindingObserver {

  SharedPreferences prefs;

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print('state = $state'); // resumed, paused, inactive, suspending
    if (state == AppLifecycleState.paused) {
        prefs.setString('date', DateTime.now().toIso8601String());
    }
  }

  _TestPageState() {
    initPrefs();
  }

  void initPrefs() async {
    prefs = await SharedPreferences.getInstance();
    print(prefs.getString('date') ?? 'no date');
  }
  ...
Share:
1,100
TechAurelian
Author by

TechAurelian

Updated on December 14, 2022

Comments

  • TechAurelian
    TechAurelian over 1 year

    I'd like to persist the scroll location of a ListView so it is restored between app restarts.

    So I guess it should be saved on disk using something like shared_preferences (in NSUserDefaults on iOS and SharedPreferences on Android)

    Is there a builtin way (or plugin) to do that?

    If I have to do it manually, when should I save the scroll position? (In a native Android app I would do it in the onPause Activity life-cycle method.)

    I searched a lot, and I can't seem to find any post or sample code that shows how to save and restore the scroll position of a ListView.

    • CoolMind
      CoolMind over 4 years
      Why not in onSaveInstanceState in Android?
    • Admin
      Admin over 4 years
    • TechAurelian
      TechAurelian over 4 years
      @Eugene There is nothing wrong with shared_preferences. I'm asking if there is a builtin way (or a plugin) that already does that.