buildTextFormField method

  1. @protected
Widget buildTextFormField(
  1. BuildParameters params
)

Implementation

@protected
Widget buildTextFormField(BuildParameters params) {
  final props = params.props;
  final actions = params.actions;
  final alias = props["alias"] ?? params.id;
  final enabled = parseBool(props["enabled"], defaultValue: true);
  final textStyle =
      enabled ? params.buildProp("style") : params.buildProp("disabledStyle");
  final textInputAction =
      Types.textInputAction.build(props["textInputAction"]);
  final controller = props["controller"] ??
      TextEditingController(text: props["value"]?.toString());
  controller.selection =
      TextSelection.collapsed(offset: controller.text.length);
  props["controller"] = controller;
  final onSaved = props["onSaved"] ??
      (v) {
        if (alias != null) {
          params.state[alias] = v;
        }
      };

  final widgetMap = <String, Widget>{};
  for (var key in params.widgets.keys) {
    widgetMap[key] = builder.buildWidget(params.context, params.widgets[key],
        params.state, params.parentContext);
  }

  var onFieldSubmitted = events.getValueFunction(params.context,
      actions["onFieldSubmitted"], params.state, params.parentContext);
  var onEditingComplete = events.getValueFunction(params.context,
      actions["onEditingComplete"], params.state, params.parentContext);
  final onChangedAction = events.getValueFunction(params.context,
      actions["onChanged"], params.state, params.parentContext);

  ActionValueFunction<String>? onChanged;
  if (onChangedAction != null) {
    final onChangeDebounce =
        parseInt(props["onChangedDebounce"], defaultValue: 500);
    Timer? timer;
    onChanged = (value) {
      if (timer?.isActive ?? false) timer!.cancel();
      timer = Timer(Duration(milliseconds: onChangeDebounce),
          () => onChangedAction(value));
    };
    onFieldSubmitted ??= (value) {
      if (timer?.isActive ?? false) timer!.cancel();
      onChangedAction(value);
    };
  }

  if (onFieldSubmitted != null && onEditingComplete == null) {
    onEditingComplete = (v) {};
  }

  VoidCallback? finalOnEditingComplete;
  if (onEditingComplete != null) {
    finalOnEditingComplete = () => onEditingComplete!(controller.text);
  }

  return TextFormField(
    key: properties.getKey(params.id),
    controller: controller,
    autovalidateMode: AutovalidateMode.disabled,
    obscureText: parseBool(props["obscureText"]),
    enabled: enabled,
    textInputAction: textInputAction,
    autocorrect: parseBool(props["autocorrect"], defaultValue: true),
    keyboardType: params.buildProp("keyboardType"),
    minLines: tryParseInt(props["minLines"]),
    maxLines: parseInt(props["maxLines"], defaultValue: 1),
    maxLengthEnforcement: MaxLengthEnforcement.enforced,
    textAlign: params.buildProp("textAlign"),
    textAlignVertical: params.buildProp("textAlignVertical"),
    style: textStyle,
    readOnly: parseBool(props["readOnly"]),
    autofocus: parseBool(props["autofocus"]),
    validator: builder.getStringValidator(props),
    enableSuggestions:
        parseBool(props["enableSuggestions"], defaultValue: true),
    expands: parseBool(props["expands"]),
    inputFormatters: params.buildProp("inputFormatters"),
    contextMenuBuilder: params.buildProp("toolbarOptions"),
    textCapitalization: params.buildProp("textCapitalization"),
    decoration: params.buildProp("decoration", argument: widgetMap),
    onTap: events.getFunction(params.context, params.actions["onTap"],
        params.state, params.parentContext),
    onEditingComplete: finalOnEditingComplete,
    onFieldSubmitted: onFieldSubmitted,
    onChanged: onChanged,
    onSaved: onSaved,
  );
}