ReactiveFilePicker<T> constructor

ReactiveFilePicker<T>({
  1. Key? key,
  2. String? formControlName,
  3. FormControl<MultiFile<T>>? formControl,
  4. Map<String, ValidationMessageFunction>? validationMessages,
  5. ControlValueAccessor<MultiFile<T>, MultiFile<T>>? valueAccessor,
  6. ShowErrorsFunction<MultiFile<T>>? showErrors,
  7. InputDecoration? decoration,
  8. FilePickerBuilder<T>? filePickerBuilder,
  9. String? dialogTitle,
  10. bool allowMultiple = false,
  11. FileType type = FileType.any,
  12. List<String>? allowedExtensions,
  13. dynamic onFileLoading(
    1. FilePickerStatus
    )?,
  14. bool allowCompression = false,
  15. bool withData = false,
  16. bool withReadStream = false,
  17. bool lockParentWindow = false,
  18. double disabledOpacity = 0.5,
  19. String? initialDirectory,
})

Creates a ReactiveFilePicker that contains a FilePicker.

Can optionally provide a formControl to bind this widget to a control.

Can optionally provide a formControlName to bind this ReactiveFormField to a FormControl.

Must provide one of the arguments formControl or a formControlName, but not both at the same time.

Can optionally provide a validationMessages argument to customize a message for different kinds of validation errors.

Can optionally provide a valueAccessor to set a custom value accessors. See ControlValueAccessor.

Can optionally provide a showErrors function to customize when to show validation messages. Reactive Widgets make validation messages visible when the control is INVALID and TOUCHED, this behavior can be customized in the showErrors function.

Example:

Binds a text field.

final form = fb.group({'email': Validators.required});

ReactiveFilePicker(
  formControlName: 'email',
),

Binds a text field directly with a FormControl.

final form = fb.group({'email': Validators.required});

ReactiveFilePicker(
  formControl: form.control('email'),
),

Customize validation messages

ReactiveFilePicker(
  formControlName: 'email',
  validationMessages: {
    ValidationMessage.required: 'The email must not be empty',
    ValidationMessage.email: 'The email must be a valid email',
  }
),

Customize when to show up validation messages.

ReactiveFilePicker(
  formControlName: 'email',
  showErrors: (control) => control.invalid && control.touched && control.dirty,
),

For documentation about the various parameters, see the FilePicker class and FilePicker, the constructor.

Implementation

ReactiveFilePicker({
  Key? key,
  String? formControlName,
  FormControl<MultiFile<T>>? formControl,
  Map<String, ValidationMessageFunction>? validationMessages,
  ControlValueAccessor<MultiFile<T>, MultiFile<T>>? valueAccessor,
  ShowErrorsFunction<MultiFile<T>>? showErrors,

  ////////////////////////////////////////////////////////////////////////////
  InputDecoration? decoration,
  FilePickerBuilder<T>? filePickerBuilder,
  String? dialogTitle,
  bool allowMultiple = false,
  FileType type = FileType.any,
  List<String>? allowedExtensions,
  Function(FilePickerStatus)? onFileLoading,
  bool allowCompression = false,
  bool withData = false,
  bool withReadStream = false,
  bool lockParentWindow = false,
  double disabledOpacity = 0.5,
  String? initialDirectory,
}) : super(
        key: key,
        formControl: formControl,
        formControlName: formControlName,
        valueAccessor: valueAccessor,
        validationMessages: validationMessages,
        showErrors: showErrors,
        builder: (field) {
          final value = field.value ?? MultiFile<T>();
          final InputDecoration effectiveDecoration = (decoration ??
                  const InputDecoration())
              .applyDefaults(Theme.of(field.context).inputDecorationTheme);

          String? pickerError;

          pickFile() async {
            List<PlatformFile> platformFiles = [];
            try {
              platformFiles = ((await FilePicker.platform.pickFiles(
                    initialDirectory: initialDirectory,
                    dialogTitle: dialogTitle,
                    allowMultiple: allowMultiple,
                    type: type,
                    allowedExtensions: allowedExtensions,
                    onFileLoading: onFileLoading,
                    allowCompression: allowCompression,
                    withData: withData,
                    withReadStream: withReadStream,
                    lockParentWindow: lockParentWindow,
                  ))
                      ?.files) ??
                  [];
            } on PlatformException catch (e) {
              pickerError = "Unsupported operation $e";
            } catch (e) {
              pickerError = e.toString();
            }

            if (platformFiles.isNotEmpty) {
              field.control.markAsTouched();
              field.didChange(
                value.copyWith(
                  platformFiles: [
                    ...value.platformFiles,
                    ...platformFiles,
                  ],
                ),
              );
            }
          }

          return InputDecorator(
            decoration: effectiveDecoration.copyWith(
              errorText: field.errorText ?? pickerError,
              enabled: field.control.enabled,
            ),
            child: IgnorePointer(
              ignoring: !field.control.enabled,
              child: Opacity(
                opacity: field.control.enabled ? 1 : disabledOpacity,
                child: filePickerBuilder?.call(pickFile, value, (files) {
                  field.control.markAsTouched();
                  field.didChange(files);
                }),
              ),
            ),
          );
        },
      );