Skip to content

Instantly share code, notes, and snippets.

@bienvenuelisis
Created March 7, 2022 15:01
Show Gist options
  • Save bienvenuelisis/3aa66c3fe1a9348bec9d24f2056c2867 to your computer and use it in GitHub Desktop.
Save bienvenuelisis/3aa66c3fe1a9348bec9d24f2056c2867 to your computer and use it in GitHub Desktop.
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