Dart SDK version: 2.13.4 (stable)
Flutter 2.2.3 • channel stable
i'm writing this answer on July 25, 2021, suggesting a little bit simpler solution, only using built-in TextField's `inputFormatters`.
i'm trying to ensure all of you, that the field won't accept floating point longer than 2(accepted: 12.25 vs notAccepted: 65.536). And also, it won't accept multiple dots, only accept a single dot(accepted: 12.25 vs notAccepted: 1.1.11, 1.11, .1.1, 1.1.1.1, whatsoever..).
The difference considering the other answers is that the code below won't accept `.1` which is equal to `0.1` programmatically, which is actually way more user-friendly. And it is simple, nice to look at for us. You can just copy and paste the code below into `inputFormatters: []`.
If you want to accept both `0.1` and `.1`(not only `0.1`), you can just comment out `FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*'))`!
`inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')),
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*')),
]`
When you want to change the maximum decimal point, you can change the `N` value in the line below:
`inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,N}')),
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*')),
]`
First example shows the case in which you want to limit decimal point to 2, and the N value exactly controls the limit. You might try within your own code [:
Appreciate all the other answers, hope this answer might help future wanderers! Have a wonderful day [: