Created
March 7, 2022 15:01
-
-
Save bienvenuelisis/3aa66c3fe1a9348bec9d24f2056c2867 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DoubleTextInputFormatter extends TextInputFormatter { | |
final bool positive; | |
final double maxValue; | |
DoubleTextInputFormatter({ | |
this.positive = false, | |
this.maxValue = double.maxFinite, | |
}); | |
@override | |
TextEditingValue formatEditUpdate( | |
TextEditingValue oldValue, | |
TextEditingValue newValue, | |
) { | |
return ((newValue.text.isEmpty) || | |
(newValue.text.startsWith(plus) && | |
(newValue.text.length == 1 || | |
validateDoubleValue( | |
newValue.text, | |
"...", | |
positive: positive, | |
maxValue: maxValue, | |
) == | |
null)) || | |
(!positive && | |
newValue.text.startsWith(minus) && | |
(newValue.text.length == 1 || | |
validateDoubleValue( | |
newValue.text, | |
"...", | |
positive: positive, | |
maxValue: maxValue, | |
) == | |
null)) || | |
(validateDoubleValue( | |
newValue.text, | |
"...", | |
positive: positive, | |
maxValue: maxValue, | |
) == | |
null)) | |
? newValue | |
: oldValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment