FormPinField<TValue> constructor
- Key? key,
- bool keepAlive = true,
- int maxLength = kPinLength,
- int? minLength,
- FormController<
TValue> ? form, - TextEditingController? controller,
- FormStyle? style,
- bool enabled = true,
- String? initialValue,
- FocusNode? focusNode,
- TextInputType keyboardType = TextInputType.phone,
- TextInputAction textInputAction = TextInputAction.done,
- List<
TextInputFormatter> ? inputFormatters, - bool autocorrect = false,
- bool enableInteractiveSelection = false,
- bool readOnly = false,
- Iterable<
String> ? autofillHints, - Cursor? mouseCursor,
- TextCapitalization textCapitalization = TextCapitalization.none,
- ValueChanged<
String> ? onChanged, - ValueChanged<
String> ? onSubmitted, - String? emptyErrorText,
- String? lengthErrorText,
- String? validator(
- String? value
- TValue onSaved(
- String value
- String? hintText,
- bool obscureText = false,
- bool autofocus = false,
Widget for Pin text field for forms.
Place under the Form that gave FormController.key, or pass FormController to form.
When FormController is passed to form, onSaved
must also be passed together. The contents of onSaved
will be used to save the data.
Enter the initial value given by FormController.value in initialValue
.
Each time the content is changed, onChanged
is executed.
When FormController.validate is executed, validation and data saving are performed.
Only when emptyErrorText
is specified, emptyErrorText
will be displayed as an error if no characters are entered.
Only when lengthErrorText
is specified, if the number of characters entered is less than minLength
, it is displayed as lengthErrorText
.
Other error checking is performed by specifying validator
.
If a string other than Null is returned in the callback, the string is displayed as an error statement. If Null is returned, it is processed as no error.
The onSubmitted
process is executed when the Enter key or other keys are pressed.
If enabled is false
, the text is deactivated.
If readOnly is set to true
, the activation is displayed, but the text cannot be changed.
If obscureText
is set to true
, the input string will be hidden. Please use this function for inputting passwords, etc.
If inputFormatters
is specified, it is possible to restrict the characters to be entered.
フォーム用のPinテキストフィールド用のウィジェット。
FormController.keyを与えたForm配下に配置、もしくはformにFormControllerを渡します。
formにFormControllerを渡した場合、一緒にonSaved
も渡してください。データの保存はonSaved
の内容が実行されます。
initialValue
にFormController.valueから与えられた初期値を入力します。
内容が変更される度onChanged
が実行されます。
FormController.validateが実行された場合、バリデーションとデータの保存を行ないます。
emptyErrorText
が指定されている時に限り、文字が入力されていない場合emptyErrorText
がエラーとして表示されます。
lengthErrorText
が指定されている時に限り、minLength
より入力された文字数が少ない場合lengthErrorText
として表示されます。
それ以外のエラーチェックはvalidator
を指定することで行ないます。
コールバック内でNull以外を返すようにするとその文字列がエラー文として表示されます。Nullの場合はエラーなしとして処理されます。
Enterキーなどが押された場合の処理をonSubmitted
が実行されます。
enabledがfalse
になるとテキストが非有効化されます。
readOnlyがtrue
になっている場合は、有効化の表示になりますが、テキストが変更できなくなります。
obscureText
がtrue
になると入力された文字列が隠されます。パスワードの入力などにご利用ください。
inputFormatters
が指定されると入力される文字を制限することが可能です。
Implementation
FormPinField({
super.key,
this.keepAlive = true,
int maxLength = kPinLength,
int? minLength,
this.form,
this.controller,
this.style,
super.enabled,
String? initialValue,
this.focusNode,
TextInputType keyboardType = TextInputType.phone,
TextInputAction textInputAction = TextInputAction.done,
List<TextInputFormatter>? inputFormatters,
bool autocorrect = false,
bool enableInteractiveSelection = false,
this.readOnly = false,
Iterable<String>? autofillHints,
Cursor? mouseCursor,
TextCapitalization textCapitalization = TextCapitalization.none,
ValueChanged<String>? onChanged,
ValueChanged<String>? onSubmitted,
String? emptyErrorText,
String? lengthErrorText,
String? Function(String? value)? validator,
TValue Function(String value)? onSaved,
String? hintText,
bool obscureText = false,
this.autofocus = false,
}) : assert(
(form == null && onSaved == null) ||
(form != null && onSaved != null),
"Both are required when using [form] or [onSaved].",
),
super(
initialValue:
controller != null ? controller.text : (initialValue ?? ""),
validator: (value) {
if (emptyErrorText.isNotEmpty && value.isEmpty) {
return emptyErrorText;
}
if (lengthErrorText.isNotEmpty && minLength.def(0) > value.length) {
return lengthErrorText;
}
return validator?.call(value);
},
onSaved: (value) {
if (value == null) {
return;
}
final res = onSaved?.call(value);
if (res == null) {
return;
}
form!.value = res;
},
autovalidateMode: AutovalidateMode.disabled,
builder: (FormFieldState<String> field) {
final _FormPinFieldState<TValue> state =
field as _FormPinFieldState<TValue>;
final context = state.context;
void onChangedHandler(String value) {
field.didChange(value);
if (onChanged != null) {
onChanged(value);
}
}
final mainTextStyle = style?.textStyle?.copyWith(
color: style.color,
) ??
Theme.of(context).textTheme.displaySmall?.copyWith(
color: style?.color ??
Theme.of(context).textTheme.displaySmall?.color,
) ??
TextStyle(
color: style?.color ??
Theme.of(context).textTheme.displaySmall?.color ??
Theme.of(context).textTheme.displaySmall?.color,
);
final subTextStyle = style?.textStyle?.copyWith(
color: style.subColor,
) ??
TextStyle(
color: style?.subColor ??
style?.color?.withOpacity(0.5) ??
Theme.of(context)
.textTheme
.titleMedium
?.color
?.withOpacity(0.5),
);
final errorTextStyle = style?.errorTextStyle?.copyWith(
color: style.errorColor,
) ??
style?.textStyle?.copyWith(
color: style.errorColor,
) ??
TextStyle(
color:
style?.errorColor ?? Theme.of(context).colorScheme.error,
);
final borderColor =
style?.borderColor ?? Theme.of(context).dividerColor;
return Container(
alignment: style?.alignment,
padding:
style?.padding ?? const EdgeInsets.symmetric(vertical: 16),
child: SizedBox(
height: style?.height,
width: style?.width,
child: UnmanagedRestorationScope(
bucket: field.bucket,
child: PinInputTextField(
pinLength: maxLength,
controller: state._effectiveController,
focusNode: state._effectiveFocusNode,
decoration: BoxLooseDecoration(
errorText: state.errorText,
hintText: hintText,
errorTextStyle: errorTextStyle,
hintTextStyle: subTextStyle,
obscureStyle: ObscureStyle(isTextObscure: obscureText),
strokeColorBuilder: FixedColorBuilder(borderColor),
bgColorBuilder: FixedColorBuilder(
style?.backgroundColor ??
Theme.of(context).scaffoldBackgroundColor,
),
textStyle: mainTextStyle,
),
keyboardType: keyboardType,
textInputAction: textInputAction,
textCapitalization: textCapitalization,
autocorrect: autocorrect,
onChanged: onChangedHandler,
onSubmit: onSubmitted,
inputFormatters: inputFormatters,
enabled: enabled,
enableInteractiveSelection: enableInteractiveSelection,
autofillHints: autofillHints,
cursor: mouseCursor,
),
),
),
);
},
);