Flutter documentation for ScrollController has this paragraph:
Scroll controllers are typically stored as member variables in State objects and are reused in each State.build. A single scroll controller can be used to control multiple scrollable widgets, but some operations, such as reading the scroll offset, require the controller to be used with a single scrollable widget.
Does this mean that we cannot pass the same ScrollController to different ScrollView widgets to read ScrollController.offset?
What I'm trying to accomplish is this:
- There are two screens. Each screen has a
ListView.builder()widget. - Through parameters I pass from screen 1 to screen 2 an object
ScrollControllerand apply it toListView. - I use scrolling and the
offsetvalue changes, but as soon as I move/return to another screen, theoffsetis knocked down to 0.0 and I see the beginning of the list. - The same
ScrollControllerobject is used all the time (hashcode is the same)
How can we use one ScrollController object for different ScrollView widgets, so that the offset is not knocked down when moving from screen to screen?
This problem can be solved a bit if, when switching to another screen, we create a new ScrollController object with initialScrollOffset = oldScrollController.offset and pass it to ScrollView.
Update:
I don't seem to understand how to use flutter_hooks. I created a simple example showing that if we use separate widgets and specify ScrollController as a parameter, the scroll is reset to position 0.0.
Reference for an example: https://dartpad.dev/?id=d31f4714ce95869716c18b911fee80c1
How do we overcome this?
For now, the best solution I can offer is to pass
final ValueNotifier<double> offsetState;instead offinal ScrollController controller;as a widget parameter.Then, in each widget we create a
ScrollController. By listening to it via theuseListenableSelectorhook we change theoffsetState.To avoid unnecessary rebuilding, we use the
useValueNotifierhook.A complete example looks like this:
Another idea was to use
useEffecthook and give it a function to save the last value at the moment ofdispose():But the problem is that at this point, we no longer have clients.
Bonus:
If our task is to synchronize the scroll of the
ListView, anotheruseListenableSelectorhook added to each of the widgets solves this problem. Remind that we cannot use the same `ScrollController' for two or more lists at the same time.