Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the text directly in the controller interferes onChanged #79

Closed
LuDColl opened this issue Jun 28, 2023 · 1 comment
Closed

Comments

@LuDColl
Copy link

LuDColl commented Jun 28, 2023

I updated the text directly in the empty controller and when I edited it in onChanged it returned the empty text instead of the one I inserted:

controller.text; // show ''
controller.text = mask.maskText('new text');
controller.text; // show 'new text'

TextField(
  controller: controller,
  inputFormatters: [mask],
  onChanged: (text) {
    text; // show ''
    controller.text; // show ''
  }
)

I managed to make it work only when I forced the Mask's formatEditUpdate:

controller.text; // show ''
controller.text = mask.maskText('new text');
controller.text; // show 'new text'

final value = controller.value;
mask.formatEditUpdate(value, value);

TextField(
  controller: controller,
  inputFormatters: [mask],
  onChanged: (text) {
    text; // show 'new text'
    controller.text; // show 'new text'
  }
)

Could you fix it, so I don't have to force formatEditUpdate?

@siqwin
Copy link
Owner

siqwin commented Nov 28, 2023

Instead of using the maskText method, use the initialText parameter of the MaskTextInputFormatter constructor.
The maskText method can be used if you need to format text in isolation from text fields.

@siqwin siqwin closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants