Ask the assistant
0/400
Your question is processed by an AI service and kept to improve answers. Do not enter personal data.

    TextField

    A plain text input field not tied to a form. When you need value validation and form submission, use TextFormField ; for search, chat and any field whose value lives in a variable, TextField is simpler.

    Type: multi-child (children are only decoration slot elements, see below)

    TextField
      #decoration labelText:"Email" hintText:"you@example.com"

    Parameters

    Parameter Type Default Description
    value variable — Two-way binding: value:$query — input writes to the variable, and a change of the variable rewrites the text
    initialValue string — Initial text without a binding
    onChanged callback — Called on every change; the argument value is the text: (value){…}
    onSubmitted callback — Called when input is finished (Enter, the keyboard's action key); the argument value is the text
    obscureText bool false Hide the input (password)
    obscuringCharacter string • Masking character
    maxLines int 1 Maximum number of lines; more than 1 makes a multiline field
    minLines int — How many lines are visible at once
    expands bool false Stretch the field over all of the parent's space. Works only on the site: in the app expands:true does not work for now — the field fails with an error
    maxLength int — Maximum number of characters
    readOnly bool false Read only
    enabled bool — false — the field is disabled
    autofocus bool false Focus right away when shown
    autocorrect bool true Typo correction
    enableSuggestions bool true Input suggestions
    textAlign string start Text alignment
    textCapitalization string none none , words , sentences , characters
    textInputAction string — Which key replaces Enter on a phone: next , done , go , send , search
    cursorColor color — Cursor color
    submitOnEnter bool false A multiline field submits on Enter, and Shift+Enter inserts a line break (app only)
    canRequestFocus bool true Whether the field can take focus
    The hint in an empty field and the label are set in #decoration ( hintText: , labelText: ), not as parameters of the field itself.

    Fine tuning

    Parameters of the Flutter field that are rarely needed. The "Site" column says whether the parameter works on the site; the rest apply only in the app.

    Parameter Type Default Description Site
    textDirection string ltr ltr or rtl — text direction yes ( dir )
    textAlignVertical string center Vertical alignment of the text in the field: top , center , bottom no
    showCursor bool — false — the cursor is not drawn yes (transparent caret)
    smartDashesType string enabled The system keyboard replacing -- with a dash: enabled / disabled no
    smartQuotesType string enabled Replacing straight quotes with typographic ones: enabled / disabled no
    maxLengthEnforcement string none How to enforce maxLength : enforced , truncateAfterCompositionEnds , none no
    onTapAlwaysCalled bool false Call the tap handler also on a repeated tap on a focused field no
    ignorePointers bool — true — the field ignores touches and the mouse no
    cursorWidth double 2.0 Cursor thickness no
    cursorHeight double — Cursor height (the line height by default) no
    cursorRadius #cursorRadius — Cursor rounding: #cursorRadius circular 2 or elliptical x:… y:… no
    keyboardAppearance string light Theme of the iOS system keyboard: light / dark no
    scrollPadding #scrollPadding all 20 Space the scroll view leaves around the field when it shows it above the keyboard (like #padding ) no
    enableInteractiveSelection bool — false — the text cannot be selected and copied yes ( user-select:none )
    selectionHeightStyle string — Height of the selection highlight: tight , max , includeLineSpacingMiddle , includeLineSpacingTop , includeLineSpacingBottom , strut no
    selectionWidthStyle string — Width of the selection highlight: tight , max no
    restorationId string — Key for restoring the field's state after the system restarts the app no
    enableIMEPersonalizedLearning bool true false — asks the keyboard not to learn from the input (Android incognito mode) no
    mouseCursor string — Mouse cursor over the field: text , basic , click , forbidden … no
    cursorOpacityAnimates bool — Smooth cursor blinking instead of abrupt no
    dragStartBehavior string start When dragging the selection starts: start or down no
    clipBehavior string hardEdge Clipping the field's content: none , hardEdge , antiAlias , antiAliasWithSaveLayer no
    stylusHandwritingEnabled bool system default Stylus handwriting input (Android, iPadOS) no
    hintLocales #hintLocales — Languages the keyboard offers first: #hintLocales ru:RU en:US no
    strutStyle #strutStyle — Minimum line height from a text style: #strutStyle fromTextStyle with a #textStyle inside no
    @note:""
    TextField value:$note maxLines:3 textAlignVertical:top cursorWidth:3
      #cursorRadius circular 2
      #hintLocales ru:RU en:US

    Nested structures

    #decoration

    Field decoration: hint, label, helper text below the field, error, icons, border. Same format as for TextFormField .

    TextField
      #decoration labelText:"Name" hintText:"What is your name"
        #enabledBorder outline
          #borderRadius circular 8
          #borderSide color:#585593 width:1
        #focusedBorder outline
          #borderRadius circular 8
          #borderSide color:#6C63FF width:2

    #style

    Style of the entered text — size, color, weight (as for Text ).

    TextField
      #style fontSize:18 color:#FFFFFF
      #decoration hintText:"Enter text"

    #keyboardType

    Which keyboard to show: text , multiline , number , phone , datetime , emailAddress , url , visiblePassword , name , streetAddress , webSearch .

    TextField
      #keyboardType emailAddress
      #decoration labelText:"Email"

    Examples

    A search field with clearing

    value:$query keeps the field and the variable in agreement both ways, so a button can clear the text just by writing an empty string:

    @query:""
    Row spacing:8
      Expanded
        TextField value:$query
          #decoration hintText:"Search…"
      InkWell onTap:{query = ""}
        Icon close

    Reacting to every change

    @echo:""
    Column spacing:8
      TextField onChanged:(value){echo = value}
        #decoration labelText:"Name"
      Text $"You typed: ${echo}"

    Sign-in: login and password

    On a phone without textInputAction there is no way to move from the login to the password:

    @email:""
    @password:""
    Column spacing:12
      TextField value:$email textInputAction:next
        #keyboardType emailAddress
        #decoration labelText:"Email"
      TextField value:$password obscureText:true textInputAction:done
        #decoration labelText:"Password"

    A multiline comment

    @comment:""
    TextField value:$comment maxLines:6 minLines:3
      #decoration labelText:"Comment" hintText:"Enter text…"

    Differences between the app and the site

    On the site the field is a plain <input> (a <textarea> when maxLines is greater than 1).

    • Working: value:$variable , initialValue , #decoration (hint, label, border), onChanged , onSubmitted (on Enter), obscureText , maxLines / minLines , maxLength , readOnly , enabled , autofocus , textCapitalization , textInputAction , #keyboardType , #autofillHints , #style , textAlign , cursorColor .
    • Not working on the site: submitOnEnter , #keys , and also id , token , tokenStart , focused (parameters of a field with suggestions while typing). A multiline field on the site does not submit on Enter.
    • The labelText label moves up above the field while typing, as in the app.

    Bad practices

    A hint as a field parameter

    // BAD — hintText is not a TextField parameter: the hint will not appear
    TextField hintText:"Enter your name"
    // GOOD — the hint goes in #decoration
    TextField
      #decoration hintText:"Enter your name"

    Best practices

    • Value into a variable, not into a handler. value:$query is shorter and more reliable than onChanged:(value){query = value} : it also clears the field when the variable changes.
    • Two or more fields in a row — always with textInputAction ( next , and done for the last one).
    • Value validation rules (required, e-mail, minimum length) are not on TextField but on TextFormField in #validators .

    See also

    • TextFormField — a field with validation and form submission
    • Text — displaying text
    • Button — the submit button