PositionedScrollObserver는 다양한 스크롤 가능한 위젯에서 스크롤 위치를 우아하게 관찰하고 관리하도록 설계된 플러터 패키지입니다. 기존 위젯 상태를 방해하지 않고 스크롤 뷰 내 특정 인덱스로 부드럽게 이동하거나 애니메이션을 수행할 수 있습니다. 리스트, 그리드 및 사용자 정의 스크롤 가능 요소에 적합하며, 탐색 중에도 UI 일관성을 유지합니다. 이 패키지는 모든 주요 플랫폼을 지원하며 플러터의 스크롤 시스템과 원활하게 통합됩니다.
사용 사례
프로그래밍적으로 특정 목록 항목으로 스크롤하기
동적 콘텐츠 업데이트 중 스크롤 상태 유지
긴 목록에서 인덱스 기반 탐색 만들기
스크롤 뷰에서 부드러운 애니메이션 점프 구현
여러 스크롤 가능한 컴포넌트 동기화
주요 기능
모든 표준 스크롤 뷰를 지원합니다
스크롤 점프 중에 위젯 상태를 유지합니다
부드러운 애니메이션 인덱스 전환
크로스 플랫폼 호환성
최소한의 보일러플레이트 통합
사용자 정의 스크롤 컨트롤러와 함께 작동
적합한 대상
복잡한 스크롤 가능한 인터페이스를 구축하는 개발자
인덱스 기반 탐색이 필요한 앱
동적 또는 자주 업데이트되는 목록을 가진 프로젝트
원활한 사용자 경험을 우선시하는 팀
다중 플랫폼을 대상으로 하는 Flutter 앱
고려 사항
호환되는 스크롤 뷰 설정이 필요합니다
중첩 스크롤 뷰에 대한 추가 로직이 필요할 수 있습니다
안정적인 스크롤 컨트롤러 참조와 함께 사용하는 것이 가장 좋습니다
스크롤할 수 없는 위젯용이 아님
성능에 미치는 영향은 스크롤 뷰의 복잡성에 따라 달라집니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Features
Using jumpToIndex and animateToIndex to scroll to the specific index
Jumping/animating to the position by specifying a ratio in a scroll view. See how to align the render object. That would be very useful if you want to quickly jump to the top, middle or end of a list/grid.
Using PositionRetainedScrollPhysics to retain the old offset to avoid scrolling when adding new items into the top of ListView. See retain old scroll offset.
Check if the specific index is visible on the screen. See check visibility.
Check which items are visible in the viewport. See get visible items
No breaking for your current sliver widgets, e.g., ListView/GridView, SliverList/SliverGrid/SliverAppBar, just wrapping your item widgets using ObserverProxy. Supported:
For ListView.custom and GridView.custom, you could also use PositionedChildListDelegate and PositionedChildBuilderDelegate for wrapping items in ObserverProxy conveniently
Usage
For observing slivers:
observing a sliver with single child, using ScrollObserver.sliverSingle to create.
observing a sliver with multi children, using ScrollObserver.sliverMulti to create.
For observing other scroll views that have no sliver descendants.
observing a box with single child, using ScrollObserver.boxSingle to create. (rare cases and need more testing)
observing a box with multi children, using ScrollObserver.boxMulti to create.
By setting different alignment, you could jump/animate to the position according to the ratio: alignment.
for alignment = 0.0, it would align the render object' leading to the leading of the viewport's main axis extent.
for alignment = 0.5, it would align the render object's center to the center of the viewport;s main axis extent.
for alignment = 1.0, it would align the render object's trailing to the trailing of the viewport's main axis extent.
you could also specify alignment as the number between [0, 1]
Pay attention
The item widget/builder must be wrapped using ObserverProxy
All observers would normalizeIndex to ensure the index is in a valid range determined by itemCount of observers, so developers should also update observers' itemCount when the scroll views' item count changes.
Items that have the same RenderObject observed by an observer should share the same observer instance, instead of creating different observers for each item.
When using ScrollObserver.boxMulti, axis is required so that the observer could estimate the scroll offset along the correct main axis.