formArrayBuilder<T> function

Widget formArrayBuilder<T>(
  1. FormFieldState<List<T?>> field
)

The default FormFieldBuilder of FastFormArray.

Renders a ReorderableListView when reorderable is set to true which allows moving single items around via drag & drop.

Implementation

Widget formArrayBuilder<T>(FormFieldState<List<T?>> field) {
  field as FastFormArrayState<T>;
  final FastFormArrayState<T>(:decoration, :_keys, :value, :widget) = field;
  final hasItems = value is List<T?> && value.isNotEmpty;
  final children = <Widget>[
    if (hasItems)
      for (var index = 0; index < value.length; index++)
        widget.itemBuilder(_keys[index], index, field),
    if (!hasItems && widget.emptyBuilder != null) widget.emptyBuilder!(field),
  ];

  return InputDecorator(
    decoration: decoration,
    child: widget.reorderable
        ? ReorderableListView(
            shrinkWrap: true,
            onReorder: (int oldIndex, int newIndex) {
              /// see https://github.com/flutter/flutter/issues/24786
              /// see https://github.com/flutter/flutter/pull/93146
              if (newIndex > oldIndex) newIndex = newIndex - 1;

              /// when index does not change, do not trigger a field update.
              if (oldIndex == newIndex) return;
              field.move(oldIndex, newIndex);
            },
            children: children,
          )
        : Column(children: children),
  );
}