implicitly animated reorderable list
a flutter listview that implicitly animate calculates the changes between two lists using the myersdiff algorithm and animates between them for you. the implicitlyanimatedreorderablelist adds reordering support to its items with fully custom animations.
a flutter listview that implicitly animates between the changes of two lists with support to reorder its items.
click here to view the full example.
installing implicitly animated
add it to your pubspec.yaml
file:
dependencies:
implicitly_animated_reorderable_list: ^0.1.5
install packages from the command line
flutter packages get
usage
the package contains two listviews
: implicitlyanimatedlist
which is the base class and offers implicit animation for item insertions
, removals
as well as updates
, and implicitlyanimatedreorderablelist
which extends the implicitlyanimatedlist
and adds reordering support to its items. see examples below on how to use them.
implicitlyanimatedlist
implicitlyanimatedlist
is based on animatedlist
and uses the myersdiff
algorithm to calculate the difference between two lists and calls insertitem
and removeitem
on the animatedliststate
for you.
example
// specify the generic type of the data in the list.
implicitlyanimatedlist<mygenerictype>(
// the current items in the list.
items: items,
// called by the diffutil to decide whether two object represent the same item.
// for example, if your items have unique ids, this method should check their id equality.
areitemsthesame: (a, b) => a.id == b.id,
// called, as needed, to build list item widgets.
// list items are only built when they're scrolled into view.
itembuilder: (context, animation, item, index) {
// specifiy a transition to be used by the implicitlyanimatedlist.
// in this case a custom transition.
return sizefadetranstion(
sizefraction: 0.7,
curve: curves.easeinout,
animation: animation,
child: text(item.name),
);
},
// an optional builder when an item was removed from the list.
// if not specified, the list uses the itembuilder with
// the animation reversed.
removeditembuilder: (context, animation, olditem) {
return fadetransition(
opacity: animation,
child: text(olditem.name),
);
},
);
note as
animatedlist
doesn’t support item moves, a move is handled by removing the item from the old index and inserting it at the new index.
implicitlyanimatedreorderablelist
implicitlyanimatedreorderablelist
is based on implicitlyanimatedlist
and adds reordering support to the list.
example
implicitlyanimatedreorderablelist<mygenerictype>(
items: items,
areitemsthesame: (olditem, newitem) => olditem.id == newitem.id,
onreorderfinished: (item, from, to, newitems) {
// remember to update the underlying data when the list has been
// reordered.
setstate(() {
items
..clear()
..addall(newitems);
});
},
itembuilder: (context, itemanimation, item, index) {
// each item must be wrapped in a reorderable widget.
return reorderable(
// each item must have an unique key.
key: valuekey(item),
// the animation of the reorderable builder can be used to
// change to appearance of the item between dragged and normal
// state. for example to add elevation when the item is being dragged.
// this is not to be confused with the animation of the itembuilder.
// implicit animations (like animatedcontainer) are sadly not yet supported.
builder: (context, draganimation, indrag) {
final t = draganimation.value;
final elevation = lerpdouble(0, 8, t);
final color = color.lerp(colors.white, colors.white.withopacity(0.8), t);
return sizefadetranstion(
sizefraction: 0.7,
curve: curves.easeinout,
animation: itemanimation,
child: material(
color: color,
elevation: elevation,
type: materialtype.transparency,
child: listtile(
title: text(item.name),
// the child of a handle can initialize a drag/reorder.
// this could for example be an icon or the whole item itself. you can
// use the delay parameter to specify the duration for how long a pointer
// must press the child, until it can be dragged.
trailing: handle(
delay: const duration(milliseconds: 100),
child: icon(
icons.list,
color: colors.grey,
),
),
),
),
);
},
);
},
);
for a more in depth example click here.
caveats
note that this package is still in its very early phase and not enough testing has been done to guarantee stability.
also note that computing the diff between two very large lists my take significant amount of time (the computation is done on a background isolate though).
acknowledgements
the diff algorithm that implicitlyanimatedlist
uses was written by dawid bota at gitlab.
roadmap
you can take a look at the roadmap to see which featues i am working on or plan to implement in future versions.
Comments are closed.