APincodeField
A one-time code field: a row of cells for the digits of a code from an email or SMS. Input goes into a hidden numeric field, and every cell is drawn by a template macro from the markup — the look of the cells is entirely up to the author. When all the digits are entered, the field submits the form it sits in.
Type:
multi-child
(the child is the cell macro)
Form @code:""
APincodeField name:"code" length:4
@Cell $focused:false $isCurrent:false $value:""
SizedBox width:48 height:56
Center
Text $value
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name
|
string |
pincode
|
Name of the form field: the entered code is written into the form's
@
variable with this name
|
length
|
int | 6 | Number of digits and cells |
enabled
|
bool | true | Whether input is allowed |
isHidden
|
bool | true | Has no effect: whether a cell shows the digit or a dot is decided by the macro |
Children
The only required child is the
cell macro
(
@Name …
). The field repeats it
length
times in a centered row with a gap of 16 and passes each copy:
| Macro parameter | Type | What it holds |
|---|---|---|
$value
|
string | The digit in this cell; an empty string if not entered yet |
$isCurrent
|
bool | This is the cell the next digit goes to |
$focused
|
bool | The field is focused right now (the same for all cells) |
Without the macro the field draws nothing (in a debug build — the error "Macros child not found").
Behavior
-
The field takes focus when it appears on screen and when any cell is tapped; it accepts only digits, no more than
length. -
When all
lengthdigits are entered, the field submits the form — the same as a button withonPressed:"form:submit". That is whyAPincodeFieldgoes inside a Form ; success is caught withActions "form:submit":{…}around the form. -
On submit the code is written into the form variable
name; an empty field clears it.
Examples
Cells with the current one highlighted
@verified:false
Actions "form:submit":{verified = true}
Form @otp:""
Column spacing:24
Text "Enter the code from the email"
APincodeField name:"otp" length:6
@Cell $focused:false $isCurrent:false $value:""
DecoratedBox
#decoration color:#f1f5f9
#borderRadius circular 12
#border all color:{focused && isCurrent ? 0xFF4F46E5 : 0xFFCBD5E1} width:{focused && isCurrent ? 2 : 1}
SizedBox width:44 height:56
Center
Text $value
#style fontSize:24 fontWeight:w600
Text {verified ? "Code accepted" : ""}
Hidden digits
A dot instead of the digit is set in the macro itself:
Form @pin:""
APincodeField name:"pin" length:4
@Dot $focused:false $isCurrent:false $value:""
SizedBox width:24 height:24
Center
Text {value == "" ? "○" : "●"}
Differences between the app and the site
APincodeField
is not supported on the site: the field is not drawn, and the diagnostics show "unsupported widget: APincodeField". On the site a code is entered with a plain
TextFormField
with
#validators
#pattern
.
See also
- Form — the form the field submits
- TextFormField — a regular form field
- DecoratedBox — decorating a cell