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. ToolbarOptions toolbarOptions,
  17. bool showCursor,
  18. bool obscureText: false,
  19. bool autocorrect: true,
  20. SmartDashesType smartDashesType,
  21. SmartQuotesType smartQuotesType,
  22. bool enableSuggestions: true,
  23. bool autovalidate: false,
  24. bool maxLengthEnforced: true,
  25. int maxLines: 1,
  26. int minLines,
  27. bool expands: false,
  28. int maxLength,
  29. ValueChanged<String> onChanged,
  30. GestureTapCallback onTap,
  31. VoidCallback onEditingComplete,
  32. ValueChanged<String> onFieldSubmitted,
  33. FormFieldSetter<String> onSaved,
  34. FormFieldValidator<String> validator,
  35. List<TextInputFormatter> inputFormatters,
  36. bool enabled: true,
  37. double cursorWidth: 2.0,
  38. Radius cursorRadius,
  39. Color cursorColor,
  40. Brightness keyboardAppearance,
  41. EdgeInsets scrollPadding: const EdgeInsets.all(20.0),
  42. bool enableInteractiveSelection: true,
  43. InputCounterWidgetBuilder buildCounter,
  44. ScrollPhysics scrollPhysics}
)

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 new TextField, the constructor.

Implementation

TextFormField({
  Key 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,
  ToolbarOptions toolbarOptions,
  bool showCursor,
  bool obscureText = false,
  bool autocorrect = true,
  SmartDashesType smartDashesType,
  SmartQuotesType smartQuotesType,
  bool enableSuggestions = true,
  bool autovalidate = false,
  bool maxLengthEnforced = true,
  int maxLines = 1,
  int minLines,
  bool expands = false,
  int maxLength,
  ValueChanged<String> onChanged,
  GestureTapCallback onTap,
  VoidCallback onEditingComplete,
  ValueChanged<String> onFieldSubmitted,
  FormFieldSetter<String> onSaved,
  FormFieldValidator<String> validator,
  List<TextInputFormatter> inputFormatters,
  bool enabled = true,
  double cursorWidth = 2.0,
  Radius cursorRadius,
  Color cursorColor,
  Brightness keyboardAppearance,
  EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  bool enableInteractiveSelection = true,
  InputCounterWidgetBuilder buildCounter,
  ScrollPhysics scrollPhysics,
}) : assert(initialValue == null || controller == null),
     assert(textAlign != null),
     assert(autofocus != null),
     assert(readOnly != null),
     assert(obscureText != null),
     assert(autocorrect != null),
     assert(enableSuggestions != null),
     assert(autovalidate != null),
     assert(maxLengthEnforced != null),
     assert(scrollPadding != 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(!obscureText || maxLines == 1, 'Obscured fields cannot be multiline.'),
     assert(maxLength == null || maxLength > 0),
     assert(enableInteractiveSelection != null),
     super(
  key: key,
  initialValue: controller != null ? controller.text : (initialValue ?? ''),
  onSaved: onSaved,
  validator: validator,
  autovalidate: autovalidate,
  enabled: enabled,
  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) {
      if (onChanged != null) {
        onChanged(value);
      }
      field.didChange(value);
    }
    return TextField(
      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,
      toolbarOptions: toolbarOptions,
      readOnly: readOnly,
      showCursor: showCursor,
      obscureText: obscureText,
      autocorrect: autocorrect,
      smartDashesType: smartDashesType ?? (obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
      smartQuotesType: smartQuotesType ?? (obscureText ? SmartQuotesType.disabled : SmartQuotesType.enabled),
      enableSuggestions: enableSuggestions,
      maxLengthEnforced: maxLengthEnforced,
      maxLines: maxLines,
      minLines: minLines,
      expands: expands,
      maxLength: maxLength,
      onChanged: onChangedHandler,
      onTap: onTap,
      onEditingComplete: onEditingComplete,
      onSubmitted: onFieldSubmitted,
      inputFormatters: inputFormatters,
      enabled: enabled,
      cursorWidth: cursorWidth,
      cursorRadius: cursorRadius,
      cursorColor: cursorColor,
      scrollPadding: scrollPadding,
      scrollPhysics: scrollPhysics,
      keyboardAppearance: keyboardAppearance,
      enableInteractiveSelection: enableInteractiveSelection,
      buildCounter: buildCounter,
    );
  },
);