Flutter TextField Widget for Currency Input

You are currently viewing Flutter TextField Widget for Currency Input
Currency Input

Flutter TextField Widget for Currency Input

MoneyTextFormField

MoneyTextFormField is one of the flutter widget packages that can be used to input values in the form of currencies, by displaying the output format in realtime.
This widget uses the FlutterMoneyFormatter package as a basic engine that has a very powerful ability to format currencies.

Dependencies :

intl

Install

For complete steps in installing MoneyTextFormField you can see in the Installation Guide.

Usage

The following is the simplest example of using MoneyTextFormField:

import 'package:moneytextformfield/moneytextformfield.dart';

  /// ... some lines of code ...
  MoneyTextFormField(
    settings: MoneyTextFormFieldSettings()
  )
  /// ... some lines of code ...

For those of you who have not yet understood how to implement the widget package, you can use the following code in the main.dart file on your Flutter project:

import 'package:flutter/material.dart';
import 'package:moneytextformfield/moneytextformfield.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  TextEditingController mycontroller = TextEditingController();

  @override
  void initState() {  
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('MoneyTextFormField Demo'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => print(mycontroller.text),
          child: Icon(Icons.save),
        ),
        body: Column(
          children: <Widget>[
            /// Begin of :> MoneyTextFormField
            MoneyTextFormField(
              settings: MoneyTextFormFieldSettings(
                controller: mycontroller
              )
            )
            /// End of :> MoneyTextFormField
          ]
        )
      )
    );
  }
}

From the above code it will look more or less like the following:

Currency Input TextField
Currency Input TextField

By doing a little modification, you will get the following results:

Currency Input Field
Currency Input Field

Referring to the example code above, to retrieve the value inputted by the user, you can get it through the mycontroller.text as in the onPressed event in the FloatingActionButton widget.

See also  Pincode TextField Widget in Flutter

Configurations

For now, MoneyTextFormField only uses one property to configure the display of that object, the settings property that has a data type is an instance of MoneyTextFormFieldSettings.

MoneyTextFormFieldSettings

NameData TypeDescription
controllerTextEditingControllercontroller for an editable text field.
validatorFormFieldValidator<String>An optional method that validates an input. Returns an error string to display if the input is invalid, or null otherwise.
inputFormattersList<TextInputFormatter>TextInputFormatter can be optionally injected to provide as-you-type validation and formatting of the text being edited.
onChangedvoidAn optional method that register a closure to be called when the object changes.
moneyFormatSettingsMoneyFormatSettingsSee here
appearanceSettingsAppearanceSettingsSee here
enabledboolWhether the form is able to receive user input.

Tips:

No need to initialize the value in controller.text, because the value will be ignored. the controller property is only intended to capture the value inputted by the user.

Notes:

See also  Dropdown FormField in Flutter

The value contained in controller.text is exactly the same as the one inputted by the user and has a String data type. If you want to get results in the same format, you can use the FlutterMoneyFormatter package which is also used by MoneyTextFormField.

See detailed information about FlutterMoneyFormatter.

AppearanceSettings

NameData TypeDescription
labelTextStringText that describes the input field. Default value is 'Amount'
hintTextStringText that suggests what sort of input the field accepts.
iconWidgetAn icon to show before the input field and outside of the decoration’s container.
labelStyleTextStyleThe style to use for the labelText when the label is above (i.e., vertically adjacent to) the input field.
inputStyleTextStyleThe style to use for the input field.
formattedStyleTextStyleThe style to use for the formatted output text.
errorStyleTextStyleThe style to use for the errorText
paddingEdgeInsetGeometryThe amount of space by which to inset the widget

MoneyFormatSettings

NameData TypeDescription
amountdoubleDecimal value that will be used when initializing the widget. Default value is 0.00.
fractionDigitsintThe fraction digits that will be used on formatted output. Default value is 2.
currencySymbolStringThe symbol that will be used on formatted output. Default value is '$' (dollar sign).
thousandSeparatorStringThe character that will be used as thousand separator on formatted output. Default value is ',' (comma).
decimalSeparatorStringThe character that will be used as decimal separator on formatted output. Default value is '.' (dot).
symbolAndNumberSeparatorStringThe character that will be used as separator between symbol and number. Default value is ' ' (space).
displayFormatMoneyDisplayFormatSee here

MoneyDisplayFormat

MoneyDisplayFormat is an enum object with values such as the following:

See also  Tags TextField Widget in Flutter
NameDescription
nonSymbolUsed to display currency values in full format and without a currency symbol.
symbolOnLeftUsed to display currency values in full format with currency symbols on the left.
symbolOnRightUsed to display currency values in full format with currency symbols on the right.
compactNonSymbolUsed to display currency values in a short format and without a currency symbol.
compactSymbolOnLeftUsed to display currency values in a short format with a currency symbol on the left.
compactSymbolOnRightUsed to display currency values in a short format with a currency symbol on the right.

Demo

For more complete samples, you can grab it from the example directory.

Credits:
This is developed by fadhly-permata
Download this project from the below link.
https://github.com/fadhly-permata/flutter_moneytextfieldform/archive/refs/heads/master.zip
You can visit original source page from the below link:
https://github.com/fadhly-permata/flutter_moneytextfieldform

Spread the love

Leave a Reply