GithubHelp home page GithubHelp logo

Comments (22)

maulik-benzatine avatar maulik-benzatine commented on August 23, 2024 6
itemPositionsListener.itemPositions.addListener(() {
      if (itemPositionsListener.itemPositions.value.last.index > (dataList.length - 3)) {
            // do your logic
      }
    });

from flutter.widgets.

phong764119 avatar phong764119 commented on August 23, 2024 3

Yes please something like this
_controller = ScrollController();
_controller.addListener(_scrollListener);

from flutter.widgets.

GanZhiXiong avatar GanZhiXiong commented on August 23, 2024 3

@tarobins
I also need this feature because I want to be able to click a button to go back to the top of the list.
Thank you very much for providing the plug-in and look forward to your reply.

Because I want to realize it as soon as possible, can you provide me with a method?

from flutter.widgets.

tarobins avatar tarobins commented on August 23, 2024 3

If currentMax is the index of the last item currently in the list, I think something like

needToPaginate = itemPositionsListener.itemPositions.value.firstWhere(
        (ItemPosition position) => position.index == currentMax, orElse: () => null)
    ?.itemTrailingEdge <= 1.0 ?? false; 

Haven't tested, not sure if it even compiles, but something like that should work.

from flutter.widgets.

dimityrivanov avatar dimityrivanov commented on August 23, 2024 3

Just attach the ScrollablePositionedList with NotificationListener and calculate the progress -> double progress = notification.metrics.pixels /
notification.metrics.maxScrollExtent;

from flutter.widgets.

tarobins avatar tarobins commented on August 23, 2024 2

@Zazo032

Sorry the docs are wrong and I'll correct them, but I think you should be able to do

itemPositionsListener.itemPositions.addListener((positions) => ...);

let me know if that doesn't work. I'll update the docs and investigate a bit more.

[edit]
ohhhh sorry, my mistake, you'll need to do something like

itemPositionsListener.itemPositions.addListener(() {
   doSomethingWith(itemPositionsListener.itemPositions.value); 
});

I'll update the docs.

from flutter.widgets.

ryosuji avatar ryosuji commented on August 23, 2024 2

If currentMax is the index of the last item currently in the list, I think something like

needToPaginate = itemPositionsListener.itemPositions.value.firstWhere(
        (ItemPosition position) => position.index == currentMax, orElse: () => null)
    ?.itemTrailingEdge <= 1.0 ?? false; 

Haven't tested, not sure if it even compiles, but something like that should work.

can you elaborate this @tarobins?
itemTrailingEdge <= is always null
i want to know how to detect whether its reach top or bottom in the list
thanks.

from flutter.widgets.

NaikSoftware avatar NaikSoftware commented on August 23, 2024 2

This works for detecting pixel offset 0.0:

_itemPositionsListener.itemPositions.addListener(() {
      final positions = _itemPositionsListener.itemPositions.value;
      final firstPosition = positions.first;
      final elevated = firstPosition.index != 0 || firstPosition.itemLeadingEdge != 0;
      if (_inputBoxElevated != elevated) {
        setState(() {
          _inputBoxElevated = elevated;
        });
      }
    });

from flutter.widgets.

quango2304 avatar quango2304 commented on August 23, 2024 1

you can expose the frontScrollController to use it as "_controller"

from flutter.widgets.

tarobins avatar tarobins commented on August 23, 2024

Can you explain a bit more of what you're looking for? Do you mean the pixel offset of the scroll?

from flutter.widgets.

Mdlich avatar Mdlich commented on August 23, 2024

you can expose the frontScrollController to use it as "_controller"

How would you expose it without modifying the package? I am trying to use this widget with a widget from another package that requires the default ScrollController as a parameter. It seems like the ScrollablePositionedList uses a ScrollController internally, I just don't understand why is it not exposed by default..?

from flutter.widgets.

Zazo032 avatar Zazo032 commented on August 23, 2024

@tarobins in README.md it says you can see which items are on screen, but that code doesn't work:

itemPositionsListener.positions.addListener((positions) => ...);

The correct code is

itemPositionsListener.positions.itemPositions.addListener(() => ...);

So, you can no longer see the index of the items on screen. Why was that modified/removed? We need to see which index is being shown on screen in order to trigger a listener

from flutter.widgets.

tarobins avatar tarobins commented on August 23, 2024

@Clear2 sorry, so an issue with exposing the scroll offset is that there's isn't a consistent scroll offset when you jump around the list. For example, if you jump to item 1000, we don't figure out the offset off 1000, we just create a new list with 1000 at offset 0 and display that list.

I could possibly expose an interface that give a relative offset as scrolls are occurring.

If you still need this, could you explain some more about your use case?

from flutter.widgets.

leavjenn avatar leavjenn commented on August 23, 2024

@tarobins Hi, I have one use case: for threaded comments lists (like Reddit, Hacker News, etc), I want to add a feature that can collapse comments between a selected one and its parent. After collapsing, it should keep the exact selected comment item's position for a better user experience. So it requires both the position index and offset. Thanks for this great widget.

from flutter.widgets.

tarobins avatar tarobins commented on August 23, 2024

@GanZhiXiong

"I also need this feature because I want to be able to click a button to go back to the top of the list."

That should already be doable.

itemScrollController.jumpTo(index: 0);

from flutter.widgets.

marcglasberg avatar marcglasberg commented on August 23, 2024

@tarobins This issue is impossible to solve. As you said, the scroll offset isn't a consistent scroll offset when you jump around the list.

However, yes, it would be useful to expose an interface that gives a relative offset as scrolls are occurring. This is useful, for example, to show some effects like scrollbars, shadows, or collapsing appbars. In fact, I just implemented something like this so that I can show these kinds of effects. What I did is this:

Create a ScrollPosition where:

minScrollExtent is 0

maxScrollExtent is 1000 * itemCount;

pixels:

  • Make it the correct pixels value when the first item is in the cache. Because in this situation you can calculate the pixels precisely.
  • Make it 1000 * itemCount - pixelsToEnd when the last item is in the cache. Because in this sittuation you can calculate the pixelsToEnd precisely. And calculated like this it will be compatible with maxScrollExtent.
  • For all the other situations, make it 1000 * minItem where minItem is the minimum index in the screen at the moment. This will give some good approximation to our current position in relation to maxScrollExtent.

The docs should state that these positions are approximate, and meant only to help with scrollbars, shadows, and collapsing appbars.

from flutter.widgets.

AxesandGrinds avatar AxesandGrinds commented on August 23, 2024

How do we know when user has scrolled all the way to the top or bottom of screen in order to trigger a function that will load more items hence pagination?

Using itemPositionsListener.itemPositions.value does not seem to provide any information on when index is at zero or end of list.

from flutter.widgets.

AxesandGrinds avatar AxesandGrinds commented on August 23, 2024

I think I figured it out using

ValueListenableBuilder<Iterable>(
valueListenable: itemPositionsListener.itemPositions,...

the max and min values can be compared to the index of the list displayed

from flutter.widgets.

davidmartos96 avatar davidmartos96 commented on August 23, 2024

@tarobins This issue is impossible to solve. As you said, the scroll offset isn't a consistent scroll offset when you jump around the list.

However, yes, it would be useful to expose an interface that gives a relative offset as scrolls are occurring. This is useful, for example, to show some effects like scrollbars, shadows, or collapsing appbars. In fact, I just implemented something like this so that I can show these kinds of effects. What I did is this:

Create a ScrollPosition where:

minScrollExtent is 0

maxScrollExtent is 1000 * itemCount;

pixels:

  • Make it the correct pixels value when the first item is in the cache. Because in this situation you can calculate the pixels precisely.
  • Make it 1000 * itemCount - pixelsToEnd when the last item is in the cache. Because in this sittuation you can calculate the pixelsToEnd precisely. And calculated like this it will be compatible with maxScrollExtent.
  • For all the other situations, make it 1000 * minItem where minItem is the minimum index in the screen at the moment. This will give some good approximation to our current position in relation to maxScrollExtent.

The docs should state that these positions are approximate, and meant only to help with scrollbars, shadows, and collapsing appbars.

Do you have a small example with what you did? (Repo / Gist)
Where are you doing the ScrollPosition creation and those calculations?

from flutter.widgets.

jb522185660 avatar jb522185660 commented on August 23, 2024

how do you observe the offset now?Any solution????

from flutter.widgets.

Yogesh-Dubey-Ayesavi avatar Yogesh-Dubey-Ayesavi commented on August 23, 2024

https://pub.dev/packages/scrollable_positioned_list_extended see here

from flutter.widgets.

tarobins avatar tarobins commented on August 23, 2024

Added a method for observing scroll offset changes in #472 and #473. Please let me know how this works and if more is needed.

from flutter.widgets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.