简体   繁体   English

使 ListView 滚动到一个偏移量

[英]Make ListView to scroll to an offset

I am trying to show the top element on the screen in listView to show completely or to scroll up.我试图在 listView 的屏幕上显示顶部元素以完全显示或向上滚动。

child: NotificationListener<ScrollEndNotification>(
          onNotification: (_) {
            double h = 120;
            double remaining = controller.position.pixels % h;
            int offset = 0;
            offset = (remaining > 60)
                ? ((controller.position.pixels ~/ h) + 1) * 120
                : ((controller.position.pixels ~/ h)) * 120;
            if (offset > 0) 
                controller.animateTo(offset.toDouble(),
                           duration: Duration(milliseconds: 500), curve: Curves.easeInOut);
            return true;
          },
          child: ScrollConfiguration(
            behavior: NoEffectScrollBehaviour(),
            child: ListView.builder(
                padding: EdgeInsets.zero,
                controller: controller,
                physics: BouncingScrollPhysics(),
                itemBuilder: (c, i){.......}

But the controller is not scrolling to the specified position. How can I make it the listView Elenent to either show or animate to offset?但是 controller 没有滚动到指定的 position。如何使 listView Elenent 显示或动画偏移?

Edit:编辑:

I am making the transition on scroll end event.我正在滚动结束事件上进行转换。 The problem is I am not able to do it in NotificationListner.问题是我无法在 NotificationListner 中执行此操作。

First of all you don't need NotificationListener.首先你不需要 NotificationListener。 You can simply below method.您可以简单地使用以下方法。

if you want scroll first element or scroll to top you can use如果你想滚动第一个元素或滚动到顶部你可以使用

controller.animateTo(0,duration: Duration(milliseconds: 500), curve: Curves.easeInOut);

if you want to go specific item you can use如果你想要 go 特定项目,你可以使用

final listItemHeight = 50;
final itemIndex = 3;
controller.animateTo(listItemHeight * itemIndex,duration: Duration(milliseconds: 500), curve: Curves.easeInOut);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM