Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 166 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flutter TextField input only decimal numbers

#1
I want to input only decimal number in `TextField` in `Flutter`. I tried below code but that's not working. It allows alpha (a-z) and special characters.

TextField(
controller:
new TextEditingController(text: listDisplay[position].getBlockQty(),
),
textAlign: TextAlign.center,
maxLines: 1,
keyboardType: TextInputType.numberWithOptions(
decimal: true,
signed: false),
),

Reply

#2
I have used this code in one of my Project and its working fine. I hope it will helps you.

TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Amount",
hintStyle:
TextStyle(color: Colors.grey, fontSize: 12.0),
),
style: TextStyle(color: Colors.black, fontSize: 12.0),
controller: _amountController[index],
textInputAction: TextInputAction.done,
keyboardType: TextInputType.numberWithOptions(decimal: true),)

[![enter image description here][1]][1]
[1]:
Reply

#3
You can use `TextInputFormatter` for this case.

Here is an example for the same,

Subclass `TextInputFormatter`
```dart
import 'package:flutter/services.dart';

class RegExInputFormatter implements TextInputFormatter {
final RegExp _regExp;

RegExInputFormatter._(this._regExp);

factory RegExInputFormatter.withRegex(String regexString) {
try {
final regex = RegExp(regexString);
return RegExInputFormatter._(regex);
} catch (e) {
// Something not right with regex string.
assert(false, e.toString());
return null;
}
}

@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
final oldValueValid = _isValid(oldValue.text);
final newValueValid = _isValid(newValue.text);
if (oldValueValid && !newValueValid) {
return oldValue;
}
return newValue;
}

bool _isValid(String value) {
try {
final matches = _regExp.allMatches(value);
for (Match match in matches) {
if (match.start == 0 && match.end == value.length) {
return true;
}
}
return false;
} catch (e) {
// Invalid regex
assert(false, e.toString());
return true;
}
}
}

```

Use it in with your textfield
```dart
final _amountValidator = RegExInputFormatter.withRegex('^\$|^(0|([1-9][0-9]{0,}))(\\.[0-9]{0,})?\$');
...
TextField(
inputFormatters: [_amountValidator],
keyboardType: TextInputType.numberWithOptions(
decimal: true,
signed: false,
),
)
```
Reply

#4
I am using this code and it's giving different results , on some phones it shows only numbers and on others it shows dashes and other chars , so it depends on the launcher or something like that i guess, in iPhone i guess it will show the correct keyboard ( i have tried on iPhone 7s plus).

the ```inputFormatters``` will allow only numbers so you will get what you want.

```dart
TextField(
textAlign: TextAlign.start,
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
),
```
Reply

#5
Add the inputFormatters line:




TextField(
inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}'))],
...)
Reply

#6
I've been looking the answer to this problem too and found it after many attempts and hard work.

you can use this regex for decimal.

```
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^[0-9]+.?[0-9]*'))
],
```
Reply

#7
try this one for decimal

```
import 'package:flutter/services.dart';
...
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.,]+')),],
onChanged: (value) => doubleVar = double.parse(value),
```

Also

RegExp('([0-9]+(\.[0-9]+)?)') For allow digits and only one dot
Reply

#8
try this:
```dart
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
TextInputFormatter.withFunction((oldValue, newValue) {
final text = newValue.text;
return text.isEmpty
? newValue
: double.tryParse(text) == null
? oldValue
: newValue;
}),
],
```

demo:

```dart
TextFormField(
keyboardType: TextInputType.numberWithOptions(
decimal: true,
signed: false,
),
onChanged: _yourOnChange,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
TextInputFormatter.withFunction((oldValue, newValue) {
final text = newValue.text;
return text.isEmpty
? newValue
: double.tryParse(text) == null
? oldValue
: newValue;
}),
],
)
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through