颤动 ListView 滚动到索引不可用

Dee*_*lot 31 dart flutter

我需要什么:

我想按某个索引滚动列表,我该怎么做。

我知道的:

scrollToIndex 应该从 n 索引开始,但是我们如何滚动到任何索引?

TWL*_*TWL 32

不幸的是,ListView 没有针对 scrollToIndex() 函数的内置方法。您必须开发自己的方法来测量animateTo()or 的该元素的偏移量jumpTo(),或者您可以搜索这些建议的解决方案/插件或其他帖子,例如Flutter:滚动到 ListView 中的小部件

(自 2017 年以来,在flutter/issues/12319 上讨论了一般的 scrollToIndex 问题,但目前还没有计划)


但是有一种不同类型的 ListView 确实支持 scrollToIndex (如 Slashbin 所述):

您可以像 ListView 一样设置它并且工作方式相同,但您现在可以访问ItemScrollController,它执行以下操作:

  • jumpTo({index, alignment})
  • scrollTo({index, alignment, duration, curve})

简化示例:

ItemScrollController _scrollController = ItemScrollController();

ScrollablePositionedList.builder(
  itemScrollController: _scrollController,
  itemCount: _myList.length,
  itemBuilder: (context, index) {
    return _myList[index];
  },
)

_scrollController.scrollTo(index: 150, duration: Duration(seconds: 1));
Run Code Online (Sandbox Code Playgroud)

(请注意,这个库是由 Google 开发的,而不是由 Flutter 核心团队开发的。)


Sla*_*bin 11

ScrollablePositionedList可以用于此。 https://github.com/google/flutter.widgets/tree/master/packages/scrollable_positioned_list

发布链接 - https://pub.dev/packages/scrollable_positioned_list

final ItemScrollController itemScrollController = ItemScrollController();
final ItemPositionsListener itemPositionListener = ItemPositionsListener.create();
Run Code Online (Sandbox Code Playgroud)

ScrollablePositionedList.builder(
  itemCount: 500,
  itemBuilder: (context, index) => Text('Item $index'),
  itemScrollController: itemScrollController,
  itemPositionsListener: itemPositionListener,
);
Run Code Online (Sandbox Code Playgroud)

然后可以滚动到特定项目:

itemScrollController.scrollTo(
  index: 150,
  duration: Duration(seconds: 2),
  curve: Curves.easeInOutCubic);
Run Code Online (Sandbox Code Playgroud)


jit*_*555 6

使用scroll_to_indexlib,这里滚动将始终执行到第六个位置,如下硬编码

dependencies:
    scroll_to_index: ^1.0.6
Run Code Online (Sandbox Code Playgroud)

代码片段:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final scrollDirection = Axis.vertical;

  AutoScrollController controller;
  List<List<int>> randomList;

  @override
  void initState() {
    super.initState();
    controller = AutoScrollController(
        viewportBoundaryGetter: () =>
            Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
        axis: scrollDirection);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        scrollDirection: scrollDirection,
        controller: controller,
        children: <Widget>[
          ...List.generate(20, (index) {
            return AutoScrollTag(
              key: ValueKey(index),
              controller: controller,
              index: index,
              child: Container(
                height: 100,
                color: Colors.red,
                margin: EdgeInsets.all(10),
                child: Center(child: Text('index: $index')),
              ),
              highlightColor: Colors.black.withOpacity(0.1),
            );
          }),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _scrollToIndex,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
  // Scroll listview to the sixth item of list, scrollling is dependent on this number
  Future _scrollToIndex() async {
    await controller.scrollToIndex(6, preferPosition: AutoScrollPosition.begin);
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述