TextFormField constructor

TextFormField(
  1. {Key? key,
  2. TextEditingController? controller,
  3. String? initialValue,
  4. FocusNode? focusNode,
  5. InputDecoration? decoration = const InputDecoration(),
  6. TextInputType? keyboardType,
  7. TextCapitalization textCapitalization = TextCapitalization.none,
  8. TextInputAction? textInputAction,
  9. TextStyle? style,
  10. StrutStyle? strutStyle,
  11. TextDirection? textDirection,
  12. TextAlign textAlign = TextAlign.start,
  13. TextAlignVertical? textAlignVertical,
  14. bool autofocus = false,
  15. bool readOnly = false,
  16. @Deprecated('Use `contextMenuBuilder` instead. ' 'This feature was deprecated after v3.3.0-0.5.pre.') ToolbarOptions? toolbarOptions,
  17. bool? showCursor,
  18. String obscuringCharacter = '•',
  19. bool obscureText = false,
  20. bool autocorrect = true,
  21. SmartDashesType? smartDashesType,
  22. SmartQuotesType? smartQuotesType,
  23. bool enableSuggestions = true,
  24. MaxLengthEnforcement? maxLengthEnforcement,
  25. int? maxLines = 1,
  26. int? minLines,
  27. bool expands = false,
  28. int? maxLength,
  29. ValueChanged<String>? onChanged,
  30. GestureTapCallback? onTap,
  31. bool onTapAlwaysCalled = false,
  32. TapRegionCallback? onTapOutside,
  33. VoidCallback? onEditingComplete,
  34. ValueChanged<String>? onFieldSubmitted,
  35. FormFieldSetter<String>? onSaved,
  36. FormFieldValidator<String>? validator,
  37. List<TextInputFormatter>? inputFormatters,
  38. bool? enabled,
  39. double cursorWidth = 2.0,
  40. double? cursorHeight,
  41. Radius? cursorRadius,
  42. Color? cursorColor,
  43. Color? cursorErrorColor,
  44. Brightness? keyboardAppearance,
  45. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  46. bool? enableInteractiveSelection,
  47. TextSelectionControls? selectionControls,
  48. InputCounterWidgetBuilder? buildCounter,
  49. ScrollPhysics? scrollPhysics,
  50. Iterable<String>? autofillHints,
  51. AutovalidateMode? autovalidateMode,
  52. ScrollController? scrollController,
  53. String? restorationId,
  54. bool enableIMEPersonalizedLearning = true,
  55. MouseCursor? mouseCursor,
  56. EditableTextContextMenuBuilder? contextMenuBuilder = _defaultContextMenuBuilder,
  57. SpellCheckConfiguration? spellCheckConfiguration,
  58. TextMagnifierConfiguration? magnifierConfiguration,
  59. UndoHistoryController? undoController,
  60. AppPrivateCommandCallback? onAppPrivateCommand,
  61. bool? cursorOpacityAnimates,
  62. BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.tight,
  63. BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight,
  64. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  65. ContentInsertionConfiguration? contentInsertionConfiguration,
  66. MaterialStatesController? statesController,
  67. Clip clipBehavior = Clip.hardEdge,
  68. bool scribbleEnabled = true,
  69. bool canRequestFocus = true}
)

Creates a FormField that contains a TextField.

When a controller is specified, initialValue must be null (the default). If controller is null, then a TextEditingController will be constructed automatically and its text will be initialized to initialValue or the empty string.

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

Implementation

TextFormField({
  super.key,
  this.controller,
  String? initialValue,
  FocusNode? focusNode,
  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,
  @Deprecated(
    'Use `contextMenuBuilder` instead. '
    'This feature was deprecated after v3.3.0-0.5.pre.',
  )
  ToolbarOptions? toolbarOptions,
  bool? showCursor,
  String obscuringCharacter = '•',
  bool obscureText = false,
  bool autocorrect = true,
  SmartDashesType? smartDashesType,
  SmartQuotesType? smartQuotesType,
  bool enableSuggestions = true,
  MaxLengthEnforcement? maxLengthEnforcement,
  int? maxLines = 1,
  int? minLines,
  bool expands = false,
  int? maxLength,
  this.onChanged,
  GestureTapCallback? onTap,
  bool onTapAlwaysCalled = false,
  TapRegionCallback? onTapOutside,
  VoidCallback? onEditingComplete,
  ValueChanged<String>? onFieldSubmitted,
  super.onSaved,
  super.validator,
  List<TextInputFormatter>? inputFormatters,
  bool? enabled,
  double cursorWidth = 2.0,
  double? cursorHeight,
  Radius? cursorRadius,
  Color? cursorColor,
  Color? cursorErrorColor,
  Brightness? keyboardAppearance,
  EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  bool? enableInteractiveSelection,
  TextSelectionControls? selectionControls,
  InputCounterWidgetBuilder? buildCounter,
  ScrollPhysics? scrollPhysics,
  Iterable<String>? autofillHints,
  AutovalidateMode? autovalidateMode,
  ScrollController? scrollController,
  super.restorationId,
  bool enableIMEPersonalizedLearning = true,
  MouseCursor? mouseCursor,
  EditableTextContextMenuBuilder? contextMenuBuilder = _defaultContextMenuBuilder,
  SpellCheckConfiguration? spellCheckConfiguration,
  TextMagnifierConfiguration? magnifierConfiguration,
  UndoHistoryController? undoController,
  AppPrivateCommandCallback? onAppPrivateCommand,
  bool? cursorOpacityAnimates,
  ui.BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.tight,
  ui.BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight,
  DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  ContentInsertionConfiguration? contentInsertionConfiguration,
  MaterialStatesController? statesController,
  Clip clipBehavior = Clip.hardEdge,
  bool scribbleEnabled = true,
  bool canRequestFocus = true,
}) : assert(initialValue == null || controller == null),
     assert(obscuringCharacter.length == 1),
     assert(maxLines == null || maxLines > 0),
     assert(minLines == null || minLines > 0),
     assert(
       (maxLines == null) || (minLines == null) || (maxLines >= minLines),
       "minLines can't be greater than maxLines",
     ),
     assert(
       !expands || (maxLines == null && minLines == null),
       'minLines and maxLines must be null when expands is true.',
     ),
     assert(!obscureText || maxLines == 1, 'Obscured fields cannot be multiline.'),
     assert(maxLength == null || maxLength == TextField.noMaxLength || maxLength > 0),
     super(
       initialValue: controller != null ? controller.text : (initialValue ?? ''),
       enabled: enabled ?? decoration?.enabled ?? true,
       autovalidateMode: autovalidateMode ?? AutovalidateMode.disabled,
       builder: (FormFieldState<String> field) {
         final _TextFormFieldState state = field as _TextFormFieldState;
         final InputDecoration effectiveDecoration = (decoration ?? const InputDecoration())
             .applyDefaults(Theme.of(field.context).inputDecorationTheme);
         void onChangedHandler(String value) {
           field.didChange(value);
           onChanged?.call(value);
         }
         return UnmanagedRestorationScope(
           bucket: field.bucket,
           child: TextField(
             restorationId: restorationId,
             controller: state._effectiveController,
             focusNode: focusNode,
             decoration: effectiveDecoration.copyWith(errorText: field.errorText),
             keyboardType: keyboardType,
             textInputAction: textInputAction,
             style: style,
             strutStyle: strutStyle,
             textAlign: textAlign,
             textAlignVertical: textAlignVertical,
             textDirection: textDirection,
             textCapitalization: textCapitalization,
             autofocus: autofocus,
             statesController: statesController,
             toolbarOptions: toolbarOptions,
             readOnly: readOnly,
             showCursor: showCursor,
             obscuringCharacter: obscuringCharacter,
             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: onChangedHandler,
             onTap: onTap,
             onTapAlwaysCalled: onTapAlwaysCalled,
             onTapOutside: onTapOutside,
             onEditingComplete: onEditingComplete,
             onSubmitted: onFieldSubmitted,
             inputFormatters: inputFormatters,
             enabled: enabled ?? decoration?.enabled ?? true,
             cursorWidth: cursorWidth,
             cursorHeight: cursorHeight,
             cursorRadius: cursorRadius,
             cursorColor: cursorColor,
             cursorErrorColor: cursorErrorColor,
             scrollPadding: scrollPadding,
             scrollPhysics: scrollPhysics,
             keyboardAppearance: keyboardAppearance,
             enableInteractiveSelection: enableInteractiveSelection ?? (!obscureText || !readOnly),
             selectionControls: selectionControls,
             buildCounter: buildCounter,
             autofillHints: autofillHints,
             scrollController: scrollController,
             enableIMEPersonalizedLearning: enableIMEPersonalizedLearning,
             mouseCursor: mouseCursor,
             contextMenuBuilder: contextMenuBuilder,
             spellCheckConfiguration: spellCheckConfiguration,
             magnifierConfiguration: magnifierConfiguration,
             undoController: undoController,
             onAppPrivateCommand: onAppPrivateCommand,
             cursorOpacityAnimates: cursorOpacityAnimates,
             selectionHeightStyle: selectionHeightStyle,
             selectionWidthStyle: selectionWidthStyle,
             dragStartBehavior: dragStartBehavior,
             contentInsertionConfiguration: contentInsertionConfiguration,
             clipBehavior: clipBehavior,
             scribbleEnabled: scribbleEnabled,
             canRequestFocus: canRequestFocus,
           ),
         );
       },
     );