DecimalTextInputFormatter class
An advanced TextInputFormatter for numeric fields.
Features:
- Decimal support with precision control via decimalRange
(
null= unlimited,0= integers only,n= up tonfraction digits). - Negative number support via allowNegative.
- Range validation via min / max with live feedback.
- Live error feedback through errorNotifier.
- State management integration through valueNotifier (parsed num).
Character-level rules (precision, sign, single decimal point) are enforced hard — invalid keystrokes are rejected. Range rules are enforced soft — the value is still accepted while typing, but errorNotifier is updated so the UI can show an inline error.
final errorText = ValueNotifier<String?>(null);
final value = ValueNotifier<num?>(null);
TextField(
keyboardType: const TextInputType.numberWithOptions(
decimal: true,
signed: true,
),
inputFormatters: [
DecimalTextInputFormatter(
decimalRange: 2,
allowNegative: true,
min: -50,
max: 100,
errorNotifier: errorText,
valueNotifier: value,
),
],
);
// Show live error:
ValueListenableBuilder<String?>(
valueListenable: errorText,
builder: (_, error, __) => Text(error ?? ''),
);
- Inheritance
-
- Object
- TextInputFormatter
- DecimalTextInputFormatter
Constructors
-
DecimalTextInputFormatter({int? decimalRange = 2, bool allowNegative = false, num? min, num? max, ValueNotifier<
String?> ? errorNotifier, ValueNotifier<num?> ? valueNotifier, String? minErrorText, String? maxErrorText})
Properties
- allowNegative → bool
-
Whether a leading minus sign is allowed.
final
- decimalRange → int?
-
Number of digits allowed after the decimal point.
final
-
errorNotifier
→ ValueNotifier<
String?> ? -
Receives the current validation error message (or
nullwhen valid).final - hashCode → int
-
The hash code for this object.
no setterinherited
- max → num?
-
Inclusive maximum. When the parsed value is above this, errorNotifier
is updated with maxErrorText.
final
- maxErrorText → String?
-
Custom message when the value is above max.
final
- min → num?
-
Inclusive minimum. When the parsed value is below this, errorNotifier
is updated with minErrorText.
final
- minErrorText → String?
-
Custom message when the value is below min.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
valueNotifier
→ ValueNotifier<
num?> ? -
Receives the current parsed value (or
nullwhen empty/incomplete).final
Methods
-
formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) → TextEditingValue -
Called when text is being typed or cut/copy/pasted in the EditableText.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited