💰 flutter_currency_text_input_formatter

A simple and flexible Flutter TextInputFormatter to automatically format text input as currency. It handles decimal points, thousand separators, and supports customizable leading/trailing symbols.

✨ Features

  • Automatic Currency Formatting: Formats numbers with thousand separators as you type.
  • Customizable Symbols: Easily set the leading symbol (e.g., \$, £), trailing symbol (e.g., €), decimal separator, and thousand separator.
  • Decimal Digit Control: Specify the maximum number of decimal digits allowed.
  • Smart Input Handling: Manages cursor position and prevents invalid inputs (like multiple leading zeros or non-numeric characters).

🚀 Getting Started

Installation

Add the package to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  flutter_currency_text_input_formatter: ^0.0.1 # Use the latest version

Then, run:

flutter pub get

Basic Usage

Use the FlutterCurrencyTextInputFormatter in the inputFormatters property of your TextField or TextFormField.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; // Needed for TextInputFormatter
import 'package:flutter_currency_text_input_formatter/flutter_currency_text_input_formatter.dart';

// ... Inside your Widget build method:

TextFormField(
  keyboardType: TextInputType.number,
  decoration: const InputDecoration(
    labelText: 'Enter Amount',
    border: OutlineInputBorder(),
  ),
  inputFormatters: [
    // Basic currency formatting with 2 decimal places and '$' prefix
    FlutterCurrencyTextInputFormatter(
      maxDecimalDigits: 2,
      leadingSymbol: '\$',
      thousandSeparator: ',',
    ),
  ],
)

🛠 Configuration

The FlutterCurrencyTextInputFormatter, allows for extensive customization through its constructor:

Parameter Type Default Description
maxDecimalDigits int? null The maximum number of digits allowed after the decimal point. If null, any number of digits is allowed.
leadingSymbol String '' Symbol placed at the start of the formatted number (e.g., \$).
trailingSymbol String '' Symbol placed at the end of the formatted number (e.g., €).
decimalSeparator String . The character used to separate the whole and fractional parts (e.g., .).
thousandSeparator String , The character used to separate thousands (e.g., ,).

Examples

Euro Formatting (using a trailing symbol and different separators)

For European format (e.g., 1.234,56 €):

TextFormField(
  inputFormatters: [
    FlutterCurrencyTextInputFormatter(
      maxDecimalDigits: 2,
      decimalSeparator: ',', // Use comma for decimal
      thousandSeparator: '.', // Use dot for thousand separator
      trailingSymbol: ' €', // Use a trailing symbol
    ),
  ],
)

Integer Only

To accept only whole numbers (no decimal):

TextFormField(
  inputFormatters: [
    FlutterCurrencyTextInputFormatter(
      maxDecimalDigits: 0, // Set to 0 to prevent decimals
      leadingSymbol: 'Count: ',
      thousandSeparator: ' ', // Use a space for thousand separator
    ),
  ],
)

👥 Contribution

Feel free to fork the repository and submit pull requests to contribute enhancements or fixes.

📄 License

This package is released under the MIT License.