FormMedia<TValue> constructor

FormMedia<TValue>({
  1. Key? key,
  2. FormStyle? style,
  3. void onChanged(
    1. FormMediaValue? value
    )?,
  4. bool readOnly = false,
  5. FormController<TValue>? form,
  6. required void onTap(
    1. void onUpdate(
      1. String filePath,
      2. FormMediaType type
      )
    )?,
  7. required Widget builder(
    1. BuildContext context,
    2. FormMediaValue value
    ),
  8. bool showOverlayIcon = true,
  9. Color overlayColor = Colors.black38,
  10. Color overlayIconColor = Colors.white70,
  11. IconData icon = Icons.add_a_photo,
  12. double iconSize = 56.0,
  13. String? emptyErrorText,
  14. TValue onSaved(
    1. FormMediaValue value
    )?,
  15. String validator(
    1. FormMediaValue? value
    )?,
  16. FormMediaValue? initialValue,
  17. bool enabled = true,
  18. bool keepAlive = true,
})

Form for submitting images and videos. Single 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.

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.validateAndSave 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 showOverlayIcon is set to true, the icon is displayed over the preview even after the media is selected. You can change the design of icons and overlays by specifying icon, iconSize, overlayColor, and overlayIconColor.

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

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

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

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

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

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

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

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

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

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

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

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

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

showOverlayIcontrueにするとメディアが選択された後でもそのプレビューの上にアイコンを表示します。iconiconSizeoverlayColoroverlayIconColorを指定することでアイコンやオーバーレイのデザインを変更することができます。

Implementation

FormMedia({
  Key? key,
  this.style,
  this.onChanged,
  this.readOnly = false,
  this.form,
  required this.onTap,
  required Widget Function(
    BuildContext context,
    FormMediaValue value,
  )
      builder,
  this.showOverlayIcon = true,
  this.overlayColor = Colors.black38,
  this.overlayIconColor = Colors.white70,
  this.icon = Icons.add_a_photo,
  this.iconSize = 56.0,
  this.emptyErrorText,
  TValue Function(FormMediaValue value)? onSaved,
  String Function(FormMediaValue? value)? validator,
  FormMediaValue? initialValue,
  bool enabled = true,
  this.keepAlive = true,
})  : _builder = builder,
      super(
        key: key,
        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 == null) {
            return emptyErrorText;
          }
          return validator?.call(value);
        },
        initialValue: initialValue,
        enabled: enabled,
      );