TextFormField constructor
TextFormField({
- Key? key,
- TextEditingController? controller,
- String? initialValue,
- FocusNode? focusNode,
- InputDecorationV3? decoration = const InputDecorationV3(),
- TextInputType? keyboardType,
- TextCapitalization textCapitalization = TextCapitalization.none,
- TextInputAction? textInputAction,
- TextStyle? style,
- StrutStyle? strutStyle,
- TextDirection? textDirection,
- TextAlign textAlign = TextAlign.start,
- TextAlignVertical? textAlignVertical,
- bool autofocus = false,
- bool readOnly = false,
- ToolbarOptions? toolbarOptions,
- bool? showCursor,
- String obscuringCharacter = '•',
- bool obscureText = false,
- bool autocorrect = true,
- SmartDashesType? smartDashesType,
- SmartQuotesType? smartQuotesType,
- bool enableSuggestions = true,
- @Deprecated('Use autovalidateMode parameter which provide more specific ' 'behaviour related to auto validation. ' 'This feature was deprecated after v1.19.0.') bool autovalidate = false,
- @Deprecated('Use maxLengthEnforcement parameter which provides more specific ' 'behavior related to the maxLength limit. ' 'This feature was deprecated after v1.25.0-5.0.pre.') MaxLengthEnforcement? maxLengthEnforcement,
- int? maxLines = 1,
- int? minLines,
- bool expands = false,
- ValueChanged<
String> ? onChanged, - GestureTapCallback? onTap,
- VoidCallback? onEditingComplete,
- ValueChanged<
String> ? onFieldSubmitted, - FormFieldSetter<
String> ? onSaved, - FormFieldValidator<
String> ? validator, - List<
TextInputFormatter> ? inputFormatters, - bool? enabled,
- Radius? cursorRadius,
- Brightness? keyboardAppearance,
- EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
- bool enableInteractiveSelection = true,
- TextSelectionControls? selectionControls,
- InputCounterWidgetBuilder? buildCounter,
- ScrollPhysics? scrollPhysics,
- Iterable<
String> ? autofillHints, - AutovalidateMode? autovalidateMode,
- ScrollController? scrollController,
- String? restorationId,
- bool enableIMEPersonalizedLearning = true,
- bool hasFocus = false,
Creates a FormField that contains a TextField.
When a controller
is specified, initialValue
must be null (the
default). If controller
is null, then a TextEditingController
will be constructed automatically and its text
will be initialized
to initialValue
or the empty string.
For documentation about the various parameters, see the TextField class
and new TextField
, the constructor.
Implementation
TextFormField(
{Key? key,
this.controller,
String? initialValue,
FocusNode? focusNode,
InputDecorationV3? decoration = const InputDecorationV3(),
TextInputType? keyboardType,
TextCapitalization textCapitalization = TextCapitalization.none,
TextInputAction? textInputAction,
TextStyle? style,
StrutStyle? strutStyle,
TextDirection? textDirection,
TextAlign textAlign = TextAlign.start,
TextAlignVertical? textAlignVertical,
bool autofocus = false,
bool readOnly = false,
ToolbarOptions? toolbarOptions,
bool? showCursor,
String obscuringCharacter = '•',
bool obscureText = false,
bool autocorrect = true,
SmartDashesType? smartDashesType,
SmartQuotesType? smartQuotesType,
bool enableSuggestions = true,
@Deprecated(
'Use autovalidateMode parameter which provide more specific '
'behaviour related to auto validation. '
'This feature was deprecated after v1.19.0.',
)
bool autovalidate = false,
@Deprecated(
'Use maxLengthEnforcement parameter which provides more specific '
'behavior related to the maxLength limit. '
'This feature was deprecated after v1.25.0-5.0.pre.',
)
MaxLengthEnforcement? maxLengthEnforcement,
int? maxLines = 1,
int? minLines,
bool expands = false,
// int? maxLength,
ValueChanged<String>? onChanged,
GestureTapCallback? onTap,
VoidCallback? onEditingComplete,
ValueChanged<String>? onFieldSubmitted,
FormFieldSetter<String>? onSaved,
FormFieldValidator<String>? validator,
List<TextInputFormatter>? inputFormatters,
bool? enabled,
// double cursorWidth = 2.0,
// double? cursorHeight,
Radius? cursorRadius,
// Color? cursorColor,
Brightness? keyboardAppearance,
EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
bool enableInteractiveSelection = true,
TextSelectionControls? selectionControls,
InputCounterWidgetBuilder? buildCounter,
ScrollPhysics? scrollPhysics,
Iterable<String>? autofillHints,
AutovalidateMode? autovalidateMode,
ScrollController? scrollController,
String? restorationId,
bool enableIMEPersonalizedLearning = true,
bool hasFocus = false})
: assert(initialValue == null || controller == null),
assert(obscuringCharacter.length == 1),
assert(
autovalidate == false ||
autovalidate == true && autovalidateMode == null,
'autovalidate and autovalidateMode should not be used together.',
),
assert(maxLines == null || maxLines > 0),
assert(minLines == null || minLines > 0),
assert(
(maxLines == null) || (minLines == null) || (maxLines >= minLines),
"minLines can't be greater than maxLines",
),
assert(
!expands || (maxLines == null && minLines == null),
'minLines and maxLines must be null when expands is true.',
),
assert(!obscureText || maxLines == 1,
'Obscured fields cannot be multiline.'),
// assert(maxLength == null ||
// maxLength == TextField.noMaxLength ||
// maxLength > 0),
super(
key: key,
restorationId: restorationId,
initialValue:
controller != null ? controller.text : (initialValue ?? ''),
onSaved: onSaved,
validator: validator,
enabled: enabled ?? decoration?.enabled ?? true,
autovalidateMode: autovalidate
? AutovalidateMode.always
: (autovalidateMode ?? AutovalidateMode.disabled),
builder: (FormFieldState<String> field) {
final _TextFormFieldState state = field as _TextFormFieldState;
final InputDecorationV3 effectiveDecoration = (decoration ??
const InputDecorationV3())
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
void onChangedHandler(String value) {
field.didChange(value);
if (onChanged != null) {
onChanged(value);
}
}
final TextStyle errorStyle = effectiveDecoration.errorStyle ??
Theme.of(field.context)
.textTheme
.caption!
.copyWith(color: Theme.of(field.context).errorColor);
final labelStyle = effectiveDecoration.labelStyle ??
Theme.of(field.context).textTheme.bodyText1;
style = style ??
TextStyle(
fontSize: 16,
fontFamily: UI.getFontFamily('TitilliumWeb', field.context),
color: Theme.of(field.context).textTheme.headline2?.color);
return UnmanagedRestorationScope(
bucket: field.bucket,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
decoration?.label != null || decoration?.labelText != null
? Padding(
padding: const EdgeInsets.only(bottom: 3),
child: decoration?.label ??
Text(
decoration?.labelText ?? "",
style: labelStyle,
),
)
: Container(),
Stack(
alignment: Alignment.center,
children: [
InnerShadowBGCar(
isWhite:
(enabled ?? decoration!.enabled) ? true : false,
child: SizedBox(
width: double.infinity,
height: 11 + (maxLines ?? 1) * 21,
)),
Container(
height: 11 + 16.h + (maxLines ?? 1) * 21,
decoration: hasFocus
? BoxDecoration(
borderRadius: const BorderRadius.all(
Radius.circular(8)),
border: Border.all(
width: 1.5,
color: Theme.of(field.context)
.toggleableActiveColor))
: null,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
children: [
Expanded(
child: TextField(
restorationId: restorationId,
controller: state._effectiveController,
focusNode: focusNode,
decoration: effectiveDecoration.copyWith(
errorText: null,
label: null,
isDense: true,
contentPadding:
const EdgeInsets.symmetric(
vertical: 5.5),
hintStyle: TextStyle(
fontSize:
UI.getTextSize(16, field.context),
fontFamily: UI.getFontFamily(
'TitilliumWeb', field.context),
color: UI.isDarkTheme(field.context)
? Colors.white.withAlpha(125)
: const Color(0x77565554)),
suffix: null,
suffixIcon: null),
keyboardType: keyboardType,
textInputAction: textInputAction,
style: style,
strutStyle: strutStyle,
textAlign: textAlign,
textAlignVertical: textAlignVertical,
textDirection: textDirection,
textCapitalization: textCapitalization,
autofocus: autofocus,
toolbarOptions: toolbarOptions,
readOnly: readOnly,
showCursor: hasFocus,
obscuringCharacter: obscuringCharacter,
obscureText: obscureText,
autocorrect: autocorrect,
smartDashesType: smartDashesType ??
(obscureText
? SmartDashesType.disabled
: SmartDashesType.enabled),
smartQuotesType: smartQuotesType ??
(obscureText
? SmartQuotesType.disabled
: SmartQuotesType.enabled),
enableSuggestions: enableSuggestions,
maxLengthEnforcement: maxLengthEnforcement,
maxLines: maxLines,
minLines: minLines,
expands: expands,
maxLength: null,
onChanged: onChangedHandler,
onTap: onTap,
onEditingComplete: onEditingComplete,
onSubmitted: onFieldSubmitted,
inputFormatters: inputFormatters,
enabled: enabled ?? decoration!.enabled,
cursorWidth: 2,
cursorHeight: 20,
cursorRadius: cursorRadius,
cursorColor: Theme.of(field.context)
.toggleableActiveColor,
scrollPadding: scrollPadding,
scrollPhysics: scrollPhysics,
keyboardAppearance: keyboardAppearance,
enableInteractiveSelection:
enableInteractiveSelection,
selectionControls: selectionControls,
buildCounter: buildCounter,
autofillHints: autofillHints,
scrollController: scrollController,
enableIMEPersonalizedLearning:
enableIMEPersonalizedLearning,
)),
decoration!.suffix != null
? decoration.suffix!
: decoration.suffixIcon != null
? decoration.suffixIcon!
: Container()
],
)),
],
),
field.errorText != null && field.errorText!.isNotEmpty
? Padding(
padding: const EdgeInsets.only(top: 3),
child: Text(
field.errorText!,
style: errorStyle,
),
)
: Container()
],
));
},
);