搜索插件、代码仓与 APP 分类

插件详情v3.0.1

scroll_to_index

在 Flutter 中滚动到任何可滚动控件的特定子项

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
53.7万
喜欢数
821
Pub 分数
140

安装插件

flutter pub add scroll_to_index

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

scroll_to_index 包允许在任何可滚动小部件(如 ListView 或 GridView)中精确滚动到特定子项。它提供了一个简单的 API,用于以编程方式滚动到指定索引,非常适合在长列表或动态内容中进行导航。该包可在所有主要平台(包括 Android、iOS、Web、macOS、Windows 和 Linux)上使用。当您需要根据用户交互或状态变化跳转到特定项目时,它特别有用。实现轻量且与现有可滚动小部件无缝集成。

适合场景

  • 跳转到特定列表项
  • 在长滚动列表中导航
  • 动态UI中的程序化滚动
  • 改进可访问性在可滚动界面中的应用
  • 与外部事件同步滚动位置

主要特点

  • 通过索引号滚动到任意索引
  • 支持所有可滚动的控件
  • 跨平台兼容性
  • 轻量且易于集成
  • 适用于动态列表

适合对象

  • 开发列表密集型应用的开发者
  • 需要流畅导航的应用
  • 面向可访问性的界面
  • 频繁更新的动态内容

使用建议

  • 需要手动处理滚动控制器
  • 可能需要额外的逻辑来实现流畅的动画
  • 未经优化,不适合非常大的列表

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

scroll-to-index

This package provides the scroll to index mechanism for fixed/variable row height for Flutter scrollable widget.

Getting Started

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  scroll_to_index: any

In your library add the following import:

import 'package:scroll_to_index/scroll_to_index.dart';

For help getting started with Flutter, view the online documentation.

Usage

This is a widget level library, means you can use this mechanism inside any Flutter scrollable widget.

Example for Flutter ListView

ListView(
  scrollDirection: scrollDirection,
  controller: controller,
  children: randomList.map<Widget>((data) {
  	final index = data[0];
  	final height = data[1];
    return AutoScrollTag(
      key: ValueKey(index),
      controller: controller,
      index: index,
      child: Text('index: $index, height: $height'),
      highlightColor: Colors.black.withOpacity(0.1),
    );
  }).toList(),
)

you can wrap any of your row widget which has dynamic row height

AutoScrollTag(
  key: ValueKey(index),
  controller: controller,
  index: index,
  child: child
)

with the AutoScrollController controller.

when you need to trigger scroll to a specified index, you can call

controller.scrollToIndex(index, preferPosition: AutoScrollPosition.begin)

even more, with a fixed row height, you can give it a suggested height for more efficient scrolling. there are more configuration.

final controller = AutoScrollController(
  //add this for advanced viewport boundary. e.g. SafeArea
  viewportBoundaryGetter: () => Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),

  //choose vertical/horizontal
  axis: scrollDirection,

  //this given value will bring the scroll offset to the nearest position in fixed row height case.
  //for variable row height case, you can still set the average height, it will try to get to the relatively closer offset 
  //and then start searching.
  suggestedRowHeight: 200
);

for full example, please see this Demo.

Who Uses

  • Quire - a simple, collaborative, multi-level task management tool.

所属分类

源码仓库

使用此插件的项目