CupertinoTextField constructor Null safety

const CupertinoTextField(
  1. {Key? key,
  2. TextEditingController? controller,
  3. FocusNode? focusNode,
  4. BoxDecoration? decoration = _kDefaultRoundedBorderDecoration,
  5. EdgeInsetsGeometry padding = const EdgeInsets.all(6.0),
  6. String? placeholder,
  7. TextStyle? placeholderStyle = const TextStyle(fontWeight: FontWeight.w400, color: CupertinoColors.placeholderText),
  8. Widget? prefix,
  9. OverlayVisibilityMode prefixMode = OverlayVisibilityMode.always,
  10. Widget? suffix,
  11. OverlayVisibilityMode suffixMode = OverlayVisibilityMode.always,
  12. OverlayVisibilityMode clearButtonMode = OverlayVisibilityMode.never,
  13. TextInputType? keyboardType,
  14. TextInputAction? textInputAction,
  15. TextCapitalization textCapitalization = TextCapitalization.none,
  16. TextStyle? style,
  17. StrutStyle? strutStyle,
  18. TextAlign textAlign = TextAlign.start,
  19. TextAlignVertical? textAlignVertical,
  20. TextDirection? textDirection,
  21. bool readOnly = false,
  22. ToolbarOptions? toolbarOptions,
  23. bool? showCursor,
  24. bool autofocus = false,
  25. String obscuringCharacter = '•',
  26. bool obscureText = false,
  27. bool autocorrect = true,
  28. SmartDashesType? smartDashesType,
  29. SmartQuotesType? smartQuotesType,
  30. bool enableSuggestions = true,
  31. int? maxLines = 1,
  32. int? minLines,
  33. bool expands = false,
  34. int? maxLength,
  35. MaxLengthEnforcement? maxLengthEnforcement,
  36. ValueChanged<String>? onChanged,
  37. VoidCallback? onEditingComplete,
  38. ValueChanged<String>? onSubmitted,
  39. List<TextInputFormatter>? inputFormatters,
  40. bool? enabled,
  41. double cursorWidth = 2.0,
  42. double? cursorHeight,
  43. Radius cursorRadius = const Radius.circular(2.0),
  44. Color? cursorColor,
  45. BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.tight,
  46. BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight,
  47. Brightness? keyboardAppearance,
  48. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  49. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  50. bool? enableInteractiveSelection,
  51. TextSelectionControls? selectionControls,
  52. GestureTapCallback? onTap,
  53. ScrollController? scrollController,
  54. ScrollPhysics? scrollPhysics,
  55. Iterable<String>? autofillHints = const <String>[],
  56. Clip clipBehavior = Clip.hardEdge,
  57. String? restorationId,
  58. bool scribbleEnabled = true,
  59. bool enableIMEPersonalizedLearning = true}
)

Creates an iOS-style text field.

To provide a prefilled text entry, pass in a TextEditingController with an initial value to the controller parameter.

To provide a hint placeholder text that appears when the text entry is empty, pass a String to the placeholder parameter.

The maxLines property can be set to null to remove the restriction on the number of lines. In this mode, the intrinsic height of the widget will grow as the number of lines of text grows. By default, it is 1, meaning this is a single-line text field and will scroll horizontally when overflown. maxLines must not be zero.

The text cursor is not shown if showCursor is false or if showCursor is null (the default) and readOnly is true.

If specified, the maxLength property must be greater than zero.

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 autocorrect, autofocus, clearButtonMode, dragStartBehavior, expands, obscureText, prefixMode, readOnly, scrollPadding, suffixMode, textAlign, selectionHeightStyle, selectionWidthStyle, enableSuggestions, and enableIMEPersonalizedLearning properties must not be null.

See also:

  • minLines, which is the minimum number of lines to occupy when the content spans fewer lines.
  • expands, to allow the widget to size itself to its parent's height.
  • maxLength, which discusses the precise meaning of "number of characters" and how it may differ from the intuitive meaning.

Implementation

const CupertinoTextField({
  Key? key,
  this.controller,
  this.focusNode,
  this.decoration = _kDefaultRoundedBorderDecoration,
  this.padding = const EdgeInsets.all(6.0),
  this.placeholder,
  this.placeholderStyle = const TextStyle(
    fontWeight: FontWeight.w400,
    color: CupertinoColors.placeholderText,
  ),
  this.prefix,
  this.prefixMode = OverlayVisibilityMode.always,
  this.suffix,
  this.suffixMode = OverlayVisibilityMode.always,
  this.clearButtonMode = OverlayVisibilityMode.never,
  TextInputType? keyboardType,
  this.textInputAction,
  this.textCapitalization = TextCapitalization.none,
  this.style,
  this.strutStyle,
  this.textAlign = TextAlign.start,
  this.textAlignVertical,
  this.textDirection,
  this.readOnly = false,
  ToolbarOptions? toolbarOptions,
  this.showCursor,
  this.autofocus = false,
  this.obscuringCharacter = '•',
  this.obscureText = false,
  this.autocorrect = true,
  SmartDashesType? smartDashesType,
  SmartQuotesType? smartQuotesType,
  this.enableSuggestions = true,
  this.maxLines = 1,
  this.minLines,
  this.expands = false,
  this.maxLength,
  this.maxLengthEnforcement,
  this.onChanged,
  this.onEditingComplete,
  this.onSubmitted,
  this.inputFormatters,
  this.enabled,
  this.cursorWidth = 2.0,
  this.cursorHeight,
  this.cursorRadius = const Radius.circular(2.0),
  this.cursorColor,
  this.selectionHeightStyle = ui.BoxHeightStyle.tight,
  this.selectionWidthStyle = ui.BoxWidthStyle.tight,
  this.keyboardAppearance,
  this.scrollPadding = const EdgeInsets.all(20.0),
  this.dragStartBehavior = DragStartBehavior.start,
  bool? enableInteractiveSelection,
  this.selectionControls,
  this.onTap,
  this.scrollController,
  this.scrollPhysics,
  this.autofillHints = const <String>[],
  this.clipBehavior = Clip.hardEdge,
  this.restorationId,
  this.scribbleEnabled = true,
  this.enableIMEPersonalizedLearning = 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(scrollPadding != null),
     assert(dragStartBehavior != null),
     assert(selectionHeightStyle != null),
     assert(selectionWidthStyle != 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(clearButtonMode != null),
     assert(prefixMode != null),
     assert(suffixMode != null),
     // 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.',
     ),
     assert(enableIMEPersonalizedLearning != null),
     keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),
     enableInteractiveSelection = enableInteractiveSelection ?? (!readOnly || !obscureText),
     toolbarOptions = toolbarOptions ??
         (obscureText
             ? (readOnly
                 // No point in even offering "Select All" in a read-only obscured
                 // field.
                 ? const ToolbarOptions()
                 // Writable, but obscured.
                 : const ToolbarOptions(
                     selectAll: true,
                     paste: true,
                   ))
             : (readOnly
                 // Read-only, not obscured.
                 ? const ToolbarOptions(
                     selectAll: true,
                     copy: true,
                   )
                 // Writable, not obscured.
                 : const ToolbarOptions(
                     copy: true,
                     cut: true,
                     selectAll: true,
                     paste: true,
                   ))),
     super(key: key);