TextField constructor

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

Creates a Material Design text field.

If decoration is non-null (which is the default), the text field requires one of its ancestors to be a Material widget.

To remove the decoration entirely (including the extra padding introduced by the decoration to save space for the labels), set the decoration to null.

The maxLines property can be set to null to remove the restriction on the number of lines. By default, it is one, meaning this is a single-line text field. maxLines must not be zero.

The maxLength property is set to null by default, which means the number of characters allowed in the text field is not restricted. If maxLength is set a character counter will be displayed below the field showing how many characters have been entered. If the value is set to a positive integer it will also display the maximum allowed number of characters to be entered. If the value is set to TextField.noMaxLength then only the current length is displayed.

After maxLength characters have been input, additional input is ignored, unless maxLengthEnforcement is set to MaxLengthEnforcement.none. The text field enforces the length with a LengthLimitingTextInputFormatter, which is evaluated after the supplied inputFormatters, if any. The maxLength value must be either null or greater than zero.

If maxLengthEnforcement is set to MaxLengthEnforcement.none, then more than maxLength characters may be entered, and the error counter and divider will switch to the decoration.errorStyle when the limit is exceeded.

The text cursor is not shown if showCursor is false or if showCursor is null (the default) and readOnly is true.

The selectionHeightStyle and selectionWidthStyle properties allow changing the shape of the selection highlighting. These properties default to ui.BoxHeightStyle.tight and ui.BoxWidthStyle.tight, respectively.

See also:

  • maxLength, which discusses the precise meaning of "number of characters" and how it may differ from the intuitive meaning.

Implementation

const TextField({
  super.key,
  this.controller,
  this.focusNode,
  this.undoController,
  this.decoration = const InputDecoration(),
  TextInputType? keyboardType,
  this.textInputAction,
  this.textCapitalization = TextCapitalization.none,
  this.style,
  this.strutStyle,
  this.textAlign = TextAlign.start,
  this.textAlignVertical,
  this.textDirection,
  this.readOnly = false,
  @Deprecated(
    'Use `contextMenuBuilder` instead. '
    'This feature was deprecated after v3.3.0-0.5.pre.',
  )
  this.toolbarOptions,
  this.showCursor,
  this.autofocus = false,
  this.statesController,
  this.obscuringCharacter = '•',
  this.obscureText = false,
  this.autocorrect = true,
  SmartDashesType? smartDashesType,
  SmartQuotesType? smartQuotesType,
  this.enableSuggestions = true,
  this.maxLines = 1,
  this.minLines,
  this.expands = false,
  this.maxLength,
  this.maxLengthEnforcement,
  this.onChanged,
  this.onEditingComplete,
  this.onSubmitted,
  this.onAppPrivateCommand,
  this.inputFormatters,
  this.enabled,
  this.cursorWidth = 2.0,
  this.cursorHeight,
  this.cursorRadius,
  this.cursorOpacityAnimates,
  this.cursorColor,
  this.cursorErrorColor,
  this.selectionHeightStyle = ui.BoxHeightStyle.tight,
  this.selectionWidthStyle = ui.BoxWidthStyle.tight,
  this.keyboardAppearance,
  this.scrollPadding = const EdgeInsets.all(20.0),
  this.dragStartBehavior = DragStartBehavior.start,
  bool? enableInteractiveSelection,
  this.selectionControls,
  this.onTap,
  this.onTapAlwaysCalled = false,
  this.onTapOutside,
  this.mouseCursor,
  this.buildCounter,
  this.scrollController,
  this.scrollPhysics,
  this.autofillHints = const <String>[],
  this.contentInsertionConfiguration,
  this.clipBehavior = Clip.hardEdge,
  this.restorationId,
  this.scribbleEnabled = true,
  this.enableIMEPersonalizedLearning = true,
  this.contextMenuBuilder = _defaultContextMenuBuilder,
  this.canRequestFocus = true,
  this.spellCheckConfiguration,
  this.magnifierConfiguration,
}) : assert(obscuringCharacter.length == 1),
     smartDashesType = smartDashesType ?? (obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
     smartQuotesType = smartQuotesType ?? (obscureText ? SmartQuotesType.disabled : SmartQuotesType.enabled),
     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),
     // Assert the following instead of setting it directly to avoid surprising the user by silently changing the value they set.
     assert(
       !identical(textInputAction, TextInputAction.newline) ||
       maxLines == 1 ||
       !identical(keyboardType, TextInputType.text),
       'Use keyboardType TextInputType.multiline when using TextInputAction.newline on a multiline TextField.',
     ),
     keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),
     enableInteractiveSelection = enableInteractiveSelection ?? (!readOnly || !obscureText);