dart - Flutter TextField with currency format

Dart - Flutter TextField with currency format

In Flutter, if you want to create a TextField that accepts currency input and displays it in a formatted way (like "$1,234.56"), you need to consider how the formatting interacts with user input. To achieve this, you can use a TextEditingController with a custom TextInputFormatter to handle the currency formatting logic.

Here's a basic approach to creating a TextField with currency formatting in Flutter:

  1. Custom Text Input Formatter: Create a custom TextInputFormatter to format the text as the user types. This formatter should handle adding/removing commas, currency symbols, and managing decimal places.

  2. Using a TextEditingController: Use a TextEditingController to manage the text in the TextField. Update the controller with the formatted text when the user interacts with the field.

  3. Create a Stateless/Stateful Widget: Implement a widget that uses the custom formatter and controller to achieve the desired currency formatting.

Here's an example of a custom currency formatter and its use in a Flutter widget:

dependencies: flutter: sdk: flutter intl: # For currency formatting 
import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:intl/intl.dart'; class CurrencyTextInputFormatter extends TextInputFormatter { final NumberFormat _currencyFormat = NumberFormat.currency(symbol: "\$", decimalDigits: 2); @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue, ) { // Remove any characters that aren't digits or decimals final cleaned = newValue.text.replaceAll(RegExp(r'[^\d.]'), ''); // If there's more than one decimal point, ignore the input if (cleaned.indexOf('.') != cleaned.lastIndexOf('.')) { return oldValue; } // Convert the cleaned string to a number final double? value = double.tryParse(cleaned); if (value == null) { return oldValue; } // Format as currency final formatted = _currencyFormat.format(value); // Calculate the new selection offset final newSelectionOffset = formatted.length - (cleaned.length - newValue.selection.baseOffset); return TextEditingValue( text: formatted, selection: TextSelection.collapsed(offset: newSelectionOffset), ); } } class CurrencyInputField extends StatelessWidget { final TextEditingController _controller = TextEditingController(); @override Widget build(BuildContext context) { return TextField( controller: _controller, inputFormatters: [CurrencyTextInputFormatter()], keyboardType: TextInputType.numberWithOptions(decimal: true), decoration: InputDecoration( labelText: 'Enter amount', ), ); } } void main() { runApp(MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Currency TextField Example')), body: Padding( padding: const EdgeInsets.all(16.0), child: CurrencyInputField(), ), ), )); } 

In this example:

  • The CurrencyTextInputFormatter class formats the input to show currency with a dollar symbol, commas, and two decimal places.
  • The CurrencyInputField widget uses this formatter to manage the text field's input and formatting.
  • As the user types, the formatter updates the text to maintain the desired currency format, adjusting the selection to keep user interactions consistent.

Examples

  1. Flutter TextField currency format example

    • Description: This query seeks examples of how to format a TextField to accept currency input with proper formatting.
    TextField( keyboardType: TextInputType.number, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')), CurrencyInputFormatter() ], decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  2. Flutter TextField currency mask

    • Description: Indicates a need to apply a currency mask to a TextField in a Flutter app to format the input properly.
    TextField( keyboardType: TextInputType.number, inputFormatters: [CurrencyInputFormatter()], decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  3. Flutter TextField currency formatter

    • Description: This query looks for ways to implement a currency formatter for a TextField in a Flutter app.
    TextField( keyboardType: TextInputType.number, inputFormatters: [CurrencyInputFormatter()], decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  4. Flutter TextField currency input

    • Description: Indicates a need to accept currency input in a TextField and format it properly.
    TextField( keyboardType: TextInputType.number, inputFormatters: [CurrencyInputFormatter()], decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  5. Flutter TextField currency mask example

    • Description: This query seeks examples of how to apply a currency mask to a TextField in a Flutter app.
    TextField( keyboardType: TextInputType.number, inputFormatters: [CurrencyInputFormatter()], decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  6. Flutter TextField currency format on change

    • Description: Indicates a need to format the input in a TextField to currency format as the user types.
    TextField( onChanged: (value) { final _currencyFormat = NumberFormat.currency(locale: 'en_US', symbol: '\$'); String formattedValue = _currencyFormat.format(double.parse(value)); // Do something with formattedValue }, decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  7. Flutter TextField currency formatter example

    • Description: This query seeks examples of how to implement a currency formatter for a TextField in a Flutter app.
    TextField( keyboardType: TextInputType.number, inputFormatters: [CurrencyInputFormatter()], decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  8. Flutter TextField currency input example

    • Description: Indicates a need for examples on accepting currency input in a TextField and formatting it properly.
    TextField( keyboardType: TextInputType.number, inputFormatters: [CurrencyInputFormatter()], decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  9. Flutter TextField currency format on typing

    • Description: This query looks for ways to format the input in a TextField to currency format as the user types.
    TextField( onChanged: (value) { final _currencyFormat = NumberFormat.currency(locale: 'en_US', symbol: '\$'); String formattedValue = _currencyFormat.format(double.parse(value)); // Do something with formattedValue }, decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 
  10. Flutter TextField currency format on focus

    • Description: Indicates a need to format the input in a TextField to currency format when it gains focus.
    TextField( focusNode: _focusNode, onChanged: (value) { final _currencyFormat = NumberFormat.currency(locale: 'en_US', symbol: '\$'); String formattedValue = _currencyFormat.format(double.parse(value)); // Do something with formattedValue }, decoration: InputDecoration( labelText: 'Enter amount', prefixText: '\$', ), ) 

More Tags

azure-pipelines capitalize jboss uicolor char python-mock asp.net-core-tag-helpers gregorian-calendar summarize jsp-tags

More Programming Questions

More Mixtures and solutions Calculators

More Chemistry Calculators

More Chemical reactions Calculators

More Bio laboratory Calculators