Field.currency constructor

Field.currency(
  1. String key, {
  2. String? label,
  3. required String currency,
  4. String? value,
  5. FormValidator? validator,
  6. bool autofocus = false,
  7. String? dummyData,
  8. Widget? header,
  9. Widget? footer,
  10. TextStyle? titleStyle,
  11. bool? hidden = false,
  12. bool? readOnly,
  13. dynamic onChanged(
    1. dynamic value
    )?,
  14. FieldStyleTextField? style,
  15. bool separateSymbol = false,
})

Field.currency is a constructor that helps in managing currency fields

With separateSymbol, the symbol is drawn beside the amount in the hint color, e.g. a grey \$ before 1,200.00, and the field's value is the amount alone.

Implementation

Field.currency(
  this.key, {
  this.label,
  required String currency,
  String? value,
  this.validator,
  this.autofocus = false,
  this.dummyData,
  this.header,
  this.footer,
  this.titleStyle,
  this.hidden = false,
  this.readOnly,
  Function(dynamic value)? onChanged,
  FieldStyleTextField? style,
  bool separateSymbol = false,
}) : _value = value,
     style = style ?? FieldStyleTextField() {
  CurrencyMeta currencyMeta = CurrencyInputMatcher.getCurrencyMeta(
    "currency:$currency",
    value: value ?? dummyData ?? "0",
    onChanged: onChanged,
    separateSymbol: separateSymbol,
  );

  this.style = (this.style as FieldStyleTextField).copyWith(
    type: "currency",
    keyboardType: const TextInputType.numberWithOptions(decimal: true),
    inputFormatters: [currencyMeta.formatter],
  );
  if (separateSymbol) {
    // The symbol sits beside the amount, in the hint color.
    final FieldStyleTextField current = this.style as FieldStyleTextField;
    final Widget symbol = _CurrencySymbol(
      currencyMeta.symbol,
      leads: currencyMeta.symbolLeads,
    );
    const BoxConstraints tight = BoxConstraints(minWidth: 0, minHeight: 0);
    this.style = current.copyWith(
      prefixIcon: currencyMeta.symbolLeads ? symbol : null,
      suffixIcon: currencyMeta.symbolLeads ? null : symbol,
      decoration: (current.decoration ?? const InputDecoration()).copyWith(
        prefixIconConstraints: tight,
        suffixIconConstraints: tight,
      ),
    );
  }

  _value = currencyMeta.initialValue;
  dummyData = null;

  setOnChanged(onChanged);
  widget = NyFormTextField.fromField(this);
}