currency_field_formatter 1.1.2
currency_field_formatter: ^1.1.2 copied to clipboard
Bulletproof Flutter currency TextInputFormatter with exact cursor tracking, backspace handling, Indian Lakhs/Crores, and ISO 4217 presets.
currency_field_formatter #
⚡ Why currency_field_formatter? #
Most currency input formatters in the Flutter ecosystem suffer from frustrating production bugs:
- The Cursor-Jumping Bug: Editing digits in the middle of an amount (e.g.
1,2[cursor]34,567) throws the cursor to the end of the input string.currency_field_formattercalculates exact character deltas so mid-text editing stays locked to the user's cursor. - Broken Backspace on Separators: Hitting backspace directly on a comma or period separator either does nothing or breaks formatting.
currency_field_formatterautomatically detects separator deletion and removes the preceding digit cleanly. - South Asian / Indian Numbering Support: Western formatters hardcode 3-digit groups (
1,000,000).currency_field_formatternatively supports Indian Lakhs & Crores grouping (10,00,000.00). - Direct Minor Units (Cents): Get payment gateway amounts (e.g.
$12.34->1234integer cents) directly fromformatter.integerCentswithout floating-point parsing errors.
📦 Installation #
Add currency_field_formatter to your pubspec.yaml:
dependencies:
currency_field_formatter: ^1.1.1
Or run:
flutter pub add currency_field_formatter
🚀 Quick Start #
1. US Dollar Input ($1,234.50) #
import 'package:flutter/material.dart';
import 'package:currency_field_formatter/currency_field_formatter.dart';
final formatter = CurrencyFieldFormatter.usd();
TextField(
keyboardType: const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [formatter],
onChanged: (text) {
print('Raw Numeric Value: ${formatter.numericValue}'); // 1234.50 (double)
print('Stripe Minor Cents: ${formatter.integerCents}'); // 123450 (int)
print('ISO String: ${formatter.toIsoString()}'); // "1234.50"
},
);
2. Indian Rupee with Lakhs & Crores (₹ 15,50,000.00) #
final inrFormatter = CurrencyFieldFormatter.inr();
TextField(
keyboardType: const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [inrFormatter],
);
3. Euro with Suffix Symbol & Comma Decimals (1.234,50 €) #
final eurFormatter = CurrencyFieldFormatter.eur();
TextField(
keyboardType: const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [eurFormatter],
);
4. Dynamic Formatting by Locale or Currency Code #
final dynamicFormatter = CurrencyFieldFormatter.byLocale('en_IN'); // or 'USD', 'EUR', 'GBP', 'JPY'
🛠️ API Reference #
Formatter Accessors #
| Property / Method | Type | Description | Example |
|---|---|---|---|
numericValue |
double |
The parsed decimal amount. | 1234.50 |
integerCents |
int |
Amount in minor units (zero-float precision for Stripe/Razorpay). | 123450 |
toIsoString() |
String |
Standard ISO unformatted decimal string. | "1234.50" |
Configuration Options #
| Parameter | Type | Default | Description |
|---|---|---|---|
symbol |
String |
'$' |
Currency symbol (e.g. $, ₹, €, £, ¥). |
symbolPosition |
SymbolPosition |
prefix |
Place symbol before (prefix) or after (suffix) amount. |
symbolSeparator |
String |
'' |
Spacing between symbol and amount (e.g. ' '). |
thousandSeparator |
String |
',' |
Grouping separator (e.g. , or .). |
decimalSeparator |
String |
'.' |
Decimal point character. |
allowDecimals |
bool |
true |
Whether fractional decimals are accepted. |
decimalDigits |
int |
2 |
Maximum decimal digits allowed. |
maxIntegerDigits |
int |
14 |
Maximum integer digits allowed. |
numberingSystem |
NumberingSystem |
western |
western (3, 3, 3) or indianLakhCrore (3, 2, 2). |
💖 Support the Project #
If you find this project useful, consider supporting its active maintenance and future development:
📄 License #
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Maintained with ❤️ by Govind Tank.