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

scroll_to_id 封面
插件详情v1.5.1

scroll_to_id

scroll_to_id 是一个 Flutter 库,通过选择 ScrollView 中每个 widget 定义的 id,实现屏幕自动滚动。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
177
喜欢数
61
Pub 分数
150

安装插件

flutter pub add scroll_to_id

插件介绍

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

scroll_to_id 是一个 Flutter 包,允许通过引用其唯一 ID 自动滚动到可滚动视图中的特定小部件。它通过根据分配的 ID 程序化滚动到元素,简化了长列表或可滚动布局中的导航。非常适合动态内容、锚点链接或带有滚动到错误功能的表单验证。该包支持所有主要平台,并可无缝集成到现有的滚动视图中。

适合场景

  • 导航到特定列表项
  • 滚动到有错误的表单字段
  • 在长页面中实现锚点链接
  • 创建平滑的章节过渡
  • 导航后自动滚动
  • 通过键盘快捷键增强可访问性

主要特点

  • 通过 ID 滚动到控件
  • 支持所有 Flutter 平台
  • 所需设置最少
  • 与任何可滚动的 widget 兼容
  • 无需全局键
  • 大型列表中的高效性能

适合对象

  • 开发长篇内容应用的开发者
  • 使用动态列表的团队
  • 需要锚点导航的应用
  • 注重可访问性的应用程序
  • 表单密集型界面

使用建议

  • ID 必须在滚动视图内唯一
  • 性能可能因列表过大而有所不同
  • 需要正确的控件结构
  • 不适用于不可滚动的容器
  • 仅限可滚动控件的直接子控件

原始 README

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

Scroll to id

scroll_to_id is a Flutter library for Scroll to id in ScrollView

Image Image

Installing

You should add the following to your pubspec.yaml file:

dependencies:
  scroll_to_id: ^1.5.1

Getting Started

To start, import the dependency in your code:

import 'package:scroll_to_id/scroll_to_id.dart';

Next, to define ScrollController:

final scrollController = ScrollController();

Next, to create instance with ScrollController:

ScrollToId scrollToId = ScrollToId(scrollController: scrollController);

Next, to set InteractiveScrollViewer and ScrollContent:

InteractiveScrollViewer(
  scrollToId: scrollToId,
  children: <ScrollContent>[
    ScrollContent(
      id: 'a',
      child: Container(
        color: Colors.red,
        width: 300,
        height: 200,
      )
    ),
    ScrollContent(
      id: 'b',
      child: Container(
        color: Colors.green,
        width: 300,
        height: 200,
      )
    ),
  ],
)

id property is destination of scroll. Do not use the same id.

Next, to scroll to id:

/// animate
scrollToId.animateTo(
  'b',
  duration: Duration(milliseconds: 500),
  curve: Curves.ease
);

/// jump
scrollToId.jumpTo('b');

Examples

import 'package:flutter/material.dart';
import 'package:scroll_to_id/scroll_to_id.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ScrollToId scrollToId;
  final ScrollController scrollController = ScrollController();

  List<Color> _colorList = [
    Colors.green,
    Colors.red,
    Colors.yellow,
    Colors.blue
  ];

  void _scrollListener() {
    print(scrollToId.idPosition());
  }

  @override
  void initState() {
    super.initState();

    /// Create ScrollToId instance
    scrollToId = ScrollToId(scrollController: scrollController);

    scrollController.addListener(_scrollListener);
  }

  /// Generate 10 Container
  /// Case [Axis.horizontal] set buildStackHorizontal() to body
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Scroll to id',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Scroll to id'),
        ),
        body: buildStackVertical(),
      ),
    );
  }

  /// [Axis.vertical]
  /// https://raw.githubusercontent.com/wiki/yusukeinouehatchout/scroll_to_id/gif/scroll_to_id_vertical.gif
  Widget buildStackVertical() {
    return Stack(
      alignment: Alignment.topRight,
      children: [
        InteractiveScrollViewer(
          scrollToId: scrollToId,
          children: List.generate(10, (index) {
            return ScrollContent(
              id: '$index',
              child: Container(
                alignment: Alignment.center,
                width: double.infinity,
                height: 200,
                child: Text(
                  '$index',
                  style: TextStyle(color: Colors.white, fontSize: 50),
                ),
                color: _colorList[index % _colorList.length],
              ),
            );
          }),
        ),
        Container(
          decoration: BoxDecoration(
            border: Border.all(color: Colors.white, width: 3),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: List.generate(10, (index) {
              return GestureDetector(
                child: Container(
                  width: 100,
                  alignment: Alignment.center,
                  height: 50,
                  child: Text(
                    '$index',
                    style: TextStyle(color: Colors.white),
                  ),
                  color: _colorList[index % _colorList.length],
                ),
                onTap: () {
                  /// scroll with animation
                  scrollToId.animateTo('$index',
                      duration: Duration(milliseconds: 500),
                      curve: Curves.ease);

                  /// scroll with jump
                  // scrollToId.jumpTo('$index');
                },
              );
            }),
          ),
        )
      ],
    );
  }

  /// [Axis.horizontal]
  /// https://raw.githubusercontent.com/wiki/yusukeinouehatchout/scroll_to_id/gif/scroll_to_id_horizontal.gif
  Widget buildStackHorizontal() {
    return Column(
      children: [
        Container(
          width: double.infinity,
          decoration: BoxDecoration(
            border: Border.all(color: Colors.white, width: 3),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: List.generate(10, (index) {
              return Expanded(
                child: GestureDetector(
                  child: Container(
                    alignment: Alignment.center,
                    height: 80,
                    child: Text(
                      '$index',
                      style: TextStyle(color: Colors.white),
                    ),
                    color: _colorList[index % _colorList.length],
                  ),
                  onTap: () {
                    /// scroll with animation
                    scrollToId.animateTo('$index',
                        duration: Duration(milliseconds: 500),
                        curve: Curves.ease);

                    /// scroll with jump
                    // scrollToId.jumpTo('$index');
                  },
                ),
              );
            }),
          ),
        ),
        Expanded(
          child: InteractiveScrollViewer(
            scrollToId: scrollToId,
            scrollDirection: Axis.horizontal,
            children: List.generate(10, (index) {
              return ScrollContent(
                id: '$index',
                child: Container(
                  alignment: Alignment.center,
                  width: 200,
                  child: Text(
                    '$index',
                    style: TextStyle(color: Colors.white, fontSize: 50),
                  ),
                  color: _colorList[index % _colorList.length],
                ),
              );
            }),
          ),
        ),
      ],
    );
  }
}

所属分类

源码仓库

使用此插件的项目

暂无相关数据。