gildaswise / firestore_ui

A port of firebase_auth's FirebaseAnimatedList to Firestore, called FirestoreAnimatedList
BSD 3-Clause "New" or "Revised" License
35 stars 28 forks source link

How to save scroll position? #21

Closed ciriousjoker closed 4 years ago

ciriousjoker commented 4 years ago

I'm trying to use this inside a tab view, but I cannot get the scroll position to be remembered when i come back to the tab...

For context, this is my code ``` void returnTabs() { TabBarView( children: [ SomeOtherTab(), FirestoreAnimatedList( key: _keyPageStorage, controller: _scrollController, query: Firestore.instance .collection(Collections.users) .orderBy("created", descending: true), itemBuilder: ( BuildContext context, DocumentSnapshot snapshot, Animation animation, int index, ) => FadeTransition( opacity: animation, child: FeedCardWidget(), ), ), ], ); } ```
ciriousjoker commented 4 years ago

I found this workaround:

class Foo extends StatefulWidget {
  @override
  _FooState createState() => _FooState();
}

class _FooState extends State<Foo> with AutomaticKeepAliveClientMixin {
  @override
  bool wantKeepAlive = true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container();
  }
}

Source

The drawback is that it's more resource intensive, but it's actually exactly what I wanted. Since it basically keeps drawing the other tab offscreen, the scroll position stays the same obviously.

I don't yet know the performance implications of this, hopefully I remember to update this once I'm done implementing it.

After some testing, I found that the code above actually somewhat works, the only issue is that it "jumps around" when trying to restore scroll position. Instead of going to the bottom, it moves a bit up. Looks pretty weird, but could be caused because my list items have different heights. Anyway, since the workaround seems to be what I wanted in the first place, I'm not going to look into this any further.

TLDR

gildaswise commented 4 years ago

Hey, this is exactly my solution on my projects, isn't really something I can solve inside the library due to how the queries work.

Also, your code has a function that's returning a Widget, and usually that leads to more builds and state management issues, including scrolling position, check this out and try to create new widgets instead of functions!