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

MaskTextInputFormatter erases characters when I do the insertion #76

Closed
LSambo02 opened this issue Oct 26, 2022 · 2 comments
Closed

MaskTextInputFormatter erases characters when I do the insertion #76

LSambo02 opened this issue Oct 26, 2022 · 2 comments

Comments

@LSambo02
Copy link

When I insert the number it erases the previous digit. And when a non-number character is inserted it erases all the inserted data.

ScreenRecording

I tried using it in a 'vanilla' TextFormField and had the same result.

TextFormField(
  onChanged: (value) {
    setState(() {
      mobile.setContactNumber(value);
    });
  },
  initialValue:
      mobile.contactNumber,
  inputFormatters: [MaskTextInputFormatter(mask: "+258 ###-##-##")],
),
@sebandroidev
Copy link

I'm having the same issue. But i noticed if there is no onChanged function it's working fine.

@LSambo02
Copy link
Author

LSambo02 commented Oct 28, 2022

I'm having the same issue. But i noticed if there is no onChanged function it's working fine.

A solution I found was to declare the MaskTextInputFormatter outside the implementation.

class _MyHomePageState extends State<MyHomePage> {
  final  maskFormatter = MaskTextInputFormatter( mask: '+### ###-##-##');
  @override
  Widget build(BuildContext context) {
    return Scaffold(
     body: Padding(
       padding: const EdgeInsets.all(8.0),
       child: Column(
        children: [
          SizedBox(height: 50,),
          TextField(
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
              hintText: 'Enter Phone Number'
              ),
              inputFormatters: [
                maskFormatter
              ],
          ),
        ],
       ),
     ), 
    );
  }
}

Stackoverfow Response
And on my own implementation, I kept the onChanged() function. And you don't have necessarily to keep it a final. Just make sure you instantiate outside the Textfield/TextformField implementation.

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