Igor L Sambo
Igor L Sambo

Reputation: 121

MaskTextInputFormatter erases characters when I do the insertion

I am using MaskInputFormatter to format a phone number textfield and it's a customTextField I created. 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 ###-##-##")],
),

Upvotes: 0

Views: 1790

Answers (2)

Erkinjon
Erkinjon

Reputation: 1

This code:

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

Upvotes: 0

J&#250;nior Nogueira
J&#250;nior Nogueira

Reputation: 106

try this

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
              ],
          ),
        ],
       ),
     ), 
    );
  }
}

That's how it works for me.

Upvotes: 3

Related Questions