FormMultiMedia<TValue> constructor

FormMultiMedia<TValue>({
  1. Key? key,
  2. FormController<TValue>? form,
  3. FormStyle? style,
  4. void onRemove(
    1. FormMediaValue value
    )?,
  5. int? maxLength,
  6. List<FormMediaValue>? initialValue,
  7. String? emptyErrorText,
  8. bool readOnly = false,
  9. required Widget builder(
    1. BuildContext context,
    2. FormMediaValue value
    ),
  10. required void onTap(
    1. FormMultiMediaRef ref
    ),
  11. void onChanged(
    1. List<FormMediaValue> value
    )?,
  12. TValue onSaved(
    1. List<FormMediaValue> value
    )?,
  13. String validator(
    1. List<FormMediaValue> value
    )?,
  14. FormMultiMediaDelegate delegate = const FormMultiMediaInlineDelegate(),
  15. bool enabled = true,
  16. bool keepAlive = true,
})

Form for submitting images and videos. Multiple media can be submitted.

All media is managed by FormMediaValue.

The path to the file asset and the asset type are passed to FormMediaValue as FormMediaType, so display and save accordingly.

Describe the process when the form is tapped in the onTap field. Normally, use image_picker or file_picker to display a dialog to select a file and return the media path to onUpdate.

Implement the part that actually displays the image based on the FormMediaValue in builder.

Specify a callback for deleting items in onRemove.

The entire design can be changed by specifying delegate. Specify FormMultiMediaInlineDelegate to display a list of media inline. Specify FormMultiMediaListTileDelegate to display a list of media in a vertical list view.

Place under the Form that gave FormController.key, or pass FormController to form.

When FormController is passed to form, onSaved must also be passed together. The contents of onSaved will be used to save the data.

Enter the initial value given by FormController.value in initialValue.

Each time the content is changed, onChanged is executed.

When FormController.validate is executed, validation and data saving are performed.

Only when emptyErrorText is specified, emptyErrorText will be displayed as an error if the item is not specified.

Other error checking is performed by specifying validator. If a string other than Null is returned in the callback, the string is displayed as an error statement. If Null is returned, it is processed as no error.

Deactivated if enabled is false or readOnly is true.

If maxLength is specified, no more items can be added.

画像や映像を投稿するためのフォーム。複数のメディアを投稿することが可能です。

メディアはすべてFormMediaValueで管理されます。

FormMediaValueにファイルアセットへのパスとアセットのタイプがFormMediaTypeで渡されるのでそれに応じて表示や保存を行ってください。

onTapにフォームがタップされた場合の処理を記述します。通常はimage_pickerfile_pickerを用いてファイルを選択するダイアログを表示しメディアのパスをonUpdateに返すようにします。

builderFormMediaValueを元に実際に画像を表示する部分を実装してください。

onRemoveに項目の削除時のコールバックを指定します。

delegateを指定することで、全体のデザインを変更することが可能です。 FormMultiMediaInlineDelegateを指定するとインライン内でメディアの一覧が表示されるようになります。FormMultiMediaListTileDelegateを指定すると縦にリスト表示でメディアの一覧が表示されるようになります。

FormController.keyを与えたForm配下に配置、もしくはformFormControllerを渡します。

formFormControllerを渡した場合、一緒にonSavedも渡してください。データの保存はonSavedの内容が実行されます。

initialValueFormController.valueから与えられた初期値を入力します。

内容が変更される度onChangedが実行されます。

FormController.validateが実行された場合、バリデーションとデータの保存を行ないます。

emptyErrorTextが指定されている時に限り、項目が指定されていない場合emptyErrorTextがエラーとして表示されます。

それ以外のエラーチェックはvalidatorを指定することで行ないます。 コールバック内でNull以外を返すようにするとその文字列がエラー文として表示されます。Nullの場合はエラーなしとして処理されます。

enabledfalseになる、もしくはreadOnlytrueになっている場合は非有効化されます。

maxLengthが指定されている場合、それ以上項目を増やすことができなくなります。

Implementation

FormMultiMedia({
  super.key,
  this.form,
  this.style,
  this.onRemove,
  this.maxLength,
  List<FormMediaValue>? initialValue,
  this.emptyErrorText,
  this.readOnly = false,
  required Widget Function(
    BuildContext context,
    FormMediaValue value,
  ) builder,
  required this.onTap,
  this.onChanged,
  TValue Function(List<FormMediaValue> value)? onSaved,
  String Function(List<FormMediaValue> value)? validator,
  this.delegate = const FormMultiMediaInlineDelegate(),
  super.enabled,
  this.keepAlive = true,
})  : _builder = builder,
      assert(
        (form == null && onSaved == null) ||
            (form != null && onSaved != null),
        "Both are required when using [form] or [onSaved].",
      ),
      super(
        builder: (state) {
          return const SizedBox.shrink();
        },
        onSaved: (value) {
          if (value == null) {
            return;
          }
          final res = onSaved?.call(value);
          if (res == null) {
            return;
          }
          form!.value = res;
        },
        validator: (value) {
          if (emptyErrorText.isNotEmpty && value.isEmpty) {
            return emptyErrorText;
          }
          return validator?.call(value ?? []);
        },
        initialValue: initialValue ?? [],
      );