Input only even numbers in textField Flutter - flutter

so, is it way to do TextFormField where users only can insert even numbers? Now they can't type for example 12 and that's the problem.
child: TextFormField(
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp("[1,3,5,7,9]"))
],
keyboardType: TextInputType.number,
),

The even or odd number can be tested by only looking at the last digit, which need to be even or odd too. So the Regex for odd number runs could be:
"$\s*(\d*[13579]\s*,\s*)*\d*[13579]$"
Use regrex expression as below
child: TextFormField(
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp("$\s*(\d*[13579]\s*,\s*)*\d*[13579]$"))
],
keyboardType: TextInputType.number,
),
For to deny even number use below regrex expression
"$\s*(\d*[02468]\s*,\s*)*\d*[02468]$"

Related

How to make sure textformfield input is a number(positive or negative or double)

Hi im facing the issue when converting user input to a double example when i input -3 i faced with an FormatException (FormatException: Invalid double -). How can i deal with this? Furthermore how can i prevent user from entering 2 - or . ( 3.3.3 or 3- or --3)as this will also cause error with the double.parse(). Thanks in advance!
TextFormField(
inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.-]')),],
keyboardType: TextInputType.number,
onChanged:(value) {
setState(() {
fieldPointX = double.parse(value);
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'X'
),
),
try using this regExp in your TextFormField widget
TextFormField(
keyboardType: const TextInputType.numberWithOptions(
signed: true,
decimal: true,
),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'^-?\d*.?\d*')),
],
),

make TextField accept only numbers and + sign in flutter

I want the field to allow the entry of numbers and the + sign only but all I could do was make the field allow the entry of numbers only and I couldn't make it accept the + sign
here is my code
TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(
"[0-9]",
),
),
],
),
TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(
r'^\+?\d*',
),
),],
)
This will accept an optional + sign at the begining and the rest are numbers. The same as international phone numbers.
TextField(
keyboardType:TextInputType.numberWithOptions(decimal:true,signed:true),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9.,]+')),
],
)
,
just add this.

How to accept only numbers in TextFormFields?

I want to accept only numbers in TextFormFields. I have designed a TextFormField for accepting phone numbers. I have changed the keyboard type.
keyboardType: TextInputType.phone,
But, it can also accept special symbols or can paste other inputs also.
Keyboard screenshots
Try below code, refer FilteringTextInputFormatter
import below library
import 'package:flutter/services.dart';
Your Widget:
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
//you can used below formater also
/* inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp("[0-9]"),
),
],*/
),
In Your TextFormField add:
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp("[0-9]")),
],
You can also modify RegExp for supporting any kind of input characters.
Try used input formatter
import 'package:flutter/src/services/text_formatter.dart';
TextFormField(
keyboardType: TextInputType.phone,
inputFormatters:
[
FilteringTextInputFormatter.digitsOnly
]
)
You can use FilteringTextInputFormatter.
TextField(
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
),
Simply, This could Work:
keyboardType: TextInputType.number,

How to include plus symbol to a numeric keyboard

I was wondering how to implement in my application a TextFormField with a numeric keyboard that includes the plus("+") with the intention of including of facilitating the user to input international phone numbers.
TextFormField(
controller: phoneNumberController,
keyboardType: TextInputType.number, // Fix to include plus symbol
),
change type TextInputType.number at TextInputType.phone
TextFormField(
controller: phoneNumberController,
keyboardType: TextInputType.phone, // Fix to include plus symbol
),

Dynamic TextFormField within a DataCell => (PaginatedDataTable)

I have a problem that when I create a TextFormField inside a DataCell it does not support placing a controller, that is, if I put a controller or even a focusnode, all the TextFormField in the table, which in theory are the same, will be typed together or always take the last one, it varies a lot.
Look at the code I'm doing:
DataCell(
TextFormField(
autofocus: true,
inputFormatters: [
new LengthLimitingTextInputFormatter(6),
],
style: TextStyle(color: context.read<DadosAcesso>().color),
initialValue: row.quantidade,
keyboardType: TextInputType.number,
onChanged: (text) {
txt = text;
updateProdutoQtde(text, index, row.codbarras);
},
onEditingComplete: (){
context.read<DadosAcesso>().search = false;
context.read<DadosAcesso>().focusBusca.requestFocus();
},
),
),
I tried using the provider there to see if it helped but it stayed the same, I believe I would have to do something with the index of the table, like creating a TextFormField that contains the index, the key will not be possible to use the index, because the index is int.