Search packages, repositories and app categories

scroll_to_id cover
Package detailsv1.5.1

scroll_to_id

scroll_to_id is a Flutter library that enables screen to auto-scroll by selecting id defined for each widget in scrollview.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
177
Likes
61
Pub points
150

Install package

flutter pub add scroll_to_id

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

scroll_to_id is a Flutter package that allows automatic scrolling to a specific widget within a scrollable view by referencing its unique ID. It simplifies navigation in long lists or scrollable layouts by enabling developers to programmatically scroll to elements based on their assigned IDs. Ideal for dynamic content, anchor links, or form validation with scroll-to-error features. The package supports all major platforms and integrates seamlessly into existing scroll views.

Use cases

  • Navigating to specific list items
  • Scrolling to form fields with errors
  • Implementing anchor links in long pages
  • Creating smooth section transitions
  • Auto-scrolling after navigation
  • Enhancing accessibility with keyboard shortcuts

Key features

  • Scroll to widget by ID
  • Supports all Flutter platforms
  • Minimal setup required
  • Works with any scrollable widget
  • No need for global keys
  • Efficient performance with large lists

Suitable for

  • Developers building long-form content apps
  • Teams using dynamic lists
  • Apps requiring anchor navigation
  • Accessibility-focused applications
  • Form-heavy interfaces

Considerations

  • IDs must be unique within the scroll view
  • Performance may vary with very large lists
  • Requires proper widget structure
  • Not suitable for non-scrollable containers
  • Limited to direct children of scrollable widgets

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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],
                ),
              );
            }),
          ),
        ),
      ],
    );
  }
}

Categories

Source repository

Projects using this package

No data available yet.