controller property
A controller for an editable text field.
Implementation
@override
TextEditingControllerImp get controller {
if (!_formIsSet) {
form ??= InjectedFormImp._currentInitializedForm;
_formIsSet = true; // TODO check me
if (form != null) {
formTextFieldDisposer =
(form as InjectedFormImp).addTextFieldToForm(this);
if (form!.autovalidateMode == AutovalidateMode.always) {
//When initialized and always auto validated, then validate in the next
//frame
WidgetsBinding.instance.addPostFrameCallback(
(timeStamp) {
form!.validate();
},
);
} else {
if (_validateOnLoseFocus == null && _validateOnValueChange != true) {
//If the TextField is inside a On.form, set _validateOnLoseFocus to
//true if it is not
_validateOnLoseFocus = true;
if (!_isValidOnLoseFocusDefined) {
_listenToFocusNodeForValidation();
}
}
}
}
}
if (_controller != null) {
value; // fix issue 241
return _controller!;
}
// _removeFromInjectedList = addToInjectedModels(this);
_controller ??= TextEditingControllerImp.fromValue(
TextEditingValue(
text: initialValue ?? '',
selection: _selection,
composing: _composing,
),
inj: this,
);
if (_validator == null) {
//If the field is not validate then set its snapshot to hasData, so that
//in the [InjectedForm.isValid] consider it as a valid field
snapValue = snapValue.copyToHasData(text);
}
// else {
// //IF there is a validator, then set with idle flag so that isValid
// //is false unless validator is called
// snapState = snapState.copyToIsIdle(this.text);
// }
_controller!.addListener(() {
if (isReadOnly) {
if (_controller!.text != snapValue.state) {
_controller!.text = snapValue.state;
}
return;
}
onTextEditing?.call(this);
if (snapValue.state == _controller!.text) {
//if only selection is changed notify and return
notify();
return;
}
isDirty = _controller!.text.trim() != _initialIsDirtyText.trim();
snapValue = snapValue.copyWith(data: _controller!.text);
if (form != null) {
//If form is not null than override the autoValidate of this Injected
_validateOnValueChange ??=
form!.autovalidateMode != AutovalidateMode.disabled;
}
if (_validateOnValueChange ?? !(_validateOnLoseFocus ?? false)) {
validate();
}
notify();
});
return _controller!;
}