Skip to content

ScrollablePositionedList: When and why is itemPositionsListener.itemPositions empty? #88

Open
@Alfie-AD

Description

@Alfie-AD
final ItemScrollController itemScrollController = ItemScrollController();
final ItemPositionsListener itemPositionsListener = ItemPositionsListener.create();

...

ScrollablePositionedList.builder(
   itemScrollController: itemScrollController,
   itemPositionsListener: itemPositionsListener,
   reverse: true,
   itemCount: items.length,
   itemBuilder: (context, i) => ListTile(...),
),

...

ValueListenableBuilder<Iterable<ItemPosition>>(
                    valueListenable: itemPositionsListener.itemPositions,
                    builder: (context, positions, child) {
                      int min;
                      int max;
                      if (positions.isNotEmpty) {
                        min = positions
                            .where((ItemPosition position) =>
                                position.itemTrailingEdge > 0)
                            .reduce((ItemPosition min, ItemPosition position) =>
                                position.itemTrailingEdge < min.itemTrailingEdge
                                    ? position
                                    : min)
                            .index;
                        max = positions
                            .where((ItemPosition position) =>
                                position.itemLeadingEdge < 1)
                            .reduce((ItemPosition max, ItemPosition position) =>
                                position.itemLeadingEdge > max.itemLeadingEdge
                                    ? position
                                    : max)
                            .index;
                      }
                      print( min); //prints 'null'
                      print(max); //prints 'null'
                      print(positions); //prints '[]'

                      return AnimatedSwitcher(
                        duration: _switcher400,
                        reverseDuration: _switcher400,
                        child: min ?? 0 < 5
                            ? Container(height: 0, width: 0)
                            : RawMaterialButton(
                                padding: EdgeInsets.all(3),
                                constraints: BoxConstraints(minWidth: 50),
                                fillColor: Colors.white,
                                elevation: 10,
                                shape: CircleBorder(),
                                onPressed: () {
                                  if (min ?? 0 < 15)
                                    itemScrollController.scrollTo(
                                        index: 0, duration: _switcher400);
                                  else
                                    itemScrollController.jumpTo(index: 0);
                                },
                                child: Icon(
                                  Icons.arrow_drop_down,
                                  size: 40,
                                ),
                              ),
                      );
                    },
                  ),

Hi. This code was working fine a moment ago and then there is something wrong with it.
What am trying to acheive is animate in a button whenever the list is scrolled away from index 0 and the button will jump back to index 0. Simple.
The issue is itemPositionsListener.itemPositions is not returning the positions so i have no way to find out where i am in the list.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions