OTPTextField class
A customizable OTP (One-Time Password) text field widget.
This widget creates multiple text fields for entering OTP codes with various styling options, automatic focus management, and keyboard navigation.
Basic Usage
final controllers = List.generate(6, (_) => TextEditingController());
final focusNodes = List.generate(6, (_) => FocusNode());
OTPTextField(
controllers: controllers,
focusNodes: focusNodes,
textStyle: TextStyle(fontSize: 24),
onCompleted: (otp) => print('OTP: $otp'),
)
Styling Options
The widget supports multiple visual styles through fieldStyle:
- OTPFieldStyle.box - Box-shaped fields (default)
- OTPFieldStyle.underline - Underlined fields
- OTPFieldStyle.circle - Circular fields
Input Types
Control what characters are allowed via inputType:
- OTPInputType.number - Only numeric digits (default)
- OTPInputType.text - Only alphabetic characters
- OTPInputType.any - Both numbers and letters
Pasting
An edit that inserts more than one character into a field is a paste. Only the focused field accepts one, and only the characters inputType allows are kept. When exactly one per field remains, every field is filled from the first, whichever field received the paste; onChange and then onCompleted are called once with the full code, and the receiving field loses focus before onCompleted. Otherwise no field changes and no callback is called. A code the platform autofills into a field (see autofillHints) is handled the same way.
Theming
Customize appearance using theme or let it automatically adapt to the current ThemeData.brightness:
OTPTextField(
theme: OTPTheme.custom(
borderColor: Colors.blue,
focusedBorderColor: Colors.blueAccent,
),
)
See also:
- OTPTheme for detailed theming options
- OTPFieldStyle for available visual styles
- OTPInputType for input type options
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- OTPTextField
Constructors
-
OTPTextField({Key? key, required List<
TextEditingController> controllers, required List<FocusNode> focusNodes, required TextStyle textStyle, ValueChanged<String> ? onCompleted, ValueChanged<String> ? onChange, double fieldWidth = 40, double fieldHeight = 50, double fieldSpacing = 8, double? centerSpacing, OTPTheme? theme, OTPFieldStyle fieldStyle = OTPFieldStyle.box, OTPInputType inputType = OTPInputType.number, bool obscureText = false, String obscuringCharacter = '●', bool enabled = true, bool autoFocus = false, bool hasError = false, bool? showCursor, Color? cursorColor, double? cursorHeight, double? cursorWidth, Color? selectionColor, Color? selectionHandleColor, TextCapitalization textCapitalization = TextCapitalization.none, TextInputType? keyboardType, Iterable<String> ? autofillHints = const [AutofillHints.oneTimeCode]}) -
const
Properties
-
autofillHints
→ Iterable<
String> ? -
The autofill hints every field sends to the platform.
final
- autoFocus → bool
-
Whether to automatically focus the first field when the widget is built.
final
- centerSpacing → double?
-
Extra spacing applied at the middle of the OTP fields.
final
-
controllers
→ List<
TextEditingController> -
The list of text controllers for each OTP field.
final
- cursorColor → Color?
-
Overrides the cursor color.
final
- cursorHeight → double?
-
Overrides the cursor height in logical pixels.
final
- cursorWidth → double?
-
Overrides the cursor width in logical pixels.
final
- enabled → bool
-
Whether the text fields are enabled for input.
final
- fieldHeight → double
-
The height of each OTP field.
final
- fieldSpacing → double
-
The spacing between adjacent OTP fields.
final
- fieldStyle → OTPFieldStyle
-
The visual style of the OTP fields.
final
- fieldWidth → double
-
The width of each OTP field.
final
-
focusNodes
→ List<
FocusNode> -
The list of focus nodes for each OTP field.
final
- hasError → bool
-
Whether to display the error state styling.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- inputType → OTPInputType
-
The type of input allowed in the OTP fields.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- keyboardType → TextInputType?
-
Overrides the keyboard type to show.
final
- obscureText → bool
-
Whether to obscure the text being entered.
final
- obscuringCharacter → String
-
The character used to obscure text when obscureText is true.
final
-
onChange
→ ValueChanged<
String> ? -
Callback invoked whenever any field value changes.
final
-
onCompleted
→ ValueChanged<
String> ? -
Callback invoked when all OTP fields are filled.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- selectionColor → Color?
-
Overrides the text selection color (highlight color when text is selected).
final
- selectionHandleColor → Color?
-
Overrides the text selection handle color.
final
- showCursor → bool?
-
Overrides the cursor visibility.
final
- textCapitalization → TextCapitalization
-
Configures how the text input should be capitalized.
final
- textStyle → TextStyle
-
The text style applied to the input text.
final
- theme → OTPTheme?
-
The theme configuration for styling the OTP fields.
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< OTPTextField> -
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