KnownExtentsReorderableListView.builder constructor
- Key? key,
- required IndexedWidgetBuilder itemBuilder,
- required int itemCount,
- required ReorderCallback onReorder,
- required List<
double> itemExtents, - ReorderItemProxyDecorator? proxyDecorator,
- bool buildDefaultDragHandles = true,
- EdgeInsets? padding,
- Widget? header,
- Axis scrollDirection = Axis.vertical,
- bool reverse = false,
- ScrollController? scrollController,
- bool? primary,
- ScrollPhysics? physics,
- bool shrinkWrap = false,
- double anchor = 0.0,
- double? cacheExtent,
- DragStartBehavior dragStartBehavior = DragStartBehavior.start,
- ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
- String? restorationId,
- Clip clipBehavior = Clip.hardEdge,
Creates a reorderable list from widget items that are created on demand.
This constructor is appropriate for list views with a large number of children because the builder is called only for those children that are actually visible.
The itemBuilder
callback will be called only with indices greater than
or equal to zero and less than itemCount
.
The itemBuilder
should always return a non-null widget, and actually
create the widget instances when called. Avoid using a builder that
returns a previously-constructed widget; if the list view's children are
created in advance, or all at once when the KnownExtentsReorderableListView itself
is created, it is more efficient to use the KnownExtentsReorderableListView
constructor. Even more efficient, however, is to create the instances
on demand using this constructor's itemBuilder
callback.
This example creates a list using the KnownExtentsReorderableListView.builder constructor. Using the IndexedWidgetBuilder, The list items are built lazily on demand. {@tool dartpad --template=stateful_widget_material}
final List<int> _items = List<int>.generate(50, (int index) => index);
@override
Widget build(BuildContext context) {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
final Color oddItemColor = colorScheme.primary.withOpacity(0.05);
final Color evenItemColor = colorScheme.primary.withOpacity(0.15);
return KnownExtentsReorderableListView.builder(
padding: const EdgeInsets.symmetric(horizontal: 40),
itemCount:_items.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
key: Key('$index'),
tileColor: _items[index].isOdd ? oddItemColor : evenItemColor,
title: Text('Item ${_items[index]}'),
);
},
onReorder: (int oldIndex, int newIndex) {
setState(() {
if (oldIndex < newIndex) {
newIndex -= 1;
}
final int item = _items.removeAt(oldIndex);
_items.insert(newIndex, item);
});
},
);
}
{@end-tool} See also:
- KnownExtentsReorderableListView, which allows you to build a reorderable list with all the items passed into the constructor.
Implementation
const KnownExtentsReorderableListView.builder({
Key? key,
required this.itemBuilder,
required this.itemCount,
required this.onReorder,
required this.itemExtents,
this.proxyDecorator,
this.buildDefaultDragHandles = true,
this.padding,
this.header,
this.scrollDirection = Axis.vertical,
this.reverse = false,
this.scrollController,
this.primary,
this.physics,
this.shrinkWrap = false,
this.anchor = 0.0,
this.cacheExtent,
this.dragStartBehavior = DragStartBehavior.start,
this.keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
this.restorationId,
this.clipBehavior = Clip.hardEdge,
}) : assert(itemCount >= 0),
super(key: key);