| Live demo |
Rounded with shadows |
Rounded with cursor |
 |
 |
 |
More styles
| Rounded filled |
Bottom cursor |
Filled |
 |
 |
 |
Features
- Animated decoration switching between pin states
- App-controlled validation feedback, with
Form integration through FormField
- SMS autofill on iOS and Android
- Standard, custom and animated cursor
- Obscuring by character or widget
- Custom keyboard support, paste, haptic feedback
- Closes the keyboard after completion
- Ready-made templates to pick from
Platforms: Android, iOS, web, macOS, Windows and Linux. SMS autofill works out of the box on iOS; on Android the app sets the code on the controller, see SMS autofill.
Install
flutter pub add pinput
import 'package:pinput/pinput.dart';
Usage
Pinput(
onCompleted: (pin) => print(pin),
)
By default Pinput shows 6 pins. Set length for another size, for example Pinput(length: 4).
Use inputMode to change how it accepts input:
PinInputMode |
Behavior |
editable |
Default; the native keyboard edits the PIN |
readOnly |
Focusable and selectable, but can't be edited |
disabled |
Ignores input and uses the disabled pin state |
customKeyboard |
Focusable, but ignores pointer input and never opens the native keyboard. Drive it with your own keys, see Controller and focus |
Theming
A pin has six states: default, focused, filled, empty, disabled and error. Each can have its own theme, and the pin animates between them automatically. Create a base PinTheme, then derive the others from it:
final defaultDecoration = BoxDecoration(
border: Border.all(color: const Color.fromRGBO(234, 239, 243, 1)),
borderRadius: BorderRadius.circular(20),
);
final defaultPinTheme = PinTheme(
width: 56,
height: 56,
textStyle: const TextStyle(fontSize: 20, color: Color.fromRGBO(30, 60, 87, 1), fontWeight: FontWeight.w600),
decoration: defaultDecoration,
);
final focusedPinTheme = defaultPinTheme.copyWith(
decoration: defaultDecoration.copyWith(
border: Border.all(color: const Color.fromRGBO(114, 178, 238, 1)),
borderRadius: BorderRadius.circular(8),
),
);
final filledPinTheme = defaultPinTheme.copyWith(
decoration: defaultDecoration.copyWith(
color: const Color.fromRGBO(234, 239, 243, 1),
),
);
return Pinput(
theme: PinThemes(
fallback: defaultPinTheme,
focused: focusedPinTheme,
filled: filledPinTheme,
),
validationState: const PinValidationState.idle(),
cursor: const PinCursor.blinking(),
onCompleted: (pin) => print(pin),
);
To make every state look the same, pass PinThemes.all(defaultPinTheme). Without a theme, the built-in look marks the focused, disabled and error states.
PinTheme property |
Type / default |
width |
56.0 |
height |
60.0 |
textStyle |
TextStyle |
margin, padding |
EdgeInsetsGeometry |
constraints |
BoxConstraints |
decoration |
any Decoration |
placeholder |
Widget (replaces preFilledWidget) |
PinThemes in depth
PinThemes mirrors WidgetStateProperty: use PinThemes(fallback:, focused:, …), PinThemes.all, or PinThemes.resolveWith((item, validation) => ...) to pick a theme per pin. item has the pin's index, value, type (PinItemStateType: focused, filled, empty, disabled, error), isFilled, isFocused and showCursor. isFocused stays accurate while type is error, and showCursor tells a full PIN's focused last pin from the cursor.
- In the named form, a state you don't set uses
fallback.
- Set
valid or validating to style the pins while the app supplies PinValidationState.valid() or .validating(). Otherwise those states don't change the look.
- Implement
PinThemes to share a theme as your own type, or wrap part of your app in DefaultPinThemes(themes: ..., child: ...) to style every Pinput below it that has no theme.
- A state theme is used exactly as written and is not merged with
fallback, so derive it with copyWith.
PinTheme<T> and PinThemes<T> accept any Decoration (BoxDecoration, ShapeDecoration, …).
PinTheme.defaults is the built-in theme to derive from.
Controller and focus
final pinController = TextEditingController();
pinController.setText('1222'); // set text programmatically
pinController.appendPin('1', 4); // append a typed character, useful for a custom keyboard
pinController.deleteLastPin(); // delete the last character
// Text longer than Pinput.length is cut to that length, like typed text.
final pinputFocusNode = FocusNode();
pinputFocusNode.requestFocus();
pinputFocusNode.unfocus();
Pinput(
controller: pinController,
focusNode: pinputFocusNode,
);
Don't call setText, appendPin, deleteLastPin, requestFocus or unfocus in build. The snippet is only an illustration.
Cursor, masking and animation
Pinput(
cursor: PinCursor.blinking(child: MyCursor()), // static() or hidden() are also available
obscuring: const PinObscuring.character('*'), // null shows the original text
animation: const PinAnimation.slide(
beginOffset: Offset(0, 0.5),
duration: Duration(milliseconds: 250),
),
)
PinAnimation supports none(), scale() (default), fade(), slide(), rotation() and custom(transitionBuilder: …). Every constructor accepts duration and curve. Cursor blinking is independent of pin-content transitions, and PinCursor.blinking(blinkDuration: …) sets its speed. Animations and blinking stop when the platform asks to reduce motion.
Obscuring, layout and selection details
PinObscuring.character() uses a bullet by default. A character must be one UTF-16 code unit, matching Flutter's native requirement.
- To keep a typed pin visible for a moment before masking, pass a duration:
PinObscuring.character('*', Duration(seconds: 1)) or PinObscuring.widget(MyDot(), revealDuration: …).
PinObscuring.widget(MyDot()) supplies custom visible masking and uses a bullet for native obscuring. Masking widgets may be stateful: each slot creates its own State. Don't reuse a masking widget containing a shared GlobalKey across slots.
- The original text remains available in the controller and callbacks.
- With
itemBuilder, obscuring only enables native masking. The builder must mask visible content itself.
PinLayout(textDirection: TextDirection.ltr) keeps a numeric code left to right in an RTL app.
- Set
enableInteractiveSelection: false to disable interactive selection. Set contextMenuBuilder: null to hide the modern context menu while keeping selection enabled. Legacy custom selectionControls follow Flutter's legacy toolbar behavior. Use controls with TextSelectionHandleControls to customize handles with the modern menu API.
Validation
Pinput renders app-owned validation feedback through validationState:
| State |
Behavior |
PinValidationState.idle() |
Default; no current result or feedback |
PinValidationState.validating() |
Pending; normal appearance, no error |
PinValidationState.valid() |
Passed; normal appearance, no error |
PinValidationState.invalid([message]) |
Error pin theme and optional error message, even while focused |
Pinput(
validationState: state.pinValidation,
onChanged: cubit.pinChanged,
onCompleted: cubit.verifyPin,
)
Run synchronous or asynchronous validation in your app, using onChanged, onCompleted, onSubmitted or a submit button. onCompleted reports a full PIN with the cursor at the end, not successful validation. Clear old results when the PIN changes and ignore async responses for outdated inputs or disposed views.
Validation details and error widgets
- Pinput does not clear supplied feedback, run checks, or modify text or focus.
- Pending and valid states add no loading or success visuals and don't disable editing. Use
inputMode: PinInputMode.readOnly or PinInputMode.disabled explicitly when needed.
errorTextStyle styles the default error text. errorBuilder(context, message, pin) replaces the default error widget and ignores errorTextStyle. Both work in Pinput, including with itemBuilder.
- The message of
invalid() is optional: without it, error styling applies with no default text or error padding. An errorBuilder still runs with a null message, so it can supply its own localized text.
- The
validating() factory uses the PinValidationValidating subtype for pattern matching. PinAnimation and PinObscuring support value equality, and custom masking widgets use normal widget equality.
- For a runnable async example, open Async example in the default example app, or the Async validation tab in the demo gallery. Enter
2222 for success. See AsyncValidationExample.
Form integration
Pinput does not register itself with Form