ReactiveTextField<T> constructor

ReactiveTextField<T>({
  1. Key? key,
  2. String? formControlName,
  3. FormControl<T>? formControl,
  4. ValidationMessagesFunction<T>? validationMessages,
  5. ControlValueAccessor<T, String>? valueAccessor,
  6. ShowErrorsFunction? showErrors,
  7. InputDecoration decoration = const InputDecoration(),
  8. TextInputType? keyboardType,
  9. TextCapitalization textCapitalization = TextCapitalization.none,
  10. TextInputAction? textInputAction,
  11. TextStyle? style,
  12. StrutStyle? strutStyle,
  13. TextDirection? textDirection,
  14. TextAlign textAlign = TextAlign.start,
  15. TextAlignVertical? textAlignVertical,
  16. bool autofocus = false,
  17. bool readOnly = false,
  18. ToolbarOptions? toolbarOptions,
  19. bool? showCursor,
  20. bool obscureText = false,
  21. String obscuringCharacter = '•',
  22. bool autocorrect = true,
  23. SmartDashesType? smartDashesType,
  24. SmartQuotesType? smartQuotesType,
  25. bool enableSuggestions = true,
  26. MaxLengthEnforcement? maxLengthEnforcement,
  27. int? maxLines = 1,
  28. int? minLines,
  29. bool expands = false,
  30. int? maxLength,
  31. GestureTapCallback? onTap,
  32. VoidCallback? onEditingComplete,
  33. List<TextInputFormatter>? inputFormatters,
  34. double cursorWidth = 2.0,
  35. double? cursorHeight,
  36. Radius? cursorRadius,
  37. Color? cursorColor,
  38. Brightness? keyboardAppearance,
  39. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  40. bool enableInteractiveSelection = true,
  41. InputCounterWidgetBuilder? buildCounter,
  42. ScrollPhysics? scrollPhysics,
  43. VoidCallback? onSubmitted,
  44. FocusNode? focusNode,
  45. Iterable<String>? autofillHints,
  46. MouseCursor? mouseCursor,
  47. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  48. AppPrivateCommandCallback? onAppPrivateCommand,
  49. String? restorationId,
  50. ScrollController? scrollController,
  51. TextSelectionControls? selectionControls,
  52. BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.tight,
  53. BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight,
  54. TextEditingController? controller,
  55. Clip clipBehavior = Clip.hardEdge,
  56. bool enableIMEPersonalizedLearning = true,
  57. bool scribbleEnabled = true,
})

Creates a ReactiveTextField that contains a TextField.

Can optionally provide a formControl to bind this widget to a control.

Can optionally provide a formControlName to bind this ReactiveFormField to a FormControl.

Must provide one of the arguments formControl or a formControlName, but not both at the same time.

Can optionally provide a validationMessages argument to customize a message for different kinds of validation errors.

Can optionally provide a valueAccessor to set a custom value accessors. See ControlValueAccessor.

Can optionally provide a showErrors function to customize when to show validation messages. Reactive Widgets make validation messages visible when the control is INVALID and TOUCHED, this behavior can be customized in the showErrors function.

Example:

Binds a text field.

final form = fb.group({'email': Validators.required});

ReactiveTextField(
  formControlName: 'email',
),

Binds a text field directly with a FormControl.

final form = fb.group({'email': Validators.required});

ReactiveTextField(
  formControl: form.control('email'),
),

Customize validation messages

ReactiveTextField(
  formControlName: 'email',
  validationMessages: {
    ValidationMessage.required: 'The email must not be empty',
    ValidationMessage.email: 'The email must be a valid email',
  }
),

Customize when to show up validation messages.

ReactiveTextField(
  formControlName: 'email',
  showErrors: (control) => control.invalid && control.touched && control.dirty,
),

For documentation about the various parameters, see the TextField class and TextField, the constructor.

Implementation

