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

TextFormField value disappears when next field is tapped #47

Open
leofontes opened this issue Dec 7, 2019 · 2 comments
Open

TextFormField value disappears when next field is tapped #47

leofontes opened this issue Dec 7, 2019 · 2 comments

Comments

@leofontes
Copy link

Hi

I'm attempting to use this library to mask some of my TextFormField fields. However, when I add the controller attribute using the MaskedTextController, whenever I tap on the next field or exit the field I'm editing by any way, the TextFormField value is cleaned.

I've checked issues #13 and #28 but neither helped me.

Dependency:

  flutter_masked_text: ^0.8.0

This is my code:

class _SignupInfoStepState extends State<SignupInfoStep> {
  final formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final MaskedTextController cpfController = MaskedTextController(mask: '000.000.000-00', text: widget.form.cpf);
    cpfController.afterChange = (String previous, String next) {
      cpfController.updateText(next);
    };

    return SingleChildScrollView(
      child: Form(
        key: formKey,
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            children: <Widget>[
              if (widget.form.complete) ...[
                TextFormField(
                  decoration: InputDecoration(
                    labelText: 'CPF',
                    border: OutlineInputBorder(),
                  ),
                  style: TextStyle(fontSize: 20),
                  initialValue: widget.form.cpf,
                  validator: (cpf) {
                    if (cpf == null || cpf.isEmpty) {
                      return 'Campo obrigatório';
                    }
                    if (!CPFValidator.isValid(cpf)) {
                      return 'CPF inválido';
                    }
                    return null;
                  },
                  keyboardType: TextInputType.number,
                  onSaved: (cpf) {
                    setState(() => widget.form.cpf = cpf);
                  },
                  maxLength: 14,
                  controller: cpfController,
                ),
            ],
          ),
        ),
      ),
    );
  }
}

flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15.1 19B88, locale
    en-US)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 11.2.1)
[✓] Android Studio (version 3.5)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.3)
[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.
@Bonfs
Copy link

Bonfs commented Dec 9, 2019

Try declare your controller outside of build method as a propertie.
`class _SignupInfoStepState extends State {
final formKey = GlobalKey();
final cpfController = MaskedTextController(mask: '000.000.000-00', text: widget.form.cpf);

@OverRide
Widget build(BuildContext context) {
final theme = Theme.of(context);
`

@ahmedjaafar6
Copy link

Try declare your controller outside of build method as a propertie.
`class _SignupInfoStepState extends State {
final formKey = GlobalKey();
final cpfController = MaskedTextController(mask: '000.000.000-00', text: widget.form.cpf);

@OverRide
Widget build(BuildContext context) {
final theme = Theme.of(context);
`

Thank you! I had mine declared inside the build method, once I put it above the build method it worked.

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

3 participants