TextField constructor

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

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 maxLengthEnforced is set to false. 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 maxLengthEnforced is set to false, 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 and must not be null.

The textAlign, autofocus, obscureText, readOnly, autocorrect, maxLengthEnforced, scrollPadding, maxLines, maxLength, selectionHeightStyle, selectionWidthStyle, and enableSuggestions arguments must not be null.

See also:

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


const TextField({
  Key key,
  this.decoration = const InputDecoration(),
  TextInputType keyboardType,
  this.textCapitalization = TextCapitalization.none,
  this.textAlign = TextAlign.start,
  this.readOnly = false,
  ToolbarOptions toolbarOptions,
  this.autofocus = false,
  this.obscuringCharacter = '•',
  this.obscureText = false,
  this.autocorrect = true,
  SmartDashesType smartDashesType,
  SmartQuotesType smartQuotesType,
  this.enableSuggestions = true,
  this.maxLines = 1,
  this.expands = false,
  this.maxLengthEnforced = true,
  this.cursorWidth = 2.0,
  this.selectionHeightStyle = ui.BoxHeightStyle.tight,
  this.selectionWidthStyle = ui.BoxWidthStyle.tight,
  this.scrollPadding = const EdgeInsets.all(20.0),
  this.dragStartBehavior = DragStartBehavior.start,
  this.enableInteractiveSelection = true,
}) : assert(textAlign != null),
     assert(readOnly != null),
     assert(autofocus != null),
     assert(obscuringCharacter != null && obscuringCharacter.length == 1),
     assert(obscureText != null),
     assert(autocorrect != null),
     smartDashesType = smartDashesType ?? (obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
     smartQuotesType = smartQuotesType ?? (obscureText ? SmartQuotesType.disabled : SmartQuotesType.enabled),
     assert(enableSuggestions != null),
     assert(enableInteractiveSelection != null),
     assert(maxLengthEnforced != null),
     assert(scrollPadding != null),
     assert(dragStartBehavior != null),
     assert(selectionHeightStyle != null),
     assert(selectionWidthStyle != null),
     assert(maxLines == null || maxLines > 0),
     assert(minLines == null || minLines > 0),
       (maxLines == null) || (minLines == null) || (maxLines >= minLines),
       "minLines can't be greater than maxLines",
     assert(expands != null),
       !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),
     toolbarOptions = toolbarOptions ?? (obscureText ?
       const ToolbarOptions(
         selectAll: true,
         paste: true,
       ) :
       const ToolbarOptions(
         copy: true,
         cut: true,
         selectAll: true,
         paste: true,
     super(key: key);