KnownExtentsReorderableListView.builder constructor

const KnownExtentsReorderableListView.builder({
  1. Key? key,
  2. required IndexedWidgetBuilder itemBuilder,
  3. required int itemCount,
  4. required ReorderCallback onReorder,
  5. required List<double> itemExtents,
  6. ReorderItemProxyDecorator? proxyDecorator,
  7. bool buildDefaultDragHandles = true,
  8. EdgeInsets? padding,
  9. Widget? header,
  10. Axis scrollDirection = Axis.vertical,
  11. bool reverse = false,
  12. ScrollController? scrollController,
  13. bool? primary,
  14. ScrollPhysics? physics,
  15. bool shrinkWrap = false,
  16. double anchor = 0.0,
  17. double? cacheExtent,
  18. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  19. ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
  20. String? restorationId,
  21. 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:

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);