FormMedia<TValue> constructor
- Key? key,
- FormStyle? style,
- void onChanged(
- FormMediaValue? value
- bool readOnly = false,
- FormController<
TValue> ? form, - required void onTap(
- void onUpdate(
- String filePath,
- FormMediaType type
- void onUpdate(
- required Widget builder(
- BuildContext context,
- FormMediaValue value
- bool showOverlayIcon = true,
- Color overlayColor = Colors.black38,
- Color overlayIconColor = Colors.white70,
- IconData icon = Icons.add_a_photo,
- double iconSize = 56.0,
- String? emptyErrorText,
- TValue onSaved(
- FormMediaValue value
- String validator(
- FormMediaValue? value
- FormMediaValue? initialValue,
- bool enabled = true,
- 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_picker
やfile_picker
を用いてファイルを選択するダイアログを表示しメディアのパスをonUpdate
に返すようにします。
builder
にFormMediaValueを元に実際に画像を表示する部分を実装してください。
FormController.keyを与えたForm配下に配置、もしくはformにFormControllerを渡します。
formにFormControllerを渡した場合、一緒にonSaved
も渡してください。データの保存はonSaved
の内容が実行されます。
initialValue
にFormController.valueから与えられた初期値を入力します。
内容が変更される度onChangedが実行されます。
FormController.validateAndSaveが実行された場合、バリデーションとデータの保存を行ないます。
emptyErrorTextが指定されている時に限り、項目が指定されていない場合emptyErrorTextがエラーとして表示されます。
それ以外のエラーチェックはvalidator
を指定することで行ないます。
コールバック内でNull以外を返すようにするとその文字列がエラー文として表示されます。Nullの場合はエラーなしとして処理されます。
enabled
がfalse
になる、もしくはreadOnlyがtrue
になっている場合は非有効化されます。
showOverlayIconをtrue
にするとメディアが選択された後でもそのプレビューの上にアイコンを表示します。iconやiconSize、overlayColor、overlayIconColorを指定することでアイコンやオーバーレイのデザインを変更することができます。
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,
);