flutter-masked-text icon indicating copy to clipboard operation
flutter-masked-text copied to clipboard

TextFormField value disappears when next field is tapped

Open leofontes opened this issue 4 years ago • 2 comments

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.

leofontes avatar Dec 07 '19 04:12 leofontes

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

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

Bonfs avatar Dec 09 '19 15:12 Bonfs

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.

ahmedjaafar6 avatar Jun 10 '21 05:06 ahmedjaafar6