CustomTextfield class
A customizable text field widget that provides advanced styling and behavior options.
The CustomTextfield widget wraps a TextField with extended customization such as:
- Custom Sizing: Supports fixed pixel dimensions or screen-relative sizes.
- Custom Styling: Allows control over borders, background color, text styles, etc.
- Icon Support: Supports optional prefix and suffix icons. The suffix icon can be conditionally displayed based on the focus and text input state.
- Automatic Resource Management: Automatically creates and disposes of a TextEditingController and a FocusNode if they are not provided.
- Event Callbacks: Provides callbacks for tap, tap outside, on changed, on submitted, and on editing complete events.
- Advanced Input Control: Supports input formatters, maximum length, cursor customization, and more.
Example Usage
CustomTextfield(
hint: 'Enter your name',
label: 'Name',
pixelHeight: 48,
pixelWidth: 300,
backgroundColor: Colors.white,
borderRadius: 12,
suffixIcon: Icon(Icons.clear),
onchanged: (text) => print("Current input: $text"),
)
If you need to listen to changes in the suffix icon visibility, consider using a
ValueNotifier and a ValueListenableBuilder as demonstrated in the implementation.
Note: When using this widget in a stateful context, the widget manages its internal state (such as the suffix icon visibility) and rebuilds appropriately when those values change. A StatefulWidget that wraps a TextField with extended customization options.
This widget automatically manages its own TextEditingController and FocusNode if they are not provided via constructor parameters. It also handles conditional display of the suffix icon based on whether the text field is focused and non-empty, unless overridden by the alwaysShowSuffixIcon parameter.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- CustomTextfield
Constructors
-
CustomTextfield({Key? key, String? hint, String? label, bool alwaysShowSuffixIcon = false, String defaultText = "", void ontap()?, void onTapOutside()?, dynamic onchanged(String text)?, dynamic onSubmitted(String text)?, void onEditingComplete()?, TextInputType? keyboardType, Widget? suffixIcon, Widget? prefixIcon, double? pixelHeight, double? pixelWidth, bool obscureText = false, TextStyle? hintStyle, TextStyle? labelStyle, TextStyle? inputTextStyle, double borderRadius = 8, Color? backgroundColor, InputBorder? border, InputBorder? disabledBorder, InputBorder? enabledBorder, InputBorder? focusedBorder, TextEditingController? controller, TextAlign textAlign = TextAlign.start, EdgeInsets inputContentPadding = EdgeInsets.zero, FocusNode? focusNode, int? minLines = 1, int? maxLines, bool? isEnabled = true, Color cursorColor = Colors.white, int? maxLength, List<
TextInputFormatter> ? inputFormatters, dynamic internalArgs(TextEditingController controller, FocusNode focusNode)?, bool? isDense, BoxConstraints? constraints, double? cursorHeight, double? cursorWidth, bool? cursorOpacityAnimates, bool? showCursor, Color? selectionColor, Color? selectionHandleColor, bool? readOnly, UndoHistoryController? undoController, TextInputAction? textInputAction, TextCapitalization textCapitalization = TextCapitalization.none, TextAlignVertical? textAlignVertical, StrutStyle? strutStyle, bool autocorrect = true, bool autofocus = false, Iterable<String> ? autofillHints, bool canRequestFocus = true, Color? cursorErrorColor, Widget contextMenuBuilder(BuildContext, EditableTextState)?, DragStartBehavior dragStartBehavior = DragStartBehavior.start, bool enableIMEPersonalizedLearning = true, bool? enableInteractiveSelection, bool enableSuggestions = true, bool expands = false, Object? groupId, bool? ignorePointers, Brightness? keyboardAppearance, TextMagnifierConfiguration? magnifierConfiguration, MaxLengthEnforcement? maxLengthEnforcement, MouseCursor? mouseCursor, String? obscuringCharacter, ScrollController? scrollController, EdgeInsets? scrollPadding, ScrollPhysics? scrollPhysics, TextSelectionControls? selectionControls, BoxHeightStyle? selectionHeightStyle, BoxWidthStyle? selectionWidthStyle, SmartDashesType? smartDashesType, SmartQuotesType? smartQuotesType, SpellCheckConfiguration? spellCheckConfiguration, WidgetStatesController? statesController, TextDirection? textDirection, InputDecoration? inputDecoration, InputBorder? errorBorder, bool? alignLabelWithHint, String? counterText = "", bool autoDispose = true}) -
const
Properties
- alignLabelWithHint → bool?
-
final
- alwaysShowSuffixIcon → bool
-
If true, always display the suffix icon regardless of focus or text content.
final
- autocorrect → bool
-
Enables or disables automatic correction of text input.
final
- autoDispose → bool
-
Whether to Automatically dispose the TextEditingController and FocusNode internally
Only works for the TextEditingController or FocusNode you assign to CustomTextfield
final
-
autofillHints
→ Iterable<
String> ? -
Provides autofill hints for password managers and autofill services.
final
- autofocus → bool
-
Determines whether the text field should automatically gain focus.
final
- backgroundColor → Color?
-
The background color of the text field.
final
- border → InputBorder?
-
The border to use for the text field.
final
- borderRadius → double
-
The border radius for the text field.
final
- canRequestFocus → bool
-
Specifies whether the text field can request focus.
final
- constraints → BoxConstraints?
-
Additional constraints for the text field.
final
- contextMenuBuilder → Widget Function(BuildContext, EditableTextState)?
-
A builder function to customize the context menu.
final
- controller → TextEditingController?
-
An external TextEditingController. If null, one is created automatically.
It is automatically disposed, so you don't need to dispose.
final
- counterText → String?
-
final
- cursorColor → Color
-
The color of the cursor.
final
- cursorErrorColor → Color?
-
The color used to indicate an error in the text cursor.
final
- cursorHeight → double?
-
The height of the cursor.
final
- cursorOpacityAnimates → bool?
-
Whether the cursor opacity should animate.
final
- cursorWidth → double?
-
The width of the cursor.
final
- defaultText → String
-
The default text to prepopulate the text field.
final
- disabledBorder → InputBorder?
-
The border to use when the text field is disabled.
final
- dragStartBehavior → DragStartBehavior
-
Defines the behavior when a drag gesture starts.
final
- enabledBorder → InputBorder?
-
The border to use when the text field is enabled.
final
- enableIMEPersonalizedLearning → bool
-
Enables or disables personalized learning for the keyboard input.
final
- enableInteractiveSelection → bool?
-
Determines whether interactive selection of text is enabled.
final
- enableSuggestions → bool
-
Suggests text completions while typing.
final
- errorBorder → InputBorder?
-
The border to use when the text field contains error
final
- expands → bool
-
Whether the text field should expand to fill available space.
final
- focusedBorder → InputBorder?
-
The border to use when the text field is focused.
final
- focusNode → FocusNode?
-
An external FocusNode. If null, one is created automatically.
It is automatically disposed, so you don't need to dispose it
final
- groupId → Object?
-
An optional group identifier for text input components.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- hint → String?
-
The hint text to display when the field is empty.
final
- hintStyle → TextStyle?
-
The text style for the hint.
final
- ignorePointers → bool?
-
Determines whether the text field should ignore pointer interactions.
final
- inputContentPadding → EdgeInsets
-
Padding for the input content.
final
- inputDecoration → InputDecoration?
-
The TextField's input decoration, this overrides other properties in input decoration
final
-
inputFormatters
→ List<
TextInputFormatter> ? -
Optional input formatters.
final
- inputTextStyle → TextStyle?
-
The text style for the input text.
final
- internalArgs → dynamic Function(TextEditingController controller, FocusNode focusNode)?
-
Callback to pass internal arguments (controller and focus node) for custom usage.
final
- isDense → bool?
-
Whether the text field should have a dense layout.
final
- isEnabled → bool?
-
Whether the text field is enabled.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- keyboardAppearance → Brightness?
-
The brightness of the keyboard when it appears.
final
- keyboardType → TextInputType?
-
The keyboard type to use for input.
final
- label → String?
-
The label to display above the text field.
final
- labelStyle → TextStyle?
-
The text style for the label.
final
- magnifierConfiguration → TextMagnifierConfiguration?
-
Configures the magnifier that appears when selecting text.
final
- maxLength → int?
-
The maximum length of input.
final
- maxLengthEnforcement → MaxLengthEnforcement?
-
Specifies how the maximum text length should be enforced.
final
- maxLines → int?
-
The maximum number of lines for the text field.
final
- minLines → int?
-
The minimun number of lines for the text field.
final
- mouseCursor → MouseCursor?
-
Defines the mouse cursor to be displayed when hovering over the field.
final
- obscureText → bool
-
Whether the text field should obscure the text (e.g., for passwords).
final
- obscuringCharacter → String?
-
The character used to obscure text when the input is hidden.
final
- onchanged → dynamic Function(String text)?
-
Callback when the text changes.
final
- onEditingComplete → void Function()?
-
Callback when editing is complete.
final
- onSubmitted → dynamic Function(String text)?
-
Callback when the text is submitted.
final
- ontap → void Function()?
-
Callback when the text field is tapped.
final
- onTapOutside → void Function()?
-
Callback when a tap outside the text field is detected. Adjust this to prevent keyboard from automatically unfocusing
final
- pixelHeight → double?
-
The fixed pixel height of the text field.
final
- pixelWidth → double?
-
The fixed pixel width of the text field.
final
- prefixIcon → Widget?
-
A widget to display as the prefix icon.
final
- readOnly → bool?
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scrollController → ScrollController?
-
A controller to manage scrolling within the text field.
final
- scrollPadding → EdgeInsets?
-
The padding around the text field when scrolling.
final
- scrollPhysics → ScrollPhysics?
-
Defines the physics for scrolling behavior.
final
- selectionColor → Color?
-
The color of the text selection.
final
- selectionControls → TextSelectionControls?
-
Controls how text selection is handled.
final
- selectionHandleColor → Color?
-
The color of the selection handles.
final
- selectionHeightStyle → BoxHeightStyle?
-
Determines the height style of the text selection highlight.
final
- selectionWidthStyle → BoxWidthStyle?
-
Determines the width style of the text selection highlight.
final
- showCursor → bool?
-
Whether to always show the cursor.
final
- smartDashesType → SmartDashesType?
-
Configures automatic smart dashes replacement.
final
- smartQuotesType → SmartQuotesType?
-
Configures automatic smart quotes replacement.
final
- spellCheckConfiguration → SpellCheckConfiguration?
-
Configures spell check behavior for the text input.
final
- statesController → WidgetStatesController?
-
Controls the visual states of the text input field.
final
- strutStyle → StrutStyle?
-
Defines strut style, which controls line height and spacing.
final
- suffixIcon → Widget?
-
A widget to display as the suffix icon.
final
- textAlign → TextAlign
-
How the text should be aligned.
final
- textAlignVertical → TextAlignVertical?
-
Aligns text vertically within the input field.
final
- textCapitalization → TextCapitalization
-
Configures how text capitalization is applied to the input.
final
- textDirection → TextDirection?
-
Defines the text direction (LTR or RTL).
final
- textInputAction → TextInputAction?
-
Specifies the action button on the keyboard (e.g., done, next, search).
final
- undoController → UndoHistoryController?
-
Controls the undo and redo functionality for the text input.
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< CustomTextfield> -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited