TextFormField constructor

TextFormField({
  1. Key? key,
  2. TextEditingController? controller,
  3. String? initialValue,
  4. TextInputType? keyboardType,
  5. TextCapitalization textCapitalization = TextCapitalization.none,
  6. TextInputAction? textInputAction,
  7. TextStyle? style,
  8. StrutStyle? strutStyle,
  9. TextDirection? textDirection,
  10. TextAlign textAlign = TextAlign.start,
  11. bool autofocus = false,
  12. bool readOnly = false,
  13. bool? showCursor,
  14. String obscuringCharacter = '•',
  15. bool obscureText = false,
  16. bool autocorrect = true,
  17. SmartDashesType? smartDashesType,
  18. SmartQuotesType? smartQuotesType,
  19. bool enableSuggestions = true,
  20. MaxLengthEnforcement? maxLengthEnforcement,
  21. int? maxLines = 1,
  22. int? minLines,
  23. bool expands = false,
  24. int? maxLength,
  25. ValueChanged<String>? onChanged,
  26. GestureTapCallback? onTap,
  27. VoidCallback? onEditingComplete,
  28. ValueChanged<String>? onFieldSubmitted,
  29. FormFieldSetter<String>? onSaved,
  30. FormFieldValidator<String>? validator,
  31. List<TextInputFormatter>? inputFormatters,
  32. bool enabled = true,
  33. double cursorWidth = 2.0,
  34. double? cursorHeight,
  35. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  36. bool? enableInteractiveSelection,
  37. TextSelectionControls? selectionControls,
  38. Iterable<String>? autofillHints,
  39. AutovalidateMode? autovalidateMode,
  40. ScrollController? scrollController,
  41. String? restorationId,
  42. bool enableIMEPersonalizedLearning = true,
  43. String? prefix,
  44. FocusNode? focusNode,
})

Creates a TextFormField.

Implementation

TextFormField({
  super.key,
  this.controller,
  String? initialValue,
  TextInputType? keyboardType,
  TextCapitalization textCapitalization = TextCapitalization.none,
  TextInputAction? textInputAction,
  TextStyle? style,
  StrutStyle? strutStyle,
  TextDirection? textDirection,
  TextAlign textAlign = TextAlign.start,
  bool autofocus = false,
  bool readOnly = false,
  bool? showCursor,
  String obscuringCharacter = '•',
  bool obscureText = false,
  bool autocorrect = true,
  SmartDashesType? smartDashesType,
  SmartQuotesType? smartQuotesType,
  bool enableSuggestions = true,
  MaxLengthEnforcement? maxLengthEnforcement,
  int? maxLines = 1,
  int? minLines,
  bool expands = false,
  int? maxLength,
  ValueChanged<String>? onChanged,
  GestureTapCallback? onTap,
  VoidCallback? onEditingComplete,
  ValueChanged<String>? onFieldSubmitted,
  super.onSaved,
  super.validator,
  List<TextInputFormatter>? inputFormatters,
  bool enabled = true,
  double cursorWidth = 2.0,
  double? cursorHeight,
  EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  bool? enableInteractiveSelection,
  TextSelectionControls? selectionControls,
  Iterable<String>? autofillHints,
  AutovalidateMode? autovalidateMode,
  ScrollController? scrollController,
  super.restorationId,
  bool enableIMEPersonalizedLearning = true,
  this.prefix,
  this.focusNode,
  // MouseCursor? mouseCursor,
})  : assert(initialValue == null || controller == null),
      assert(obscuringCharacter.length == 1),
      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 > 0),
      super(
        initialValue:
            controller != null ? controller.text : (initialValue ?? ''),
        enabled: enabled,
        autovalidateMode: autovalidateMode ?? AutovalidateMode.disabled,
        builder: (FormFieldState<String> field) {
          final _TextFormFieldState state = field as _TextFormFieldState;

          void onChangedHandler(String value) {
            field.didChange(value);
            if (onChanged != null) {
              onChanged(value);
            }
          }

          final FocusNode effectiveFocusNode = state._effectiveFocusNode;

          final ThemeData themeData = Theme.of(field.context);
          final ColorScheme colorScheme = themeData.colorScheme;
          final TextTheme textTheme = themeData.textTheme;

          final Widget? error = field.errorText != null
              ? Text(
                  field.errorText!,
                  style: textTheme.caption.copyWith(
                    color: textTheme.textError,
                    fontWeight: FontWeight.w500,
                  ),
                )
              : null;

          final Color background =
              enabled ? colorScheme.background[0] : colorScheme.shade[90];

          final foreground = effectiveFocusNode.hasFocus
              ? colorScheme.shade[50]
              : colorScheme.shade[30];

          final Color borderColor = foreground;

          final BoxDecoration decoration = BoxDecoration(
            color: background,
            border: enabled
                ? Border.all(color: borderColor, width: _kBorderWidth)
                : null,
          );

          final TextStyle prefixTextStyle = textTheme.caption.copyWith(
            color: foreground,
            fontWeight: FontWeight.w500,
          );

          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              DecoratedBox(
                decoration: decoration,
                child: Padding(
                  padding: decoration.padding,
                  child: Row(
                    children: [
                      if (prefix != null)
                        Padding(
                          // Considering the padding inside [TextField].
                          padding: const EdgeInsets.only(left: 4.0),
                          child: Text(
                            prefix,
                            style: prefixTextStyle,
                          ),
                        ),
                      Flexible(
                        child: Align(
                          alignment: Alignment.centerRight,
                          child: UnmanagedRestorationScope(
                            bucket: field.bucket,
                            child: TextField(
                              restorationId: restorationId,
                              controller: state._effectiveController,
                              focusNode: effectiveFocusNode,
                              decoration: const BoxDecoration(),
                              keyboardType: keyboardType,
                              textInputAction: textInputAction,
                              style: style,
                              strutStyle: strutStyle,
                              textAlign: textAlign,
                              textDirection: textDirection,
                              textCapitalization: textCapitalization,
                              autofocus: autofocus,
                              readOnly: readOnly,
                              showCursor: showCursor,
                              obscuringCharacter: obscuringCharacter,
                              obscureText: obscureText,
                              autocorrect: autocorrect,
                              smartDashesType: smartDashesType ??
                                  (obscureText
                                      ? SmartDashesType.disabled
                                      : SmartDashesType.enabled),
                              smartQuotesType: smartQuotesType ??
                                  (obscureText
                                      ? SmartQuotesType.disabled
                                      : SmartQuotesType.enabled),
                              enableSuggestions: enableSuggestions,
                              maxLines: maxLines,
                              minLines: minLines,
                              expands: expands,
                              maxLength: maxLength,
                              onChanged: onChangedHandler,
                              onTap: onTap,
                              onEditingComplete: onEditingComplete,
                              onSubmitted: onFieldSubmitted,
                              inputFormatters: inputFormatters,
                              enabled: enabled,
                              cursorWidth: cursorWidth,
                              cursorHeight: cursorHeight,
                              scrollPadding: scrollPadding,
                              enableInteractiveSelection:
                                  enableInteractiveSelection ??
                                      (!obscureText || !readOnly),
                              selectionControls: selectionControls,
                              autofillHints: autofillHints,
                              scrollController: scrollController,
                              enableIMEPersonalizedLearning:
                                  enableIMEPersonalizedLearning,
                              maxLengthEnforcement: maxLengthEnforcement,
                              // mouseCursor: mouseCursor,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              if (error != null)
                Padding(
                  padding: const EdgeInsets.only(top: 4.0),
                  child: Align(
                    alignment: Alignment.bottomRight,
                    child: error,
                  ),
                ),
            ],
          );
        },
      );