ReactiveTextField({
  Key? key,
  String? formControlName,
  FormControl<T>? formControl,
  ValidationMessagesFunction<T>? validationMessages,
  ControlValueAccessor<T, String>? valueAccessor,
  ShowErrorsFunction? showErrors,
  InputDecoration decoration = const InputDecoration(),
  TextInputType? keyboardType,
  TextCapitalization textCapitalization = TextCapitalization.none,
  TextInputAction? textInputAction,
  TextStyle? style,
  StrutStyle? strutStyle,
  TextDirection? textDirection,
  TextAlign textAlign = TextAlign.start,
  TextAlignVertical? textAlignVertical,
  bool autofocus = false,
  bool readOnly = false,
  ToolbarOptions? toolbarOptions,
  bool? showCursor,
  bool obscureText = false,
  String obscuringCharacter = '•',
  bool autocorrect = true,
  SmartDashesType? smartDashesType,
  SmartQuotesType? smartQuotesType,
  bool enableSuggestions = true,
  MaxLengthEnforcement? maxLengthEnforcement,
  int? maxLines = 1,
  int? minLines,
  bool expands = false,
  int? maxLength,
  GestureTapCallback? onTap,
  VoidCallback? onEditingComplete,
  List<TextInputFormatter>? inputFormatters,
  double cursorWidth = 2.0,
  double? cursorHeight,
  Radius? cursorRadius,
  Color? cursorColor,
  Brightness? keyboardAppearance,
  EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  bool enableInteractiveSelection = true,
  InputCounterWidgetBuilder? buildCounter,
  ScrollPhysics? scrollPhysics,
  VoidCallback? onSubmitted,
  FocusNode? focusNode,
  Iterable<String>? autofillHints,
  MouseCursor? mouseCursor,
  DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  AppPrivateCommandCallback? onAppPrivateCommand,
  String? restorationId,
  ScrollController? scrollController,
  TextSelectionControls? selectionControls,
  ui.BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.tight,
  ui.BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight,
  TextEditingController? controller,
  Clip clipBehavior = Clip.hardEdge,
  bool enableIMEPersonalizedLearning = true,
  bool scribbleEnabled = true,
})  : _textController = controller,
      super(
        key: key,
        formControl: formControl,
        formControlName: formControlName,
        valueAccessor: valueAccessor,
        validationMessages: validationMessages,
        showErrors: showErrors,
        builder: (ReactiveFormFieldState<T, String> field) {
          final state = field as _ReactiveTextFieldState<T>;
          final effectiveDecoration = decoration
              .applyDefaults(Theme.of(state.context).inputDecorationTheme);

          state._setFocusNode(focusNode);

          return TextField(
            controller: state._textController,
            focusNode: state.focusNode,
            decoration:
                effectiveDecoration.copyWith(errorText: state.errorText),
            keyboardType: keyboardType,
            textInputAction: textInputAction,
            style: style,
            strutStyle: strutStyle,
            textAlign: textAlign,
            textAlignVertical: textAlignVertical,
            textDirection: textDirection,
            textCapitalization: textCapitalization,
            autofocus: autofocus,
            toolbarOptions: toolbarOptions,
            readOnly: readOnly,
            showCursor: showCursor,
            obscureText: obscureText,
            autocorrect: autocorrect,
            smartDashesType: smartDashesType ??
                (obscureText
                    ? SmartDashesType.disabled
                    : SmartDashesType.enabled),
            smartQuotesType: smartQuotesType ??
                (obscureText
                    ? SmartQuotesType.disabled
                    : SmartQuotesType.enabled),
            enableSuggestions: enableSuggestions,
            maxLengthEnforcement: maxLengthEnforcement,
            maxLines: maxLines,
            minLines: minLines,
            expands: expands,
            maxLength: maxLength,
            onChanged: field.didChange,
            onTap: onTap,
            onSubmitted: onSubmitted != null ? (_) => onSubmitted() : null,
            onEditingComplete: onEditingComplete,
            inputFormatters: inputFormatters,
            enabled: field.control.enabled,
            cursorWidth: cursorWidth,
            cursorHeight: cursorHeight,
            cursorRadius: cursorRadius,
            cursorColor: cursorColor,
            scrollPadding: scrollPadding,
            scrollPhysics: scrollPhysics,
            keyboardAppearance: keyboardAppearance,
            enableInteractiveSelection: enableInteractiveSelection,
            buildCounter: buildCounter,
            autofillHints: autofillHints,
            mouseCursor: mouseCursor,
            obscuringCharacter: obscuringCharacter,
            dragStartBehavior: dragStartBehavior,
            onAppPrivateCommand: onAppPrivateCommand,
            restorationId: restorationId,
            scrollController: scrollController,
            selectionControls: selectionControls,
            selectionHeightStyle: selectionHeightStyle,
            selectionWidthStyle: selectionWidthStyle,
            clipBehavior: clipBehavior,
            enableIMEPersonalizedLearning: enableIMEPersonalizedLearning,
            scribbleEnabled: scribbleEnabled,
          );
        },
      );