Skip to content

Commit

Permalink
fix: fixed text field validation if controller changed from the outsi…
Browse files Browse the repository at this point in the history
…de (#694)
  • Loading branch information
dtscalac authored Aug 16, 2024
1 parent a614fc8 commit dc820e3
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions catalyst_voices/lib/widgets/text_field/voices_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class _VoicesTextFieldState extends State<VoicesTextField> {
VoicesTextFieldValidationResult _validation =
const VoicesTextFieldValidationResult(status: VoicesTextFieldStatus.none);

@override
void initState() {
super.initState();
_obtainController().addListener(_onChanged);
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
Expand All @@ -89,13 +95,21 @@ class _VoicesTextFieldState extends State<VoicesTextField> {
void didUpdateWidget(VoicesTextField oldWidget) {
super.didUpdateWidget(oldWidget);

final newController = _obtainController();

// unregister listener from potentially old controllers
_customController?.removeListener(_onChanged);
oldWidget.controller?.removeListener(_onChanged);

// the widget got controller from the parent, lets dispose our own
if (widget.controller != null) {
_customController?.dispose();
_customController = null;
}

final newController = _obtainController();
// register for a new one
newController.addListener(_onChanged);

if (oldWidget.decoration?.errorText != widget.decoration?.errorText ||
oldWidget.validator != widget.validator ||
oldWidget.controller?.text != newController.text) {
Expand All @@ -107,6 +121,7 @@ class _VoicesTextFieldState extends State<VoicesTextField> {
void dispose() {
_customController?.dispose();
_customController = null;
widget.controller?.removeListener(_onChanged);
super.dispose();
}

Expand Down Expand Up @@ -220,7 +235,7 @@ class _VoicesTextFieldState extends State<VoicesTextField> {
minLines: widget.minLines,
maxLength: widget.maxLength,
enabled: widget.enabled,
onChanged: _onChanged,
onChanged: widget.onChanged,
),
],
);
Expand Down Expand Up @@ -302,9 +317,8 @@ class _VoicesTextFieldState extends State<VoicesTextField> {
return customController;
}

void _onChanged(String value) {
_validate(value);
widget.onChanged?.call(value);
void _onChanged() {
_validate(_obtainController().text);
}

void _validate(String value) {
Expand Down

0 comments on commit dc820e3

Please sign in to comment.