infinite_scroll_pagination icon indicating copy to clipboard operation
infinite_scroll_pagination copied to clipboard

[Feature] Scroll to previous pages when setting firstPageKey

Open vintage opened this issue 4 years ago • 40 comments

The PagingController can be built with firstPageKey to indicate initial page to load. Sounds great, but how to "scroll back" to previous pages then?

I'm implementing a players ranking list where the initial page is dynamically set based on position of current user in the leaderboard.

Example

User enters the screen and sees 5th page of the results. He can scroll down to see further pages (6,7,8), but I have to provide some mechanism to be able to scroll up (to pages 4,3,2,1). Is that ever possible?

Already tried to use addStatusListener/addListener, but nothing interesting is triggered there.

vintage avatar Feb 19 '21 15:02 vintage

Hi @vintage . This is currently not possible, but you're the third one asking for this feature. If more demand comes in, I'll definitely give it a shot.

EdsonBueno avatar Feb 19 '21 16:02 EdsonBueno

Well, so why closing all of the issues instead of keeping one of these open for discussion or even making it visible to the community that help is wanted?

vintage avatar Feb 19 '21 19:02 vintage

I was going to use another issue for this @vintage , but let's use this one. I think you explained what you want in a better way.

EdsonBueno avatar Mar 03 '21 09:03 EdsonBueno

I would love this as well!

namanshergill avatar Mar 29 '21 20:03 namanshergill

I would also like to see this feature added!

tobysimone avatar Apr 26 '21 02:04 tobysimone

I need this feature too.

alejandrogiubel avatar Apr 28 '21 18:04 alejandrogiubel

+1

wizard11 avatar May 07 '21 21:05 wizard11

Is this on the roadmap/planned for future? This will be a great feature to have to have a much better control over the pagination.

namanshergill avatar May 27 '21 06:05 namanshergill

Yes, @NamanShergill, it definitely is! I just couldn't allocate the time for it yet, stuffed with work.

EdsonBueno avatar May 27 '21 10:05 EdsonBueno

Yes, @NamanShergill, it definitely is! I just couldn't allocate the time for it yet, stuffed with work.

That's awesome!

namanshergill avatar May 27 '21 14:05 namanshergill

@EdsonBueno any estimate of when it can be available?

sabetAI avatar Jun 01 '21 17:06 sabetAI

Really useful feature!

DesmondFox avatar Jun 03 '21 09:06 DesmondFox

+1

sebastianopighi avatar Jun 10 '21 20:06 sebastianopighi

+1

zzwx avatar Jun 13 '21 16:06 zzwx

+1

changlan avatar Jun 27 '21 08:06 changlan

+1

sopherwang avatar Jun 29 '21 02:06 sopherwang

+1

dush999 avatar Jul 05 '21 08:07 dush999

+1

dushmantha-b avatar Jul 05 '21 08:07 dushmantha-b

+1 Useful feature!

dushmantha-b avatar Jul 05 '21 08:07 dushmantha-b

I need this feature too +1

mohamadhadibi avatar Jul 21 '21 04:07 mohamadhadibi

+1 Useful!

tomasweigenast avatar Jul 27 '21 19:07 tomasweigenast

At the moment, for lazy loading in both directions, you can try using the implementation with Viewport.

class LoadMoreUpAndDown extends StatefulWidget {
  const LoadMoreUpAndDown({Key? key}) : super(key: key);

  @override
  _LoadMoreUpAndDownState createState() => _LoadMoreUpAndDownState();
}

class _LoadMoreUpAndDownState extends State<LoadMoreUpAndDown> {
  final Key downListKey = UniqueKey();
  static const _pageSize = 20;

  final PagingController<int, Message> _pagingReplyUpController = PagingController(
    firstPageKey: 0,
  );
  final PagingController<int, Message> _pagingReplyDownController = PagingController(
    firstPageKey: 0,
  );

  Future<void> _fetchUpPage(int pageKey) async {
    try {
      final newItems = await RemoteApi.getMessagesList(pageKey, _pageSize);
      final isLastPage = newItems.length < _pageSize;
      if (isLastPage) {
        _pagingReplyUpController.appendLastPage(newItems);
      } else {
        final nextPageKey = pageKey + newItems.length;
        _pagingReplyUpController.appendPage(newItems, nextPageKey);
      }
    } catch (error) {
      _pagingReplyUpController.error = error;
    }
  }

  Future<void> _fetchDownPage(int pageKey) async {
    try {
      final newItems = await RemoteApi.getMessagesList(pageKey, _pageSize);
      final isLastPage = newItems.length < _pageSize;
      if (isLastPage) {
        _pagingReplyDownController.appendLastPage(newItems);
      } else {
        final nextPageKey = pageKey + newItems.length;
        _pagingReplyDownController.appendPage(newItems, nextPageKey);
      }
    } catch (error) {
      _pagingReplyDownController.error = error;
    }
  }

  @override
  void initState() {
    super.initState();
    _pagingReplyUpController.addPageRequestListener((pageKey) {
      _fetchUpPage(pageKey);
    });

    _pagingReplyDownController.addPageRequestListener((pageKey) {
      _fetchDownPage(pageKey);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scrollable(
      viewportBuilder: (BuildContext context, ViewportOffset position) {
        return Viewport(
          offset: position,
          center: downListKey,
          slivers: [
            PagedSliverList(
              pagingController: _pagingReplyUpController,
              builderDelegate: PagedChildBuilderDelegate<Message>(
                itemBuilder: (context, messageUp, index) => SizedBox(
                  height: 50,
                  width: double.infinity,
                  child: Text(
                    '${messageUp.text}',
                  ),
                ),
              ),
            ),
            PagedSliverList(
              key: downListKey,
              pagingController: _pagingReplyDownController,
              builderDelegate: PagedChildBuilderDelegate<Message>(
                itemBuilder: (context, messageDown, index) => SizedBox(
                  height: 50,
                  width: double.infinity,
                  child: Text(
                    '${messageDown.text}',
                  ),
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  @override
  void dispose() {
    _pagingReplyUpController.dispose();
    _pagingReplyDownController.dispose();
    super.dispose();
  }
}

IvanAleksandrov94 avatar Jul 31 '21 19:07 IvanAleksandrov94

I'm in a similar situation to the OP. I have potentially large number of items (possibly hundreds of thousands) representing the rankings of players in a game. I want the initial view to show the position of the logged in player, and to be able to scroll up and down from that position.

PaulRudin avatar Sep 23 '21 05:09 PaulRudin

Hi, Any news about the issue?

gorkemsari avatar Sep 24 '21 13:09 gorkemsari

+1

bobbyqul avatar Oct 17 '21 21:10 bobbyqul

Can any of you confirm @IvanAleksandrov94's implementation is what you're looking for? If yes, I can expand on it and make it into the package. If not, what's missing?

EdsonBueno avatar Oct 18 '21 07:10 EdsonBueno

need this feature too.

desmeit avatar Jan 26 '22 15:01 desmeit

+1

liemeon avatar Apr 29 '22 05:04 liemeon

+1

jesusmartinoza avatar May 02 '22 20:05 jesusmartinoza

sticky_infinite_list << I found it! perfect!!!

liemeon avatar May 03 '22 08:05 liemeon