임의의 스크롤 방향을 지원하는 무한 리스트와 스티키 헤더를 가능하게 하는 플러터 패키지입니다. 스크롤 중에 헤더는 상단에 고정되며 정확한 위치에 배치할 수 있습니다. 구성 옵션 또는 메서드 오버라이드를 통해 사용자 맞춤 설정이 가능하여 다양한 UI 요구 사항에 유연하게 대응할 수 있습니다. 채팅 인터페이스, 타임라인, 분류된 데이터 표시 등 그룹화된 긴 형식의 콘텐츠에 적합합니다.
사용 사례
날짜가 표시된 메시지와 함께하는 채팅 인터페이스
섹션 헤더가 있는 타임라인 피드
분류된 제품 목록
긴 형식 콘텐츠와 동적 그룹화
스티키 제목이 있는 스크롤 가능한 데이터 테이블
주요 기능
모든 스크롤 방향의 스티키 헤더
정확한 헤더 위치
무한 스크롤 지원
구성 또는 오버라이드를 통해 사용자 정의 가능
크로스 플랫폼 호환성
적합한 대상
동적 그룹화 목록을 구축하는 개발자
스티키 섹션 헤더가 필요한 앱
유연한 무한 목록 동작이 필요한 프로젝트
복잡한 스크롤 레이아웃을 가진 UI
크로스 플랫폼 일관성을 우선시하는 팀
고려 사항
헤더 로직 처리에 주의가 필요합니다
대규모 데이터 세트에서 성능에 영향을 미칠 수 있습니다
사용자 정의는 내부 메커니즘에 대한 이해가 필요합니다
간단한 정적 목록에는 적합하지 않음
모든 대상 플랫폼에서 테스트가 필요합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Sticky Infinite List
Infinite list with sticky headers.
This package was made in order to make possible
render infinite list in both directions with sticky headers, unlike most
packages in Dart Pub.
Supports various header positioning. Also supports Vertical and
Horizontal scroll list
It highly customizable and doesn't have any third party dependencies or native(Android/iOS) code.
In addition to default usage, this package exposes some classes, that
can be overridden if needed. Also some classes it can be used inside
Scrollable widgets independently from InfiniteList container.
This package uses CustomScrollView to perform scroll with all
benefits for performance that Flutter provides.
Features
sticky headers within infinite list
multi directional infinite list
customization for sticky header position
horizontal sticky list support
dynamic header build on content scroll
dynamic min offset calculation on content scroll
Flutter versions support
If you're using Flutter version lower than 1.20 consider using v2.x.x.
For Flutter version lower than 3.41 consider using v3-v4.
When min offset callback invoked or header builder is invoked
object StickyState is passed as parameter
This object describes current state for sticky header.
classStickyState<I> {
/// Position, that header already passed////// Value can be between 0.0 and 1.0////// If it's `0.0` - sticky in max start position////// `1.0` - max end position////// If [InfiniteListItem.initialHeaderBuild] is true, initial/// header render will be with position = 0finaldouble position;
/// Number of pixels, that outside of viewport////// If [InfiniteListItem.initialHeaderBuild] is true, initial/// header render will be with offset = 0/// /// For header bottom positions (or right positions for horizontal)/// offset value also will be amount of pixels that was scrolled awayfinaldouble offset;
/// Item indexfinal I index;
/// If header is in sticky state////// If [InfiniteListItem.minOffsetProvider] is defined,/// it could be that header builder will be emitted with new state/// on scroll, but [sticky] will be false, if offset already passed/// min offset value////// WHen [InfiniteListItem.minOffsetProvider] is called, [sticky]/// will always be `false`. Since for min offset calculation/// offset itself not defined yetfinalbool sticky;
/// Scroll item height.////// If [InfiniteListItem.initialHeaderBuild] is true, initial/// header render will be called without this valuefinaldouble contentSize;
}
Extended configuration
Available configuration
Alongside with minimal config to start using.
InfiniteList allows you to define config for scroll list rendering
InfiniteList(
/// Optional parameter to pass ScrollController instance
controller: ScrollController(),
/// Optional parameter/// to specify scroll direction/// /// By default scroll will be rendered with just positive/// direction `InfiniteListDirection.forward`/// /// If you need infinite list in both directions use `InfiniteListDirection.multi`
direction: InfiniteListDirection.multi,
/// Negative max child count./// /// Will be used only when `direction: InfiniteListDirection.multi`/// /// If it's not provided, scroll will be infinite in negative direction
negChildCount: 100,
/// Positive max child count/// /// Specifies number of elements for forward list/// /// If it's not provided, scroll will be infinite in positive direction
posChildCount: 100,
/// ScrollView anchor value.
anchor: 0.0,
/// ScrollView physics value.
physics: null,
/// Item builder/// /// Should return `InfiniteListItem`
builder: (BuildContext context, int index) {
returnInfiniteListItem(
//...
)
}
)
InfiniteListItem allows you to specify more options for you customization.
InfiniteListItem(
/// See class description for more info/// /// Forces initial header render when [headerStateBuilder]/// is specified.
initialHeaderBuild: false,
/// Simple Header builder/// that will be called once during List item render
headerBuilder: (BuildContext context) {},
/// Header builder, that will be invoked each time/// when header should change it's position/// /// Unlike prev method, it also provides `state` of header/// position/// /// This callback has higher priority than [headerBuilder],/// so if both header builders will be provided,/// [headerBuilder] will be ignored
headerStateBuilder: (BuildContext context, StickyState<int> state) {},
/// Content builder
contentBuilder: (BuildContext context) {},
/// Min offset invoker/// /// This callback is called on each header position change,/// to define when header should be stick to the bottom of/// content./// /// If this method returns `0`,/// header will be in sticky state until list item/// will be visible inside view port/// /// If this method not provided or it returns null, header/// will be sticky until offset equals to /// header size
minOffsetProvider: (StickyState<int> state) {},
/// Header alignment against main axis direction////// See [HeaderMainAxisAlignment] for more infoHeaderMainAxisAlignment mainAxisAlignment: HeaderMainAxisAlignment.start,
/// Header alignment against cross axis direction////// See [HeaderCrossAxisAlignment] for more infoHeaderCrossAxisAlignment crossAxisAlignment: HeaderCrossAxisAlignment.start,
/// Header position against scroll axis for relative positioned headers////// Only for relative header positioningHeaderPositionAxis positionAxis: HeaderPositionAxis.mainAxis,
/// List item padding, see [EdgeInsets] for more infoEdgeInsets padding: constEdgeInsets.all(8.0),
/// Scroll direction////// Can be vertical or horizontal (see [Axis] class)////// This value also affects how bottom or top/// edge header positioned headers behave
scrollDirection: Axis.vertical,
);
Demos
Header alignment demo
Relative positioning
Relative cross axis positioning
Overlay positioning
Horizontal scroll demo
Reverse infinite scroll
Currently package doesn't support CustomScrollView.reverse option.
But same result can be achieved with defining anchor = 1 and
posChildCount = 0. In that way viewport center will be stick to the
bottom and positive list won't render anything.
Additionally you can specify header alignment to any side.
In most cases it will be enough to just use InfiniteListItem
But in some cases you may need to add additional functionality to
each item.
Luckily you can extend and override base InfiniteListItem class
/// Generic `I` is index type, by default list item uses `int`classSomeCustomListItem<I> extendsInfiniteListItem<I> {
/// Header alignment against main axis direction////// See [HeaderMainAxisAlignment] for more info@overridefinalHeaderMainAxisAlignment mainAxisAlignment = HeaderMainAxisAlignment.start;
/// Header alignment against cross axis direction////// See [HeaderCrossAxisAlignment] for more info@overridefinalHeaderCrossAxisAlignment crossAxisAlignment = HeaderCrossAxisAlignment.start;
/// Header position against scroll axis for relative positioned headers////// See [HeaderPositionAxis] for more info@overridefinalHeaderPositionAxis positionAxis = HeaderPositionAxis.mainAxis;
/// If header should overlay content or not@overridefinalbool overlayContent = false;
/// Let item builder know if it should watch/// header position changes/// /// If this value is `true` - it will invoke [buildHeader]/// each time header position changes@overrideboolget watchStickyState => true;
/// Let item builder know that this class/// provides header/// /// If it returns `false` - [buildHeader] will be ignored /// and never called@overrideboolget hasStickyHeader => true;
/// This methods builds header/// /// If [watchStickyState] is `true`,/// it will be invoked on each header position change/// and `state` option will be provided/// /// Otherwise it will be called only once on initial render/// and each header position change won't invoke this method./// /// Also in that case `state` will be `null`@overrideWidgetbuildHeader(BuildContext context, [StickyState<I> state]) {}
/// Content item builder/// /// This method invoked only once@overrideWidgetbuildContent(BuildContext context) {}
/// Called during init state (see [Statefull] widget [State.initState])/// /// For additional information about [Statefull] widget `initState`/// lifecycle - see Flutter docs@protected@mustCallSupervoidinitState() {}
/// Called during item dispose (see [Statefull] widget [State.dispose])/// /// For additional information about [Statefull] widget `dispose`/// lifecycle - see Flutter docs@protected@mustCallSupervoiddispose() {}
}
Need more override?..
Alongside with list item override, to use inside InfiniteList builder,
you can also use or extend StickyListItem, that exposed by this
package too, independently.
This class uses Stream to inform it's parent about header position changes
Also it requires to be rendered inside Scrollable widget and Viewport,
since it subscribes to scroll event and calculates position
against Viewport coordinates (see StickyListItemRenderObject class
for more information)
This code will render single child scroll with 4 widgets. Two middle
items - items with sticky header.
Demo
For more complex example please take a look at "Single Example" page
in Example project
Changelog
Please see the Changelog page to know what's recently changed.
Bugs/Requests
If you encounter any problems feel free to open an issue.
If you feel the library is missing a feature,
please raise a ticket on Github and I'll look into it.
Pull request are also welcome.
Known issues
Currently this package can't work with reverse scroll. For some reason
flutter calculates coordinate for negative list items in a
different way in reverse mode, comparing to regular scroll direction.