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
demo
getting started
install package and import
import 'package:sticky_infinite_list/sticky_infinite_list.dart';
package exposes infinitelist
, infinitelistitem
, stickylistitem
,
stickylistitemrenderobject
classes
examples
simple example
to start using infinite list with sticky headers,
you need to create instance infinitelist
with builder specified.
no need to specify any additional config to make it work
import 'package:sticky_infinite_list/sticky_infinite_list.dart';
class example extends statelesswidget {
@override
widget build(buildcontext context) {
return infinitelist(
builder: (buildcontext context, int index) {
/// builder requires [infinitelist] to be returned
return infinitelistitem(
/// header builder
headerbuilder: (buildcontext context) {
return container(
///...
);
},
/// content builder
contentbuilder: (buildcontext context) {
return container(
///...
);
},
);
}
);
}
}
state
when min offset callback invoked or header builder is invoked
object stickystate
is passed as parameter
this object describes current state for sticky header.
class stickystate<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 = 0
final double 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 away
final double offset;
/// item index
final 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 yet
final bool sticky;
/// scroll item height.
///
/// if [infinitelistitem.initialheaderbuild] is true, initial
/// header render will be called without this value
final double 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,
/// min child count.
///
/// will be used only when `direction: infinitelistdirection.multi`
///
/// accepts negative values only
///
/// if it's not provided, scroll will be infinite in negative direction
minchildcount: -100,
/// max child count
///
/// specifies number of elements for forward list
///
/// if it's not provided, scroll will be infinite in positive direction
maxchildcount: 100,
/// scrollview anchor value.
anchor: 0.0,
/// item builder
///
/// should return `infinitelistitem`
builder: (buildcontext context, int index) {
return infinitelistitem(
//...
)
}
)
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 not provided or it returns `0`,
/// header will be in sticky state until list item
/// will be visible inside view port
minoffsetprovider: (stickystate<int> state) {},
/// header alignment
///
/// use [headeralignment] to align header to left,
/// right, top or bottom side
///
/// optional. default value [headeralignment.topleft]
headeralignment: headeralignment.topleft,
/// 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
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
maxchildcount = 0
. in that way viewport center will be stick
to the bottom and positive list won’t render anything.
additionally you can specify headeralignment
to any side.
import 'package:sticky_infinite_list/sticky_infinite_list.dart';
class example extends statelesswidget {
@override
widget build(buildcontext context) {
return infinitelist(
anchor: 1.0,
direction: infinitelistdirection.multi,
maxchildcount: 0,
builder: (buildcontext context, int index) {
/// builder requires [infinitelist] to be returned
return infinitelistitem(
headeralignment: headeralignment.bottomleft,
/// header builder
headerbuilder: (buildcontext context) {
return container(
///...
);
},
/// content builder
contentbuilder: (buildcontext context) {
return container(
///...
);
},
);
}
);
}
}
demo
for more info take a look at
example project
available for override
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`
class somecustomlistitem extends infinitelistitem<i> {
/// header alignment
///
/// supports all sides alignment, see [headeralignment] for more info
///
/// by default [headeralignment.topleft]
final headeralignment headeralignment;
/// 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
@override
bool get watchstickystate => true;
/// let item builder know that this class
/// provides header
///
/// if it returns `false` - [buildheader] will be ignored
/// and never called
@override
bool get 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`
@override
widget buildheader(buildcontext context, [stickystate<i> state]) {}
/// content item builder
///
/// this method invoked only once
@override
widget buildcontent(buildcontext context) => {}
/// called during init state (see statefull widget [state.initstate])
///
/// for additional information about statefull widget `initstate`
/// lifecycle - see flutter docs
@protected
@mustcallsuper
void initstate() {}
/// called during item dispose (see statefull widget [state.dispose])
///
/// for additional information about statefull widget `dispose`
/// lifecycle - see flutter docs
@protected
@mustcallsuper
void dispose() {}
}
need more override?..
if you get any problems with this type of override,
please create an issue
alongside with list item override, to use inside infinitelist
builder,
you can also use 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)
for example
widget build(buildcontext context) {
return singlechildscrollview(
child: column(
children: <widget>[
container(
height: height,
color: colors.lightblueaccent,
child: placeholder(),
),
stickylistitem<string>(
header: container(
height: 30,
width: double.infinity,
color: colors.orange,
child: center(
child: text('sticky header')
),
),
content: container(
height: height,
color: colors.blueaccent,
child: placeholder(),
),
itemindex: 'single-child-index',
),
container(
height: height,
color: colors.cyan,
child: placeholder(),
),
],
),
);
}
this code will render single child scroll
with 3 widgets. middle one – item with sticky header.
demo
for more complex example please take a look at “single example” page
in example project
Comments are closed.