robert-luoqing / flutter_list_view

MIT License
45 stars 17 forks source link

How to position initIndex item in center of the screen? #36

Open r07work opened 3 months ago

r07work commented 3 months ago

I am developing a feature that allows users to search for items and click on a search result. When the results are displayed on the screen, I use initIndex to position the selected item within the view.

@override
  Widget build(BuildContext context) {
    return FlutterListView(
      key: listKey,
      controller:listViewController,
      reverse: true,
      delegate: FlutterListViewDelegate(
        _listItem,
        childCount: items.length,
        onItemKey: (index) {
          return items.itemAt(index).id;
        },
        initIndex: initialScrollIndex,
        keepPosition: true,
      ),
    );
  }

Currently the initIndex is being displayed at bottom of screen. How to position initIndex item at center?

Thank you!

robert-luoqing commented 3 months ago

The widget has initOffset and initOffsetBasedOnBottom two properties. I have not implemented showing the item in the center of the list view. But you can adjust initOffset to implement it.

The item will show in top if reverse = true and initOffsetBasedOnBottom=true. You can adjust initOffset to a distance from the top.

If you search in flutter list view, you can invoke controller.sliverController.jumpToIndex

void jumpToIndex(int index,
      {double offset = 0, bool offsetBasedOnBottom = false}
r07work commented 3 months ago

Thank you @robert-luoqing, Let me try this.