MohanKumar
MohanKumar

Reputation: 1115

Flutter: KeyboardType attribute not working as expected in TextFormField ,still can paste text.how to change input type?(Flutter)

I am using a TextFormField to get a number as input so i used this snippet

keyboardType: TextInputType.number,

This is my TextFormField Widget code

         TextFormField(
                  keyboardType: TextInputType.number,
                  style: textstyle,
                  controller: principal,
                  validator: (String value) {
                    if (value.isEmpty) {
                      return "Please enter principal amount e.g 3245";
                    }
                  },
                  decoration: InputDecoration(
                      labelText: "Principal",
                      labelStyle: textstyle,
                      hintText: "Rupees",
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(5.0))),
                )

But now i can paste some text in the same field as shown hereenter image description here

So now how can i format my TextFormfield to restrict to type only number & to restrict pasting texts?

Upvotes: 2

Views: 5435

Answers (2)

Stanislav Bondar
Stanislav Bondar

Reputation: 6245

UPDATE

Deprecated API removed after v2.5

Old answer

You can try to use WhitelistingTextInputFormatter like

inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],

See official docs

Upvotes: 3

Yivan000
Yivan000

Reputation: 82

The accepted answer is now deprecated as of Flutter version 1.20.0-1.0.pre. Now, use inputFormatters: [FilteringTextInputFormatter.digitsOnly], instead (see documentation).

In addition, if you want finer control onto what to input using RegEx, use FilteringTextInputFormatter.allow(RegExp(r'your regex here')).

Upvotes: 4

Related Questions