FormFutureField<T extends Object, TValue> constructor

FormFutureField<T extends Object, TValue>({
  1. FormController<TValue>? form,
  2. FormStyle? style,
  3. required FutureOr<T?> onTap(
    1. T? currentValue
    ),
  4. Widget builder(
    1. BuildContext context,
    2. FormFutureFieldRef<T, TValue> value
    )?,
  5. String parseToString(
    1. T? value
    )?,
  6. FormAffixStyle? prefix,
  7. FormAffixStyle? suffix,
  8. bool obscureText = false,
  9. String? hintText,
  10. bool readOnly = false,
  11. String? labelText,
  12. void onChanged(
    1. T? value
    )?,
  13. String? emptyErrorText,
  14. bool keepAlive = true,
  15. bool showDropdownIcon = true,
  16. Widget? dropdownIcon,
  17. Key? key,
  18. TValue onSaved(
    1. T value
    )?,
  19. String? validator(
    1. T? value
    )?,
  20. T? initialValue,
  21. bool enabled = true,
})

A form that waits for another process to complete and updates the value of the form based on that value.

Use this when moving to another page and updating form values based on values entered on that page, or updating form values based on API responses.

Specifying onTap describes the data acquisition process after a tap.

You can freely change the display by passing a widget with builder. If builder is not passed, a TextField will be displayed. In that case, you can use parseToString to convert the value of T to a string.

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 no characters are entered.

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.

If enabled is false, the text is deactivated.

If readOnly is set to true, the activation is displayed, but the text cannot be changed.

別の処理の完了を待ってその値を元にフォームの値を更新するフォーム。

別のページに遷移してそのページで入力した値を元にフォームの値を更新する場合やAPIのレスポンスを元にフォームの値を更新する場合に使用します。

onTapを指定することでタップした後のデータ取得処理を記述します。

builderでウィジェットを渡すことで自由に表示を変更できます。builderが渡されない場合はTextFieldが表示されます。その際parseToStringTの値を文字列に変換することが可能です。

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

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

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

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

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

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

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

enabledfalseになるとテキストが非有効化されます。

readOnlytrueになっている場合は、有効化の表示になりますが、テキストが変更できなくなります。

Implementation

FormFutureField({
  this.form,
  this.style,
  required this.onTap,
  Widget Function(
    BuildContext context,
    FormFutureFieldRef<T, TValue> value,
  )? builder,
  this.parseToString,
  this.prefix,
  this.suffix,
  this.obscureText = false,
  this.hintText,
  this.readOnly = false,
  this.labelText,
  this.onChanged,
  this.emptyErrorText,
  this.keepAlive = true,
  this.showDropdownIcon = true,
  this.dropdownIcon,
  super.key,
  TValue Function(T value)? onSaved,
  String? Function(T? value)? validator,
  super.initialValue,
  super.enabled,
})  : _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 == null) {
            return emptyErrorText;
          }
          return validator?.call(value);
        },
      );