パッケージ、リポジトリ、アプリカテゴリを検索

scroll_to_id のカバー
パッケージ詳細v1.5.1

scroll_to_id

scroll_to_id は、ScrollView 内の各ウィジェットに定義された ID を選択することで、画面を自動スクロールすることができる Flutter ライブラリです。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
177
いいね数
61
Pub ポイント
150

パッケージをインストール

flutter pub add scroll_to_id

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

scroll_to_id は、スクロール可能なビュー内の特定のウィジェットにその一意のIDを参照して自動スクロールする機能を提供するFlutterパッケージです。長リストやスクロール可能なレイアウトでのナビゲーションを簡素化し、開発者が割り当てられたIDに基づいてプログラムで要素までスクロールできるようにします。動的なコンテンツやアンカーリンク、フォーム検証におけるエラー位置へのスクロール機能に最適です。このパッケージはすべての主要プラットフォームをサポートしており、既存のスクロールビューにスムーズに統合できます。

用途

  • 特定のリスト項目に移動する
  • エラーがあるフォームフィールドまでスクロールする
  • 長いページでのアンカーリンクの実装
  • スムーズなセクション遷移の作成
  • ナビゲーション後の自動スクロール
  • キーボードショートカットによるアクセシビリティの向上

主な機能

  • ID でウィジェットにスクロール
  • すべての Flutter プラットフォームをサポートしています
  • 最小限のセットアップが必要
  • 任意のスクロール可能なウィジェットと連携可能
  • グローバルキーは不要です
  • 大規模なリストにおける効率的なパフォーマンス

適している対象

  • 長編コンテンツアプリを開発する開発者
  • 動的リストを使用するチーム
  • アンカー移動を必要とするアプリ
  • アクセシビリティを重視したアプリケーション
  • フォーム中心のインターフェース

注意事項

  • スクロールビュー内では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],
                ),
              );
            }),
          ),
        ),
      ],
    );
  }
}

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。