TextField constructor

const TextField({Key key, TextEditingController controller, FocusNode focusNode, InputDecoration decoration: const InputDecoration(), TextInputType keyboardType, TextInputAction textInputAction, TextCapitalization textCapitalization: TextCapitalization.none, TextStyle style, StrutStyle strutStyle, TextAlign textAlign: TextAlign.start, TextDirection textDirection, bool autofocus: false, bool obscureText: false, bool autocorrect: true, int maxLines: 1, int minLines, bool expands: false, int maxLength, bool maxLengthEnforced: true, ValueChanged<String> onChanged, VoidCallback onEditingComplete, ValueChanged<String> onSubmitted, List<TextInputFormatter> inputFormatters, bool enabled, double cursorWidth: 2.0, Radius cursorRadius, Color cursorColor, Brightness keyboardAppearance, EdgeInsets scrollPadding: const EdgeInsets.all(20.0), DragStartBehavior dragStartBehavior: DragStartBehavior.start, bool enableInteractiveSelection, GestureTapCallback onTap, InputCounterWidgetBuilder buildCounter, ScrollPhysics scrollPhysics })

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 textAlign, autofocus, obscureText, autocorrect, maxLengthEnforced, scrollPadding, maxLines, and maxLength 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.

Implementation

const TextField({
  Key key,
  this.controller,
  this.focusNode,
  this.decoration = const InputDecoration(),
  TextInputType keyboardType,
  this.textInputAction,
  this.textCapitalization = TextCapitalization.none,
  this.style,
  this.strutStyle,
  this.textAlign = TextAlign.start,
  this.textDirection,
  this.autofocus = false,
  this.obscureText = false,
  this.autocorrect = true,
  this.maxLines = 1,
  this.minLines,
  this.expands = false,
  this.maxLength,
  this.maxLengthEnforced = true,
  this.onChanged,
  this.onEditingComplete,
  this.onSubmitted,
  this.inputFormatters,
  this.enabled,
  this.cursorWidth = 2.0,
  this.cursorRadius,
  this.cursorColor,
  this.keyboardAppearance,
  this.scrollPadding = const EdgeInsets.all(20.0),
  this.dragStartBehavior = DragStartBehavior.start,
  this.enableInteractiveSelection,
  this.onTap,
  this.buildCounter,
  this.scrollPhysics,
}) : assert(textAlign != null),
     assert(autofocus != null),
     assert(obscureText != null),
     assert(autocorrect != null),
     assert(maxLengthEnforced != null),
     assert(scrollPadding != null),
     assert(dragStartBehavior != null),
     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 != null),
     assert(
       !expands || (maxLines == null && minLines == null),
       'minLines and maxLines must be null when expands is true.',
     ),
     assert(maxLength == null || maxLength == TextField.noMaxLength || maxLength > 0),
     keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),
     super(key: key);